spring@inject在控制器中不起作用



我使用的是context:组件扫描,但依赖项仍然没有被注入。

这是我的设置。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.LogiController.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不行。

即使使用<context:component-scan />,仍然需要<mvc:annotation-driven />,否则将不会检测到@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>

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

<context:annnotation-config/><context:component-scan/>将注册一个AutowiredAnnotationBeanPostProcessor,负责处理@Autowired、@Resource和@Inject annotations

我认为您的applicationContext.xml丢失了:

<context:annotation-config>

这为基于注释的配置注册了Springs后处理器。

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

LoginController中:

@Inject
private ConnectionHelper connectionHelper;
public ConnectionHelper getConnectionHelper() {
    return connectionHelper;
}
public void setConnectionHelper(ConnectionHelper connectionHelper) {
    this.connectionHelper = connectionHelper;
}

最新更新