Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring@inject在控制器中不工作_Spring_Jakarta Ee_Spring Mvc - Fatal编程技术网

spring@inject在控制器中不工作

spring@inject在控制器中不工作,spring,jakarta-ee,spring-mvc,Spring,Jakarta Ee,Spring Mvc,我使用的是context:component扫描,但是依赖项没有被注入 这是我的设置。未在LoginController类中注入ConnectionHelper属性 WEB-INF/WEB.xml <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-v

我使用的是context:component扫描,但是依赖项没有被注入

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

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>
com.example.connection.ConnectionHelper.java

@Component
public class ConnectionHelper {

}
请告诉我这里出了什么问题……经过数小时的故障排除,我无法确定问题所在

更新

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

我奇迹般地发现,
@Autowired
可以在这种配置下工作(注入依赖项),但
@Inject
不能

即使使用了
,仍然需要
,否则不会检测到@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">

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

我认为您的applicationContext.xml缺失:


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

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

登录控制器中

@Inject
private ConnectionHelper connectionHelper;

public ConnectionHelper getConnectionHelper() {
    return connectionHelper;
}

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

同意@Alex的说法,只是缺少的
文件不是applicationContext.xml文件,而是
vd servlet.xml
文件


将注册一个负责处理@Autowired、@Resource、@Inject注释的
组件扫描
执行相同的操作,但是
vd servlet.xml
中缺少
注释配置/组件扫描
,添加该选项应该可以解决问题。但具有讽刺意味的是,如果我跳过并仅使用component:scan,则不会检测到@RequestMapping。您需要
这是一个包含所有正确细节的好问题+1Spring在没有getter和setter的情况下可以很好地注入私有变量。组件扫描只会查找包中的所有bean,但由注释驱动的是对bean进行迭代,检测它们的@RequestMapping注释并创建映射@Inject应该像@Autowired一样工作-您提到过@Autowired适合您。您能确认您的类路径中有包含
javax.inject.inject
的jar吗?javaee-web-api-6.0.jar包含javax.inject.inject,它位于我的编译类路径上,但是我将它从部署中删除了,因为tomcat抱怨如果我将它放在运行时类路径
validateJarFile(webclient\web-INF\lib\javaee-web-api-6.0.jar)中-jar未加载。参见Servlet规范2.3,第9.7.2节。有问题的类:javax/servlet/servlet.class
因为它有自己的ServletAPI副本,所以我认为tomcat不支持@Inject then。这个问题有什么解决方案吗?你能试试这个jar吗?是的,拥有整个web api jar可能会与Tomcat冲突。
<?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>
@Inject
private ConnectionHelper connectionHelper;

public ConnectionHelper getConnectionHelper() {
    return connectionHelper;
}

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