2

I am new to Java Spring, so I may have some difficulties explaining my configurations.

I have a Java Spring Web Application that contains a Servlet that needs to access a Bean. However, the issue is the Bean is located in another common project xml file that gets loaded whenever the application get's loaded.

Project C

common-context.xml

<bean id="common-bean" ...>

CommonLoader.class

protected AbstractApplicationContext mContext;

public void start(String[] pArgs) {
   mContext = new ClassPathXmlApplicationContext("common-context.xml");
}

In another Project A, I have a servlet that needs to access common-bean, however, it is unable to find the bean.

Project A

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  ApplicationContext mAppContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

  for (String name : mAppContext.getBeanDefinitionNames()) {
    System.out.println(name);
  }
}

However, nothing gets found even though I know for a fact that common-bean is running due to the logs. My guess is because they are running in separate ApplicationContexts, they cannot talk to each other even though they are in the same Java application.

Is there any way for them to communicate or pass the bean directly to the servlet?

Chen
  • 103
  • 1
  • 9
  • 1
    If you're *new* to Spring, you should probably not be messing with your own ApplicationContexts yourself. That is an *advanced* feature. Since you're new, is there any reason you didn't start with a Spring Boot application, which makes it o much easier to setup Spring? – Andreas Mar 27 '20 at 19:17
  • I consider myself _advanced-to-expert_ in Spring and have never needed to use multiple contexts (others' experience will differ). – chrylis -cautiouslyoptimistic- Mar 27 '20 at 19:23
  • @Andreas Yeah, I am relatively new. Unfortunately, I wasn't the one who designed this application, and even more unfortunate, the person who did I can no longer contact. I am trying to add additional features to this, so any help would be appreciated. – Chen Mar 27 '20 at 19:43
  • @chrylis-onstrike- It's a little weird, but Project C is used commonly with other applications, while Project A is application specific. – Chen Mar 27 '20 at 19:46

0 Answers0