0

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

    } 
phoenix
  • 985
  • 3
  • 17
  • 38

1 Answers1

-1

include this to your dispatcher-servlet.xml <context:annotation-config/> @Autowired annotation alone is not enough you need to enable it. Ref.

<context:component-scan base-package="com.brs.in"/> is used to scan those controller as far as i know . but still im not sure :)

i just notice about how you create your dispatcher on your web.xml it should be like this.

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

from spec.

The load-on-startup element indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the web application. The optional contents of these element must be an integer indicating the order in which the servlet should be loaded. If the value is a negative integer, or the element is not present, the container is free to load the servlet whenever it chooses. If the value is a positive 128 integer or 0, the container must load and initialize the servlet as the application is deployed. The container must guarantee that servlets marked with lower integers are loaded before servlets marked with higher integers. The container may choose the order of loading of servlets with the same load-on-start-up value.

Secondo
  • 451
  • 4
  • 9