尝试使用Spring3,但自动连接对象为空(在tomcat上)

尝试使用Spring3,但自动连接对象为空(在tomcat上),spring,tomcat7,autowired,Spring,Tomcat7,Autowired,我正在尝试使用spring完成我的第一步,但我无法让它工作,我将感谢您的帮助 我使用的是tomcat,我已经在tomcat lib目录中添加了一个文件:jdbc-service.xml: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.spri

我正在尝试使用spring完成我的第一步,但我无法让它工作,我将感谢您的帮助

我使用的是tomcat,我已经在tomcat lib目录中添加了一个文件:jdbc-service.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<context:spring-configured />

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

<context:property-placeholder location="database.properties"/>

<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
问题是userManager为null。我认为春天没有开始,但我不知道为什么。我可能错过了一些重要的事情

请帮助我,因为我不能进步


提前谢谢

servlet不是春豆。它们由tomcat直接实例化,而不是由Spring实例化。因此,它们超出了Spring的范围,无法注入


考虑使用SpringMVC控制器而不是servlet。

servlet不是SpringBean。它们由tomcat直接实例化,而不是由Spring实例化。因此,它们超出了Spring的范围,无法注入


考虑使用Spring MVC控制器而不是servlet。

您确定您的UserManagerImpl位于指定为基本包的包
com.myproject
中吗

您确定您的UserManagerImpl位于指定为基本包的包
com.myproject
中吗

在web.xml中指定两个上下文侦听器,但确保第一个侦听器是Spring的:

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

<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>

org.springframework.web.context.ContextLoaderListener
com.sun.xml.ws.transport.http.servlet.WSServletContextListener

在web.xml中指定两个上下文侦听器,但确保第一个侦听器是Spring的:

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

<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>

org.springframework.web.context.ContextLoaderListener
com.sun.xml.ws.transport.http.servlet.WSServletContextListener

您好,谢谢您的回复。类UserManagerImpl在包中,但不直接在包中。它是com.myproject.user.service.impl.UserManagerImpl。如何解决此问题?请将基本包设置为com.myproject。*好的,我会尝试。我是否需要通知servlet加载所有spring上下文?(包括所有豆类)。我在这里得到了一个答案,servlet不是spring对象,因此我不能使用@Autowire将Springbean注入其中,我必须使用SpringMVC,这是我目前无法使用的。再次非常感谢:)没错……Servlet不是spring对象/bean。您也可以通过使用spring控制器实现与Servlet相同的功能。考虑使用它。好的,但是如果我仍然使用普通servlet,我不能使用我在这里描述的Spring服务吗?我不能将spring服务连接到我的servlet?谢谢。嗨,谢谢你的回复。类UserManagerImpl在包中,但不直接在包中。它是com.myproject.user.service.impl.UserManagerImpl。如何解决此问题?请将基本包设置为com.myproject。*好的,我会尝试。我是否需要通知servlet加载所有spring上下文?(包括所有豆类)。我在这里得到了一个答案,servlet不是spring对象,因此我不能使用@Autowire将Springbean注入其中,我必须使用SpringMVC,这是我目前无法使用的。再次非常感谢:)没错……Servlet不是spring对象/bean。您也可以通过使用spring控制器实现与Servlet相同的功能。考虑使用它。好的,但是如果我仍然使用普通servlet,我不能使用我在这里描述的Spring服务吗?我不能将spring服务连接到我的servlet?谢谢。我不想使用SpringMVC控制器,因为我想将其添加到现有项目中。没有办法使用它吗?此外,我尝试在spring项目所在的主类中执行相同的行为,但也得到null。我想我遗漏了一些关于如何加载弹簧的内容。有什么想法吗?谢谢。我不想使用SpringMVC控制器,因为我想将其添加到现有项目中。没有办法使用它吗?此外,我尝试在spring项目所在的主类中执行相同的行为,但也得到null。我想我遗漏了一些关于如何加载弹簧的内容。有什么想法吗?谢谢