【问题标题】:trying to use spring 3 but auto wired objects are null (on tomcat)尝试使用 spring 3,但自动连接的对象为空(在 tomcat 上)
【发布时间】:2012-11-06 22:17:20
【问题描述】:

我正在尝试使用 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>

我有一个 database.properties,它定义了几个用于获取 DataSource 对象的属性。 我还有一个使用@Configuration 的类,它采用数据库的属性。 此外,我还有另一个 @Configuration 类,它继承自第一个类,并添加了 JdbcTemplate 对象,每个方法都带有 @Bean 注释。

我创建了一个 UserRepository 接口和一个名为 UserRepositoryImpl 的实现 - 它有一个带注释的 @Autowired 成员 JdbcTemplate 和一个从 UserRepository 接口继承的方法 findById 的实现。

我有另一个名为 UserManager 的接口,它是 UserManagerImpl 的实现——它被注释为@Service,它有一个用@Autowired 注释的成员 UserRepository。 它正在实现一个 findById 的方法,只是尝试使用 UserRepository 从数据库中获取用户。

最后,我有一个使用成员的 servlet

@Autowired
private UserManager userManager;

问题是 userManager 为空。我认为春天没有启动,但我不知道为什么。我可能遗漏了一些重要的东西。

请帮助我,因为我无法进步。

提前致谢。

【问题讨论】:

    标签: spring tomcat7 autowired


    【解决方案1】:

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

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

    【讨论】:

    • 我不想使用 Spring MVC 控制器,因为我想将它添加到现有项目中。有没有办法使用呢?此外,我尝试在 spring 项目所在的主类中执行相同的行为,但我也得到了 null。我想我错过了一些关于如何加载 Spring 的内容。有任何想法吗?谢谢。
    【解决方案2】:

    您确定您的 UserManagerImpl 在指定为基本包的包com.myproject 中吗?

    【讨论】:

    • 您好,谢谢您的回复。 UserManagerImpl 类在包中,但不是直接的。它是 com.myproject.user.service.impl.UserManagerImpl。我该如何解决这个问题?
    • 将你的基础包设置为 com.myproject.*
    • 好的,我试试。难道我不需要通知 servlet 来加载所有的 spring 上下文吗? (包括所有豆类)。我在这里得到了一个答案,即 servlet 不是 spring 对象,因此我无法使用 @Autowire 将 spring bean 注入其中,我必须使用我目前无法使用的 Spring MVC。再次非常感谢你:)
    • 是的……Servlet 不是弹簧对象/bean。您也可以通过使用 Spring Controller 来实现与 Servlet 相同的功能。考虑使用它。
    • 好的,但是如果我仍然使用常规的servlet,我不能使用我这里描述的spring服务吗?我无法将弹簧服务连接到我的 servlet 中?谢谢。
    【解决方案3】:

    在您的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2012-06-18
      • 1970-01-01
      相关资源
      最近更新 更多