spring @inject无法在Controller中工作

时间:2012-07-24 10:54:37

标签: spring java-ee spring-mvc

我正在使用上下文:组件扫描,仍然没有注入依赖项。

这是我的设置。 ConnectionHelper属性未在LoginController类中注入。

WEB-INF / web.xml中

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<servlet>
    <servlet-name>vd</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>



<servlet-mapping>
    <servlet-name>vd</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

WEB-INF / applicationContext.xml的

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<context:component-scan base-package="com.example" />

</beans>

WEB-INF / VD-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Configures Handler Interceptors -->
<mvc:interceptors>
    <bean class="com.example.interceptors.SslInterceptor" />
    <mvc:interceptor>
        <mvc:mapping path="/login" />
        <bean class="com.example.interceptors.SslInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".html" />
</bean>
</beans>

com.example.controllers.LoginController.java

@Controller
public class LoginController {

@Inject
private ConnectionHelper connectionHelper;    //this is null after loading

}

com.example.connection.ConnectionHelper.java

@Component
public class ConnectionHelper {

}

请告诉我这里有什么问题......经过数小时的故障排除后我无法确定问题。

更新

我更改了配置并将所有内容移至vd-servlet.xml。

奇迹般地我发现@Autowired使用此配置工作(注入依赖关系),但@Inject没有。

即使您使用<mvc:annotation-driven />,仍然需要<context:component-scan />,否则无法检测到@RequestMapping。

VD-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<mvc:annotation-driven />
<context:component-scan base-package="com.example" />

<mvc:interceptors>
    <bean class="com.example.interceptors.SslInterceptor" />
    <mvc:interceptor>
        <mvc:mapping path="/login" />
        <bean class="com.example.interceptors.SslInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/WEB-INF/views/**" location="/WEB-INF/views/" />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".html" />
</bean>
</beans>

3 个答案:

答案 0 :(得分:1)

同意@Alex,只是您丢失的文件<context:component-scan/><context:annotation-config/>不是applicationContext.xml文件而是vd-servlet.xml文件。

<context:annnotation-config/><context:component-scan/>会注册AutowiredAnnotationBeanPostProcessor负责处理@Autowired,@ Resource,@ Inject annotations

答案 1 :(得分:0)

我相信你的applicationContext.xml丢失了:

<context:annotation-config>

这会为基于注释的配置注册弹簧后处理器。

答案 2 :(得分:0)

您需要向LoginController添加getter和setter。另一种选择是使connectionHelper成为protected变量。这就是Spring“看到”要注入的属性的方式。

LoginController

@Inject
private ConnectionHelper connectionHelper;

public ConnectionHelper getConnectionHelper() {
    return connectionHelper;
}

public void setConnectionHelper(ConnectionHelper connectionHelper) {
    this.connectionHelper = connectionHelper;
}