问题描述如下:
A 类是一个WS入口,调用B类的业务。 然后在B类里面注入service ,注入的service为空。
public class A{
private B b = new B();
public void test(){
b.method();
}
}
public class B{
@Resource
private IEsbForEmpBusinessService esbForEmpBusinessService;
在这个类里面的方法,调用esbForEmpBusinessService层的方法为空注入不进去。
}
@Service("esbForEmpBusinessServicesImpl")
public class EsbForEmpBusinessServicesImpl implements IEsbForEmpBusinessService {
@Resource
private IEsbForEmpBusinessDao esbBusinessDao ;
@Override
public EsbToHrEmpInfo queryHrEmpInfo(String emp_num) throws ServiceException {
EsbToHrEmpInfo ethe = esbBusinessDao.queryHrEmpInfo(emp_num);
return ethe ;
}
@Override
public void creatHrEmpInfo(EsbToHrEmpInfo hrempInfo) throws ServiceException {
esbBusinessDao.creatHrEmpInfo(hrempInfo);
}
@Override
public void updateHrEmpInfo(EsbToHrEmpInfo hrempInfo,String emp_num) throws ServiceException {
esbBusinessDao.updateHrEmpInfo(hrempInfo,emp_num);
}
@Override
public List<String> queryAreaCodeByNetCode(String netCode)
throws ServiceException {
return esbBusinessDao.queryAreaCodeByNetCode(netCode);
}
}
问题: class B 里面的service对象注入不进来为null ,求解!!!!
About Spring's annotation injection object problem-PHP Chinese website Q&A-About Spring's annotation injection object problem-PHP Chinese website Q&A
Look around and learn.
不能自己实例化bean,要通过sring获取。
@Service("esbForEmpBusinessServicesImpl")
public class EsbForEmpBusinessServiceImpl implements IEsbForEmpBusinessService {
@Resource
private IEsbForEmpBusinessDao esbBusinessDao;
public EsbToHrEmpInfo queryHrEmpInfo(String emp_num) {
System.out.println(emp_num);
EsbToHrEmpInfo ethe = new EsbToHrEmpInfo();
return ethe;
}
}
Java代码
@Service("serviceB")
public class B {
@Resource
private IEsbForEmpBusinessService esbForEmpBusinessService;
public void method() {
esbForEmpBusinessService.queryHrEmpInfo("111111");
}
}
@Service("serviceA")
public class A {
@Autowired
@Qualifier("serviceB")
private B b;
public void test(){
b.method();
}
public static void main(String[] args) {
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("test.xml");
A a = (A) context.getBean("serviceA");
System.out.println(a);
a.test();
}