【发布时间】: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 为空。我认为春天没有启动,但我不知道为什么。我可能遗漏了一些重要的东西。
请帮助我,因为我无法进步。
提前致谢。
【问题讨论】: