Spring:如何在 Webapp 和 Standalone 程序中获取应用程序上下文 [英] Spring: how to get hold of Application context in Webapp and Standalone program

查看:26
本文介绍了Spring:如何在 Webapp 和 Standalone 程序中获取应用程序上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring 框架的新手.我们想在一个web应用中引入它(3.1),目前在web层使用struts,在业务层使用service Facades和业务对象,在持久层使用自己编写的JDBC DAO(所有这些都是紧密耦合的!)

我创建了几个 .xml 配置,一个用于 servlet 配置,仅扫描 com.mydomain.web 包.另一个在服务层 appContext-service.xml 扫描 com.mydomain.bs 和 .bo 包,另一个用于 DAO 层 appContext-persistence.xml 扫描 .dao 包.

我们有四个具有适当项目依赖项的 Eclipse 项目:Web、Business、Common(包含域对象、DTO、Exceptions 等)、DataAccess.

我想在可能的情况下使用注释,并且已经使用 JDBC 模板创建了一个 MVC 控制器、一个带有接口的新服务和一个带有接口的新 dao,这些都很好用.

现在我的问题是:

  1. 我们不能一次重写所有代码,我们在这里讨论的是更大的代码库.但是,当新创建的服务也需要来自(尚未)Spring 感知的服务和业务对象时,我该怎么办?它们不是 bean 或不是由 Spring 创建的.我将如何获得我的服务 bean?

  2. 我们有几个独立的应用程序用于批处理、定期清理文件系统和数据库表等.它们由 cron (UNIX cron) 触发,因此有自己的 JVM.鉴于不同的 .xml 配置,我在这里如何最好地使用 Spring 服务?

  3. 我的设置是否有意义?

感谢您的任何见解.

解决方案

  1. 让 spring 处理所有 bean 的生命周期是很常见的,否则它可能会变得有点棘手.不是 spring bean 的对象有望在某处初始化.使该初始化程序成为 spring bean 并使其具有应用程序上下文感知

     公共类 SpringContextHolder 实现 ApplicationContextAware {私有静态 ApplicationContext applicationContext = null;公共静态 ApplicationContext getApplicationContext() {返回应用上下文;}public void setApplicationContext(ApplicationContext applicationContext) 抛出 BeansException {this.applicationContext = applicationContext;}公共无效初始化(){ServiceBean1 srv1 = (ServiceBean1)applicationContext.getBean("serviceBean1");myNonSpringObject.setService1(srv1);//或者其他的东西}}

  2. 设置独立的 spring 应用程序非常简单.只需创建一个 Spring XML 并连接您的 bean(通过扫描/注释或 XML).不建议在 main 方法中执行此操作,但您可以轻松找出如何在独立应用程序中进行此设置.请记住,您的应用程序本身不应该做太多的生命周期逻辑,而应该让 Spring 来做.

    public class StandaloneSpringApp{公共静态无效主(字符串 [] args){ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");SomeBeanType bean = (SomeBeanType)ctx.getBean("SomeBeanName");bean.doProcessing();//管他呢}}

  3. 您的设置非常合理,尽管我无法想象您的整个范围,但您的方法是大型模块化 Spring 应用程序的良好起点.

I'm new to the Spring Framework. We want to introduce it (3.1) in a web application, currently using struts in the web layer, service facades and business objects in the business layer and self-written JDBC DAOs in the persistence layer (all of it closely coupled!)

I created several .xml configurations, one for the servlet config, scanning the com.mydomain.web package only. Another one on the service layer appContext-service.xml which scans com.mydomain.bs and .bo packages and one for the DAO layer appContext-persistence.xml scanning the .dao package.

We have four Eclipse projects with appropriate project dependencies: Web, Business, Common (contains domain objects, DTOs, Exceptions, etc), DataAccess.

I want to use annotations where possible and already created a MVC controller, a new service with interface and a new dao with interface, using the JDBC template, which all works great.

Now my questions are:

  1. We can't re-write all the code at once, we're talking about a larger code base here. But what do I do, when the newly created service is also needed from services and business objects that are not (yet) Spring aware? They're not beans or not being created by Spring. How would I get hold of my service bean?

  2. We have several standalone applications for batch processing, cleaning up the file system and database tables periodically, etc. They're triggered by cron (UNIX cron) and therefore have their own JVM. How would I best use Spring services here, given the different .xml configurations?

  3. Does my setup make any sense at all?

Thanks for any insight.

解决方案

  1. It's very common that one let spring handle the lifecycle of all the beans, otherwise it might get a bit tricky. The objects that are not spring beans are hopefully initialized somewhere. Make that initializer a spring bean and make it application context aware

     public class SpringContextHolder implements ApplicationContextAware {
    
       private static ApplicationContext applicationContext = null;
    
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
             this.applicationContext = applicationContext;
        }
        public void init(){
    
            ServiceBean1 srv1 = (ServiceBean1)applicationContext.getBean("serviceBean1");
    
            myNonSpringObject.setService1(srv1); // Or something
        }
    }
    

  2. Setting up a standalone spring app is very easy. Just create a Spring XML and wire your beans (either via scanning/annotations or XML). It is not really recommended to do this in the main method, but you could easily figure out how to get this setup in your standalone application. Keep in mind that your application itself should not really do much lifecycle logic but let Spring do that.

    public class StandaloneSpringApp{
      public static void main(String[] args){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    
        SomeBeanType bean = (SomeBeanType)ctx.getBean("SomeBeanName");
        bean.doProcessing(); // or whatever
      }
    
    }
    

  3. Your setup makes perfect sense, even though I cannot visualize your entire scope, your approach is a good starting point for a large modularized spring application.

这篇关于Spring:如何在 Webapp 和 Standalone 程序中获取应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆