Bean的装配
-
@Autowired:使用@Autowired将Bean注入到其他spring管理的类中
- 当一个类在IOC容器中存在多个实例,则需要配合@Qualifier("xxxx"),xxx为bean的id进行注入
- @Resouce:使用JSR250的注解@Resouce将Bean注入到其他的类中
- 如果一个类在IOC容器中存在多个实例,可以使用
@Resource(name = "xxxxx")进行引用,其中xxxxx为Bean的id
- @Qualifier("xxxx")进行引用,其中xxxx为bean的id
- 如果一个类在IOC容器中存在多个实例,可以使用
- @Inject: JSR330的注解,将其他的类注入(可以在不是spring的环境下使用)
@Autowired--默认通过byType引入
@Configuration
@ComponentScan(value = {"com.lb.spring.day4.part14"})
public class MyConfigration14 {
}
@Controller
public class LbController {
@Autowired
private LbService lbService;
public LbService getLbService() {
return lbService;
}
}
@Service
public class LbService {
}
public class Part14Test {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration14.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names){
System.out.println(name);
}
LbService lbService = (LbService) app.getBean("lbService");
LbController lbController = (LbController) app.getBean("lbController");
System.out.println(lbService);
System.out.println(lbController.getLbService());
System.out.println("IOC 容器初始化完成....");
}
}
结果:
从结果发现@Autowired的注入成功,并且注入的对象和IOC容器中的为同一个对象
myConfigration14
lbController
lbService
com.lb.spring.day4.part14.service.LbService@2bbf180e
com.lb.spring.day4.part14.service.LbService@2bbf180e
当同时注入多次Bean,并且Bean的id相同:
@Configuration
@ComponentScan(value = {"com.lb.spring.day4.part14"})
public class MyConfigration14 {
@Bean("lbService")
public LbService lbService(){
LbService lbService = new LbService();
lbService.setFlag(2);
return lbService;
}
}
@Service
public class LbService {
private int flag = 1;
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
@Override
public String toString() {
return "LbService{" +
"flag=" + flag +
'}';
}
}
public class Part14Test {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration14.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names){
System.out.println(name);
}
LbService lbService = (LbService) app.getBean("lbService");
System.out.println(lbService);
System.out.println("IOC 容器初始化完成....");
}
}
结果:从结果可以看出注入的为@Configuration中@Bean注入的类,因为加载的顺序,@Bean先被注入,@Service没有被注入
myConfigration14
lbController
lbService
LbService{flag=2}
IOC 容器初始化完成....
当同时注入多次Bean,并且Bean的id不相同,使用@Qualifier:
@Configuration
@ComponentScan(value = {"com.lb.spring.day4.part14"})
public class MyConfigration14 {
@Bean("lbService02")
public LbService lbService(){
LbService lbService = new LbService();
lbService.setFlag(2);
return lbService;
}
}
@Controller
public class LbController {
@Qualifier(value = "lbService02")
@Autowired
private LbService lbService;
public LbService getLbService() {
return lbService;
}
}
public class Part14Test {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration14.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names){
System.out.println(name);
}
LbService lbService = (LbService) app.getBean("lbService");
LbController lbController = (LbController) app.getBean("lbController");
System.out.println(lbService);
System.out.println(lbController.getLbService());
System.out.println("IOC 容器初始化完成....");
}
}
结果:IOC容器中包含多个LbService的Bean,Controller中引入的是id=lbService02的Bean
myConfigration14
lbController
lbService
lbService02
LbService{flag=1}
LbService{flag=2}
IOC 容器初始化完成....
@Resource--默认通过byName引入
使用@Resource进行Bean的注入
@Controller
public class LbController {
@Resource
private LbService lbService;
public LbService getLbService() {
return lbService;
}
}
public class Part14Test {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration14.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names){
System.out.println(name);
}
LbController lbController = (LbController) app.getBean("lbController");
System.out.println(lbController.getLbService());
System.out.println("IOC 容器初始化完成....");
}
}
结果:通过@Resource引入
myConfigration14
lbController
lbService
lbService02
LbService{flag=1}
当同时注入多次Bean,并且Bean的id不相同
@Controller
public class LbController {
@Autowired
private LbService lbService;
@Resource(name = "lbService02")
private LbService lbService02;
public LbService getLbService() {
return lbService;
}
public LbService getLbService02() {
return lbService02;
}
}
public class Part14Test {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration14.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names){
System.out.println(name);
}
LbController lbController = (LbController) app.getBean("lbController");
System.out.println(lbController.getLbService());
System.out.println(lbController.getLbService02());
System.out.println("IOC 容器初始化完成....");
}
}
结果:可以看出LbService在IOC容器中存在多个实例,@Resource可以通过id name进行分别注入
myConfigration14
lbController
lbService
lbService02
LbService{flag=1}
LbService{flag=2}
IOC 容器初始化完成....
@Primary--优先注入
当一个Bean在IOC容器中存在多个实例时,即使Bean的id不相同,如果在一个Bean的注入时使用@Primary,并且使用@Autowired引入时都会先注入该类
@Configuration
@ComponentScan(value = {"com.lb.spring.day4.part15"})
public class MyConfigration15 {
@Bean("lbService02")
public LbService lbService(){
LbService lbService = new LbService();
lbService.setFlag(2);
return lbService;
}
}
@Service
public class LbService {
private int flag = 1;
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
@Override
public String toString() {
return "LbService{" +
"flag=" + flag +
'}';
}
}
@Controller
public class LbController {
@Autowired
private LbService lbService;
public LbService getLbService() {
return lbService;
}
}
public class Part15Test {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration15.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names){
System.out.println(name);
}
LbController lbController = (LbController) app.getBean("lbController");
System.out.println(lbController.getLbService());
System.out.println("IOC 容器初始化完成....");
}
}
结果:当@Autowired引入时,因为byType存在多个,则使用byName注入了id=lbService的实例
myConfigration15
lbController
lbService
lbService02
LbService{flag=1}
IOC 容器初始化完成....
当在@Bean("lbService02")上使用了@Primary时,则按照byType会直接注入这个Bean
@Configuration
@ComponentScan(value = {"com.lb.spring.day4.part15"})
public class MyConfigration15 {
@Primary
@Bean("lbService02")
public LbService lbService(){
LbService lbService = new LbService();
lbService.setFlag(2);
return lbService;
}
}
结果:直接注入了@Bean("lbService02")这个Bean,直接byType时直接注入这个优先注入的Bean
myConfigration15
lbController
lbService
lbService02
LbService{flag=2}
IOC 容器初始化完成....
@Inject
使用JSR-330引入的注解@Inject对Bean进行注入
@Configuration
@ComponentScan(value = {"com.lb.spring.day4.part16"})
public class MyConfigration16 {
}
@Service
public class LbService {
private int flag = 1;
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
@Override
public String toString() {
return "LbService{" +
"flag=" + flag +
'}';
}
}
@Controller
public class LbController {
@Inject
private LbService lbService;
public LbService getLbService() {
return lbService;
}
}
public class Part16Test {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration16.class);
String[] names = app.getBeanDefinitionNames();
for (String name : names){
System.out.println(name);
}
LbController lbController = (LbController) app.getBean("lbController");
System.out.println(lbController.getLbService());
System.out.println("IOC 容器初始化完成....");
}
}
结果:可以从结果发现使用@Inject也可以使用实现Bean注入的功能
myConfigration16
lbController
lbService
LbService{flag=1}
IOC 容器初始化完成....