我正在開發某種應由外部java應用程序調用的插件。 我的插件使用春天,因爲我試圖簡化我的,因爲我可以:如何在Spring應用程序上下文外創建Spring Bean
讓我們考慮這是3D派對應用程序,它的主要功能調用我的插件。
public class ThirdPartyClass {
public static void main(String[] args) {
GeneralPlugin plugin = new MyPlugin();
plugin.init();
//calling ext. plugin functionality.
plugin.method1();
}
}
現在,這是我的插件
package com.vanilla.spring;
@Component
public class MyPlugin implements GeneralPlugin{
@Autowired
Dao mydao;
public void init(){
//some initiation logic goes here...
}
public void method1(){
dao.delete();
}
}
現在我的道
package com.vanilla.spring;
Component(value="dao")
public class MyDao {
public void delete(){
//SOME DATABASE LOGIC GOES HERE!!!
}
}
現在我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
<context:annotation-config />
<context:component-scan base-package="com.vanilla.spring"></context:component-scan>
</beans>
我的問題是,我道是空和我訪問dao時會得到NullPointerException
對象。
我 米相信它是因爲我萌生出豆應用程序上下文和我的自動裝配不正常的結果。
是否有任何其他方法,使自動裝配工作?
你的代碼需要初始化Spring上下文 - 除非你告訴它春天不會初始化。 –