我是新來的泉水。有沒有其他的自動裝配可以用在普通的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,是的,你是對的 –