春天注射在不同的服务类不能正常工作
我有类
@Service("registrationService")
@Transactional
public class RegistrationService {
@Resource(name="registrationDAO")
public RegistrationDAO registrationDAO;
在控制器我可以访问registrationService和registrationDAO没有问题。
In the Controller i can access registrationService and registrationDAO with no problem.
我有另一个类
@Service("securityService")
public class SecurityService implements UserDetailsService {
protected static Logger logger = Logger.getLogger("service");
@Resource(name="registrationDAO")
public RegistrationDAO registrationDAO;
public String test(){
logger.debug(registrationDAO.findUserByID(1) );
return "Testing";
}
现在如果我叫测试功能的控制器则提供了有关registrationDAO空指针异常
Now if i call test function in controller then it gives null pointer exception on registrationDAO
您所有的 @Service
, @Repository
, @Controller
, @Component
(等)的注解类必须是自动装配工作的春季管理。确保他们被弹簧类路径扫描回升:
All your @Service
, @Repository
, @Controller
, @Component
(etc.) annotated class must be spring-managed for autowiring to work. Make sure they are picked up by spring classpath scanning:
<context:component-scan base-package="com.company" />
在某些情况下, @Autowire
,它按类型自动装配做,可能是有用的,以避免名称
你的说法'再与 @Resource
供应。
In some cases @Autowire
, which does autowiring by type, can be useful to avoid the name
argument you're supplying with the @Resource
.