我几乎是Spring的初学者,所以不要以为我没有提到我可能做过的事情。无法获得Spring依赖注入工作
我试图让依赖注入的工作,我有以下内容的spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.1.xsd">
<context:annotation-config/>
<bean id="connection" class="richo.project.ConnectionImpl"/>
</beans>
,然后在我的代码有:
private IConnection conn;
@Resource(name="connection")
public void setConn(IConnection conn){
this.conn = conn;
}
当我尝试在我的代码中使用conn对象我得到一个nullpointerexception
请记住,我实际上并不知道如果弹簧运行,我使用IntelliJ,它放置了13个与弹簧相关的jar文件在我的lib目录中,但我无法确定Spring是否尝试注入任何东西
因此,类中的注释只有在Spring实例化时才有效,这意味着如果我仅仅使用新的,spring将会没有阅读这些注释? –
Richo
2012-01-01 13:48:16
Guice的确如此。我不确定Spring是否属于这种情况(关于_how_很多classloader魔术的问题已经到位)。 – 2012-01-01 13:50:59
@Richo:是的,Spring只注入它创建的bean。你实例化自己的豆不会被注入。 – 2012-01-01 13:54:29