我正在开发某种应由外部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上下文 - 除非你告诉它春天不会初始化。 –