Spring新手入门
重新梳理一下Spring
spring装配bean的三种方式
Spring配置的可选方案:
- XML显示配置
- Java显示配置
- 隐式的bean发现机制和自动装配(自动配置机制更好,更强大)
隐式的bean发现机制和自动装配
1.首先创建一个接口
package soundsystem;
public interface CompactDisc {
void play();
}
2.实现接口
@Component 注解表明该类会作为组件类,同时通知spring为这个类创建bean
如果对@Component不加任何设置,默认其自动生成的BeanID为其首字母小写(sgtPeppers)
对其传递参数设置时,id就是testhappy如下
@Component("testhappy")
package soundsystem;
import org.springframework.stereotype.Component;
@Component
public class SgtPeppers implements CompactDisc{
private String title="Sge. Pepper,s Lonely Hearts Club Band";
public void play() {
}
}
3.启动注解扫描
在CDPlayerConfig中没有显示申明任何bean,但是@Configuration 相当于配置类(可以替换xml配置), @ComponentScan相当于自动扫描与配置类相同的包。 在该案例中就是扫描soundsystem包。在查找到@Component注解的类时,spring就会自动为其创建一个bean。
如果不对 @ComponentScan加任何参数,默认就是当前包, 也可以对其增加参数比如以下设置将会扫描com下面test包
@ComponentScan(basePackages={"com.test"})
package soundsystem;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
@ComponentScan
public class CDPlayerConfig {
}
4.测试结果
package soundsystem;
import org.junit.runner.RunWith;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull(){
assertNotNull(cd);
}
}
将会成功生成一个bean对象:CompactDisc cd;
此外,@Autowired 方法可以诸恶街道类的任何方法上,在spring创建bean时,会根据该注解进行自动装配。
Java显示配置
第三方库的代码合到自己的项目时你可能没有办法直接添加@Component和@Autowired注解,此时你可以使用JAVA显示配置。
1.首先创建一个接口
package soundsystem;
public interface CompactDisc {
void play();
}
2.实现接口
@Component 注解表明该类会作为组件类,同时通知spring为这个类创建bean
如果对@Component不加任何设置,默认其自动生成的BeanID为其首字母小写(sgtPeppers)
对其传递参数设置时,id就是testhappy如下
@Component("testhappy")
package soundsystem;
import org.springframework.stereotype.Component;
@Component
public class SgtPeppers implements CompactDisc{
private String title="Sge. Pepper,s Lonely Hearts Club Band";
public void play() {
}
}
3.启动注解扫描
在CDPlayerConfig中没有显示申明任何bean,但是@Configuration 相当于配置类(可以替换xml配置)此处可以删除@componentScan扫描;但是在同时也需要进行bean申明,最简单的方法就是直接@Bean,
package javaconfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
public class CDPlayerConfig {
@Bean
public CompactDisc sgtPeppers() {
return new SgtPeppers();
}
}
4.测试结果
package soundsystem;
import org.junit.runner.RunWith;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull(){
assertNotNull(cd);
}
}
将会成功生成一个bean对象:CompactDisc cd;
此外,@Autowired 方法可以诸恶街道类的任何方法上,在spring创建bean时,会根据该注解进行自动装配。
XML显示配置
正常来说,学习xml配置通常是帮助你维护已有的xml配置项目.在springboot几乎很少见了
借鉴
Spring实战开发第四版
相关链接
1.借助Spring Tool Suite创建xml配置文件创建和管理SpringX没了配置文件的一种方式使用(https://spring.io.tools/sts)