[英]Spring constructor injection
Is it possible to construct an object manually and let some other arguments be injected by Spring? 是否可以手动构造一个对象并让Spring注入其他一些参数?
eg 例如
class A
@Autowired
private SomeDao dao;
A(String x, String y) {}
Your example is using field injection, not constructor injection. 您的示例使用的是字段注入,而不是构造函数注入。
The best way is generally to use JavaConfig. 最好的方法通常是使用JavaConfig。 Your
@Bean
methods can take parameters (which Spring will autowire), which you can combine with your other options when you call new
. 您的
@Bean
方法可以使用参数(Spring将自动连接这些参数),您可以在调用new
时将其与其他选项结合使用。
也许org.springframework.web.context.support.SpringBeanAutowiringSupport类是您正在寻找的类,请尝试调用:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
You can use autowireBean from the AutowireCapableBeanFactory . 您可以从AutowireCapableBeanFactory使用autowireBean。 Given your applicationContext, you call getAutowireCapableBeanFactory() and then autowire your instance:
给定您的applicationContext,您可以调用getAutowireCapableBeanFactory() ,然后自动装配您的实例:
applicationContext.getAutowireCapableBeanFactory().autowireBean( new A("x", "y" ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.