I apologize if this question has already been asked or answered somewhere. I have a sample code, which is not working as intended. Spring annotation @Autowired
is not working. Whereas in the same setup I have @Inject
working with Weld 1.1.0 and 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);
}
}
I have applicationContext.xml
, which is empty. My dispatcher-servet.xml
is
<?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>
My 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>
As mentioned in the comments(Questions 1, 2, 3) in the first snippet of code, can anyone answer why autowiring is not working?
Entry Point:
This is the sample servlet where I am calling the method causing 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
}