Spring @Autowired属性为null

时间:2014-12-13 03:25:10

标签: java spring spring-mvc dependency-injection autowired

如果这个问题已在某个地方被提出或回答,我道歉。我有一个示例代码,它没有按预期工作。 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>

正如第一段代码中的评论(问题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

    } 

1 个答案:

答案 0 :(得分:-1)

将此内容添加到您的dispatcher-servlet.xml <context:annotation-config/>仅@Autowired注释是不够的,您需要启用它。 Ref.

据我所知,

<context:component-scan base-package="com.brs.in"/>用于扫描这些控制器。但我仍然不确定:)

我只是注意到您在web.xml上创建调度员的方式应该是这样的。

<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>

来自规范。

  

load-on-startup元素指示此servlet应该是   在启动时加载(实例化并调用其init())   Web应用程序。这些元素的可选内容必须是   整数,指示应加载servlet的顺序。如果   该值是一个负整数,或者该元素不存在,   容器可以随时加载servlet。如果   值是一个正128的整数或0,容器必须加载和   在部署应用程序时初始化servlet。容器   必须保证加载标有较低整数的servlet   在用更高整数标记的servlet之前。容器可以选择   具有相同加载启动值的servlet的加载顺序。