@Autowired

介绍

在服务启动的时候,实现bean的依赖注入。

使用

加在构造函数上
  • 当一个对象只有一个构造方法时,Spring在实例化对象时会默认选择该构造方法来进行实例化,无需添加@Autowired注解;只有存在多个构造方法时,才需要通过添加@Autowired注解的方式来告诉Spring应当选择哪个构造方法来进行实例化;
  • 如果构造方法上带有了@Autowired注解,那么即使该构造方法不是public修饰的,也可以被实例化;
  • 实例化带参数的构造方法时,Spring会先实例化构造方法中参数对象。
  1. 情景一:
@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]
  1. 情景二
@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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SONNIE在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值