【发布时间】:2014-12-13 03:25:09
【问题描述】:
如果这个问题已经在某个地方提出或回答,我深表歉意。我有一个示例代码,它没有按预期工作。 Spring 注释 @Autowired 不起作用。而在相同的设置中,我有 @Inject 使用 Weld 1.1.0 和 Tomcat 6。
WorkTester.java
//1. Do I need to mark some class as component to wire a property in that class? Intellij
//always warns if I don't.
@Component
public class WorkTester {
@Autowired
@Qualifier("work")
private Work work;
//2. There is a bean in MVC context with name work. Why doesn't it work?
public void testWork() {
System.out.println("Work : " + work);
}
public Work getWork() {
return work;
}
//2. If there is a bean called 'work' in any of the contexts, do I need setters?
public void setWork(Work work) {
this.work = work;
}
}
WorkImpl.java
public class WorkImpl implements Work {
@Override
public void doWork(String task) {
System.out.println("Yahoo! " + task);
}
}
我有applicationContext.xml,它是空的。我的dispatcher-servet.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.brs.in"/>
<bean id="work" class="com.brs.in.WorkImpl"/>
</beans>
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.brs.in.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<resource-env-ref>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
</web-app>
正如代码第一个 sn-p 中的 cmets(问题 1、2、3) 中所述,谁能回答为什么自动装配不起作用?
入口点:
这是我调用导致NullPointerException的方法的示例servlet:
@Inject
private Work worker;
@Override
protected void doGet(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Worker has been injected " + ((worker != null)));
worker.doWork(1, 2);//No Problem
new WorkTester().testWork();//Causing trouble
}
【问题讨论】:
-
你说的是:没有按预期工作。实际发生了什么?
-
@benji work 始终为空。
-
您是否提供了
-
@kamoor1982 上下文组件扫描在那里。
-
试试这个
标签: java spring spring-mvc dependency-injection autowired