介绍
在服务启动的时候,实现bean的依赖注入。
使用
加在构造函数上
- 当一个对象只有一个构造方法时,Spring在实例化对象时会默认选择该构造方法来进行实例化,无需添加@Autowired注解;只有存在多个构造方法时,才需要通过添加@Autowired注解的方式来告诉Spring应当选择哪个构造方法来进行实例化;
- 如果构造方法上带有了@Autowired注解,那么即使该构造方法不是public修饰的,也可以被实例化;
- 实例化带参数的构造方法时,Spring会先实例化构造方法中参数对象。
- 情景一:
@Component
public class AnnoAutowiredService {
private OrderService orderService;
public Map<String, OrderService> orderServiceMap;
protected List<OrderService> orderServiceList;
public AnnoAutowiredService(OrderService orderService, Map<String, OrderService> orderServiceMap, List<OrderService> orderServiceList) {
this.orderService = orderService;
this.orderServiceMap = orderServiceMap;
this.orderServiceList = orderServiceList;
}
public void test() {
System.out.println(orderService);
System.out.println(orderServiceMap);
System.out.println(orderServiceList);
}
}
com.sonnie.service.OrderService@3cd1f1c8
{orderService=com.sonnie.service.OrderService@3cd1f1c8, orderService2=com.sonnie.service.OrderService@3a4afd8d}
[com.sonnie.service.OrderService@3cd1f1c8, com.sonnie.service.OrderService@3a4afd8d]
- 情景二
@Component
public class AnnoAutowiredService {
private OrderService orderService;
public Map<String, OrderService> orderServiceMap;
protected List<OrderService> orderServiceList;
@Autowired(required = false)
public AnnoAutowiredService() {
}
@Autowired(required = true)
public AnnoAutowiredService(OrderService orderService, Map<String, OrderService> orderServiceMap, List<OrderService> orderServiceList) {
this.orderService = orderService;
this.orderServiceMap = orderServiceMap;
this.orderServiceList = orderServiceList;
}
public void test() {
System.out.println(orderService);
System.out.println(orderServiceMap);
System.out.println(orderServiceList);
}
}
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'annoAutowiredService': Invalid autowire-marked constructors: [public com.sonnie.annotest.AnnoAutowiredService()]. Found constructor with 'required' Autowired annotation: public com.sonnie.annotest.AnnoAutowiredService(com.sonnie.service.OrderService,java.util.Map,java.util.List)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1323)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1234)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:570)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:346)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:237)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:344)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:967)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:960)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:605)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:95)
at com.sonnie.Test.main(Test.java:12)
加在属性上
即使属性不是public的,也可以被赋值。
@Component
public class AnnoAutowiredService {
@Autowired
private OrderService orderService;
@Autowired
public Map<String, OrderService> orderServiceMap;
@Autowired
protected List<OrderService> orderServiceList;
public void test() {
System.out.println(orderService);
System.out.println(orderServiceMap);
System.out.println(orderServiceList);
}
}
打印结果:
com.sonnie.service.OrderService@3cd1f1c8
{orderService=com.sonnie.service.OrderService@3cd1f1c8, orderService2=com.sonnie.service.OrderService@3a4afd8d}
[com.sonnie.service.OrderService@3cd1f1c8, com.sonnie.service.OrderService@3a4afd8d]
加在方法上
当Spring容器调用该类的构造方法实例化完成该类后,会调用@Autowired修饰的方法,并将方法的参数进行实例化。也就是说,我们如果想实例化完成一个类后,执行某个方法时,可以用@Autowired注解来修饰。Spring会自动给我们执行该方法,有点类似于init-method的用法。但是必须保证该方法的参数对象在Spring容器中也存在,否则会报错。
@Component
public class AnnoAutowiredService {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
public void test() {
System.out.println(userService);
}
}
com.sonnie.service.UserService@5fe5c6f