Converting or Integrating a standalone spring appl

2019-08-29 10:54发布

问题:

Converting or Integrating a standalone spring application to a Spring MVC app.

I have a spring MVC application that has the following structure

myApp
 |- META-INF
 |- WEB-INF
        |-classes
            |-com
                |-controllers
                |-service
        |-lib
           |-UserLibrary.jar
                |-META-INF
                    |-applicationContext.xml
                    |-dbre.xml
                    |-ehcache.xml
                    |-DataSource.xml
                    |-Jpa.xml
                    |-SpringDataJpa.xml
        |-applicationContext.xml
        |-myApp-servlet.xml

The Spring MVC is loaded as per the usual process. i.e. The controllers are annotated with @Controllers, services are annotated with @Service etc.

The userLibrary.jar used to be a standalone spring application that had its own Spring/JPA configuration. It was converted so that it can be integrated into the MVC application. It has its own configuration files (about 7 in total), its own Hibernate/JPA entity managers etc.

For me to use it on the Spring MVC application i made a slight modification to the service class so that it loads the application context for the UserLibrary application when the WebApplicationContext has finished initialising.

public class MyAppService implements
        ApplicationListener<ContextRefreshedEvent> {

    protected UserLibraryService service;

        --
        -- several service methods etc
        --

    System.getProperty("username", "userA");
    System.getProperty("password", "userB");    

    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {        
        ApplicationContext context = 
         new ClassPathXmlApplicationContext("META-INF/userLibrary.xml");
        service = context.getBean(UserLibrary.class);
    }
}   

The above approach results in two containers, i.e. the WebApplicationContext for the MVC application and the ApplicationContext for the UserLibrary application. I suspect that the above is not the correct/best way to do it so im seeking advice as to what is the best approach. For example, can i use a single container as opposed to two?

Update:

Here is what i have tried. I imported the configuration file of the UserLibrary application from the applicationContext.xml of the MVC application.

<import resource="classpath:META-INF/applicationContext.xml" />

I then added the UserLibraryBean in the MVC application's applicationContext.xml file (Same file that contains the import statement.

<bean class="com.service.UserLibraryService"/>

A couple of questions:

  • Does the updated approach use only one container or does it still use two containers?
  • The UserLibraryService reads the following properties during its initialisation

    System.getProperty("username"); System.getProperty("password");

In my original example, i set the values for the above in the onApplicationEvent method and the UserLibray can read them.

These don't appear to be set anymore so i am getting an error from the UserLibrary application that the properties have not been set. I also tried setting them in the constructor for the Service class but they are still not been set.

Is there a way to set System properties as part of the bean definition in the xml file? Where can i set the properties so that they are available to the service object?

Thanks

回答1:

You can specify multiple configuration xml files in the context-param / contextConfigLocation parameter in the web application's web.xml

You can use the classpath: prefix on the file name to load it from the classpath



回答2:

You can read multiple configuration files like -

ApplicationContext context = 
         new ClassPathXmlApplicationContext(new String[] {"META-INF/userLibrary.xml",
              "META-INF/transaction.xml"});

Also you can use -

<import resource="META-INF/userLibrary.xml"/>