注入
@Service
public class FgStoServiceImpl implements FgStoService {
@Resource
private PrintInvoiceService printInvoiceService;
public TransResult printVNPT(OdsProStoHvDetail condition) {
PublishServiceSoap publishServiceSoap = printInvoiceService.getPublishServiceSoap();
...
}
定义的普通类(这种普通类有20多个,是不是只能是都加上component注解?)
package com.haier.cosmo.wms.cp.out.service.service.printInvoiceService;
import java.net.MalformedURLException;
import java.net.URL;
public class PrintInvoiceService {
private PublishServiceSoap publishServiceSoap;
public PublishServiceSoap getPublishServiceSoap() {
String url = "https://aquavn-tt78admindemo.vnpt-invoice.com.vn/PublishService.asmx";
if (publishServiceSoap != null) {
return publishServiceSoap;
}
try {
URL uurl = new URL(url);//URL在配置文件中动态配置
PublishService mobileCodeWS = new PublishService(uurl);
publishServiceSoap = mobileCodeWS.getPublishServiceSoap();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return publishServiceSoap;
}
}
报错
Description:
A component required a bean of type 'com.haier.cosmo.wms.cp.out.service.service.printInvoiceService.PrintInvoiceService' that could not be found.
Action:
Consider defining a bean of type 'com.haier.cosmo.wms.cp.out.service.service.printInvoiceService.PrintInvoiceService' in your configuration.
不一定但一般是这样的。如果你需要让Spring来管理你定义的类,你就需要首先把这些bean放进Spring的容器里交给Spring来管理,诸如@Component/@Controller/@Service/@Repository其实本质上都是一样的,就是把这个类的对象或者实现这个接口的类的对象交给Spring容器来管理。
一个可能不太恰当的比喻:你可以理解为没加注解的类是水龙头里的水,每次你要用水都得自己打开水龙头,加上这些注解的类是被装进水瓶里的水,你要用的时候只要从水瓶里倒水就可以了,前提是你得往这个水瓶里装水。
另外我注意到你的这个普通类里面:
如果你要给这个普通类加上@Component注解,这里的写法也就相应地要变化,PublishService也要加上@Component/@Service,再通过@Resource/@Autowired注入这个已经加上@Component的普通类里(现在已经是交给Spring管理的类了),如果你的类里还有这种new的对象,就要一路改下去直到没有这种情况为止。
当然了,你如果非要在一个已经交给Spring管理的类里new对象,然后还要通过Spring容器拿到这个bean,就不能采用注入的方式了,不然会报空指针异常,这里我提供一个工具类:
调用它的getBean方法就能拿到你那个里面有new对象的bean了:
其实本质上就是直接拿到容器对象,然后调用容器的getBean方法“手动”来拿到我们想要的bean罢了,这种方式适用于在一些不能被Spring管理的类里非得用到Spring管理的类的对象时。
希望我的回答对你有所帮助。