简体   繁体   中英

Can a service layer object in a Java Spring project call in a bean from the Application-Context.xml?

Can a service layer object in a Java Spring project call in a bean from the Application-Context.xml?

public class MemberInquiryServiceImpl implements MemberInquiryService {

    private String BASE_URI;
    private String user;
    private String password;

    HttpResponse response;

    public MemberInquiryServiceImpl() {

        ApplicationContext ctx = new ClassPathXmlApplicationContext();

        RequestTrackerConfig rtc = (RequestTrackerConfig) ctx
                .getBean("requestTrackerConfig");

        BASE_URI = rtc.getUrl();
        user = rtc.getUser();
        password = rtc.getPassword();

    }

Can a service layer object in a Java Spring project call in a bean from the Application-Context.xml?

Yes, but in your code you are creating a new ApplicationContext:

public MemberInquiryServiceImpl() {

        ApplicationContext ctx = new ClassPathXmlApplicationContext();

which I doubt is what you actually meant to do (I'm assuming your MemberInquiryServiceImpl is created by Spring).

Instead, you could for example autowire (using annotations or xml-config) the RequestTrackerConfig to MemberInquiryServiceImpl , or make MemberInquiryServiceImpl implement ApplicationContextAware -interface, and use the injected ApplicationContext to get the "requestTrackerConfig" -bean.

为什么不使用依赖注入来注入呢?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM