【发布时间】:2016-06-02 13:47:03
【问题描述】:
我有一个名为 ABCService 的服务接口及其实现名为 ABCServiceImpl。
ABCService.class
package com.abc.service.ABCService;
public interface ABCService{
//interface methods
}
ABCServiceImpl.class
package com.abc.service.ABCServiceImpl;
@Service
public class ABCServiceImpl implements ABCService{
// implemented methods
}
XYZ.class
package com.abc.util.XYZ;
public class XYZ{
@Autowired
ABCService abcService;
//methods
}
应用程序上下文.xml
<context: annotation-config>
<context: component-scan base-package="com.abc.service, com.abc.util"/>
但是当我尝试使用类 XYZ 中的自动装配 ABCService 来访问接口 ABCService 中的方法时,我得到一个空指针异常。 然后,我从 ABCServiceImpl 中删除了 @Service 注释,并在 application-context.xml 中添加了实现文件作为 bean,并为 XYZ 类创建了一个 bean,并将 ABCServiceImpl 的 bean 引用到 XYZ 类的 bean;它解决了这个问题
应用程序上下文.xml
<bean id="abcService" class="com.abc.service.ABCServiceImpl" />
<bean id="xyz" class="com.abc.util.XYZ" >
<property name="abcService" ref="abcService"></property>
</bean>
但我想使用 @Service 注释本身而不在 application-context.xml 中显式定义 bean。我该怎么做?
【问题讨论】:
-
是否要求 XYZ 类在包扫描服务之外?
-
@Adam :是的,XYZ 是一个实用程序类,我不想将该类放入服务包中。因此 XYZ 类将在 com.abc.util 包中
标签: java spring-mvc javabeans autowired