我是新来的泉水。有没有其他的自动装配可以用在普通的java bean中,而这个bean在spring的上下文中是不存在的。@Autowired in bean not in spring context
0
A
回答
1
你可以通过使用弹簧@Configurable以及一些AspectJ
魔法来实现。
如果您需要详细的解释,这里是link。
下面是如何实现它的简要概述。
首先,你有一些豆你想要的地方注入:
@Component
public class InjectedClass {
// ...
}
然后,你有没有春天,容器管理,要实例化一个类。你希望自动装配与这个类一起工作。您将其标记为@Configurable。
@Configurable
public class NonContainerManagedClass {
@Autowired
private InjectedClass injected;
// ...
}
现在您需要告诉spring您希望这个非容器管理的自动装配工作。所以你把下面的东西放在你的弹簧配置中。现在
<context:load-time-weaver />
<context:spring-configured />
,因为这种事情需要你@Configurable类的字节码的修改。所以你告诉Tomcat使用不同的classloader
。您可以通过在应用程序的META-INF
指南中创建一个context.xml
并将以下内容放在那里。现在
<Context path="/youWebAppName">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"
useSystemClassLoaderAsParent="false"/>
</Context>
,Tomcat的需要找到classloader
。你可以通过在你的tomcat安装的lib
目录中加入Spring的spring-tomcat-weaver.jar
(可能命名为org.springframework.instrument.tomcat-<version>.jar
),并且瞧,aspectj魔法开始工作。对于注解为@Configurable注释的类,@Autowired
依赖项会自动解析;即使这些实例是在弹簧容器外创建的。
这可能是以干净的方式与Spring合作的唯一方法。确保你在你的classpath
有适当的依赖关系。
另一种方法是使用完整的AspectJ功能,并为您的所有构造函数提供自定义方面,并自己处理依赖注入。
相关问题
- 1. Spring @Autowired Bean NullPointerException - Not Getting Wired
- 2. @Autowired bean not found
- 3. @Resource in Spring does not work?
- 4. Request scope backing bean ... not in request
- 5. Autowired spring bean是nul
- 6. Spring commonj.Workmanager in websphere
- 7. EXC_BAD_ACCESS in device..not in simulator。(Adwhirl)
- 8. Codeigniter Tank Auth Not Log In In?
- 9. IN NOT IN SQL Server 2005
- 10. LINQ to SQL in and not in
- 11. “=” 和 “IN”/ “NOT IN” 和 “MINUS”
- 12. Go Program in not in WIndows 7
- 13. isset in php not working in angularjs
- 14. LocaleResolver in filter null yet shows it has autowired! Spring MVC 3.1.1/Spring Security 3.1.0
- 15. Spring autowired bean创建
- 16. Spring @Autowired bean null null
- 17. Guice ServeContextListener not kick-in
- 18. NOT EXISTS VS NOT IN
- 19. LINQ“not in”not working
- 20. Font not not in DOMPDF
- 21. Datediff in Criteria operator in spring-data-mongodb does not working
- 22. Can not draw in CAlayer
- 23. tx:注解驱动的break @Autowired in scala
- 24. not working response.end()in Nodejs
- 25. Bean名为 - Spring in Action有错误吗?
- 26. datepicker not in modal
- 27. Where In Not - doctrine
- 28. display:inline not in IE8
- 29. NOT IN条件
- 30. NOT IN猪
所以你想要注入一个弹簧托管bean到非托管POJO? – WeMakeSoftware
你想让自动装配为你实例化的bean工作,而不是弹簧容器,那是你想要的吗? –
@Funtik,是的,你是对的 –