我遇到弹簧自动装配组件的问题。我尝试将bean(UserRepository)注入java类Reader。当我使用autowire注释时,对象总是为空。我尝试添加到spring-config.xml bean'reader'whith属性'userRepository'但这不起作用。缺少什么?
弹簧-config.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="reader" />
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.sqlite.JDBC"/>
<property name="url" value="jdbc:sqlite:C:/Users/Piotr/Downloads/projekt na projekt/IntelliDOM/IntelliDOM.sqlite"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userRepository" class="repository.UserRepositoryHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
Reader.java:
@Service
@Path("/")
public class Reader {
@Autowired
private UserRepository userRepository;
@GET
@Path("/test")
public void test(){
System.out.println("test");
userRepository.findAll(); //this is allways null
}
@POST
@Path("/dom2")
public Response read(String nonParsedWords){
Words parsedWords = WordParser.parseWords(nonParsedWords);
for(String word : parsedWords.getWords()){
System.out.println(word);
}
return Response.status(200).entity(nonParsedWords).build();
}
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
申请环境有问题吗?为什么这个豆子不能注射? 春季版:2.5.6
编辑: hibernate.cfg.xml中:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:IntelliDOM.sqlite</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="entity.User"/>
</session-factory>
</hibernate-configuration>