【问题标题】:Converting or Integrating a standalone spring application to a Spring MVC app将独立的 Spring 应用程序转换或集成到 Spring MVC 应用程序
【发布时间】:2012-12-20 11:41:51
【问题描述】:

将独立的 Spring 应用程序转换或集成到 Spring MVC 应用程序。

我有一个具有以下结构的 Spring MVC 应用程序

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

Spring MVC 按照通常的流程加载。即控制器用@Controllers注释,服务用@Service等注释。

userLibrary.jar 曾经是一个独立的 Spring 应用程序,具有自己的 Spring/JPA 配置。它进行了转换,以便可以集成到 MVC 应用程序中。它有自己的配置文件(总共大约 7 个)、自己的 Hibernate/JPA 实体管理器等。

为了在 Spring MVC 应用程序上使用它,我对服务类进行了轻微修改,以便在 WebApplicationContext 完成初始化时加载 UserLibrary 应用程序的应用程序上下文。

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);
    }
}   

上述方法会产生两个容器,即用于 MVC 应用程序的 WebApplicationContext 和用于 UserLibrary 应用程序的 ApplicationContext。 我怀疑上述方法不是正确/最好的方法,所以我正在寻求关于什么是最好方法的建议。例如,我可以使用单个容器而不是两个容器吗?

更新:

这是我尝试过的。我从MVC应用的applicationContext.xml导入了UserLibrary应用的配置文件。

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

然后我在 MVC 应用程序的 applicationContext.xml 文件中添加了 UserLibraryBean(包含导入语句的同一文件。

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

几个问题:

  • 更新后的方法是只使用一个容器还是仍然使用两个容器?
  • UserLibraryService 在其初始化期间读取以下属性

    System.getProperty("用户名"); System.getProperty("密码");

在我的原始示例中,我在onApplicationEvent 方法中设置了上述值,UserLibray 可以读取它们。

这些似乎不再设置,所以我从UserLibrary 应用程序中收到一个错误,即属性尚未设置。我还尝试在 Service 类的构造函数中设置它们,但仍未设置它们。

有没有办法将系统属性设置为 xml 文件中 bean 定义的一部分?我在哪里可以设置属性以便服务对象可以使用它们?

谢谢

【问题讨论】:

    标签: java spring servlets spring-mvc


    【解决方案1】:

    您可以在 web 应用程序的 web.xml 中的 context-param / contextConfigLocation 参数中指定多个配置 xml 文件

    您可以在文件名上使用classpath: 前缀从类路径加载它

    【讨论】:

    • 谢谢。我尝试了类似的方法(请参阅原始帖子中的更新),但我正在努力初始化系统属性。
    【解决方案2】:

    您可以读取多个配置文件,例如 -

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

    你也可以使用 -

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

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 2018-09-24
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多