2

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

这是我的设置。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>
4

3 回答 3

1

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

要么<context:annnotation-config/><context:component-scan/>将注册一个负责处理@Autowired、@Resource、@Inject 注释的AutowiredAnnotationBeanPostProcessor

于 2012-07-24T19:33:50.853 回答
0

您需要将 getter 和 setter 添加到LoginController. 另一种选择是创建connectionHelper一个protected变量。这就是 Spring “看到”要注入的属性的方式。

LoginController

@Inject
private ConnectionHelper connectionHelper;

public ConnectionHelper getConnectionHelper() {
    return connectionHelper;
}

public void setConnectionHelper(ConnectionHelper connectionHelper) {
    this.connectionHelper = connectionHelper;
}
于 2012-07-24T17:52:07.983 回答
0

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

<context:annotation-config>

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

于 2012-07-24T14:34:19.950 回答