이 질문에 대한 답변이 이미 전달되었습니다. 의도 한대로 작동하지 않는 샘플 코드가 있습니다. 스프링 주석 @Autowired
이 작동하지 않습니다. 내가 @Inject
이 같은 설정에서 반면 용접 1.1.0과 톰캣 6봄 @Autowired 속성이 null입니다.
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>
에서 autowiring 인 이유 (질문 1, 2, 3) 코드의 첫 번째 코드에서, 사람이 대답 할 수 작동 안함?
진입 점은 :
이 내가 NullPointerException
를 일으키는 방법 호출하고있는 샘플 서블릿입니다 : 충분히
@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
@benji work는 항상 null입니다. – phoenix