春天自动装配不起作用 [英] spring autowiring not working
问题描述
我在调度程序类中使用带有Quartz的spring 3.0。我已经创建了应用程序上下文
Hi, I am using spring 3.0 with Quartz in a scheduler class. I have created the application context by
private static final ClassPathXmlApplicationContext applicationContext;
static {
applicationContext = new
ClassPathXmlApplicationContext("config/applicationContext.xml");
}
问题是没有 @Autowired
bean实际上是自动连接的,所以我必须手动设置这样的依赖:
The problem is that none of the @Autowired
beans actually get auto-wired, so I have to manually set dependencies like this:
<bean class="com.spr.service.RegistrationServiceImpl" id="registrationService">
<property name="userService" ref="userService" />
</bean>
我在哪里使用 @Autowired
:
public class RegistrationService {
@AutoWired private UserService userService;
// setter for userService;
}
public class UserService{
// methods
}
我还确保在我的Spring配置中启用注释配置:
I also made sure to enable the annotation configuration in my Spring config:
<context:annotation-config/>
<bean id="registrationSevice" class="RegistrationService"/>
<bean id="userService" class="UserService"/>
为什么 @Autowired
不适合我?
推荐答案
您尚未提供UserService类源代码,因此我无法确定您的问题。看起来UserService类缺少像@Component或@Service这样的'原型'注释。您还必须使用以下配置配置Spring类路径扫描:
You haven't provided the UserService class source code so I can't be sure about your problem. Looks like the UserService class is missing a 'stereotype' annotation like @Component or @Service. You also have to configure the Spring classpath scanning using the following configuration:
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Add your classes base package here -->
<context:component-scan base-package="your.package"/>
</beans>
您的bean必须包含一个Spring构造型注释,例如:
Your beans must include one of the Spring stereotype annotations like:
package your.package;
@Service
public class UserService{
}
这篇关于春天自动装配不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!