春季:如何让应用程序上下文中举行的webapp和独立程序 [英] Spring: how to get hold of Application context in Webapp and Standalone program

查看:113
本文介绍了春季:如何让应用程序上下文中举行的webapp和独立程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Spring框架。我们希望在Web应用程序介绍它(3.1),目前使用在网络层和持久层的支柱,服务外墙和业务层业务对象自写JDBC的DAO(这一切紧密耦合!)

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

我们有合适的项目依赖4 Eclipse项目:网络,业务,普通(包含域对象,DTO的,异常等),数据访问

我要尽可能使用注释和已经创建了一个MVC控制器,具有界面新的服务和接口的新道,使用JDBC模板,而这一切的伟大工程。

现在我的问题是:


  1. 我们无法重新编写所有code一次,我们在这里谈论的是一个更大的code基地。但是,我该怎么办,也当从服务和不属于业务对象所需要的新创建的服务(还)春知道?他们不是豆类或不被春创建。我将如何得到我的服务bean的持有?


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


  3. 请问我设置任何意义可言?


感谢您的任何见解。


解决方案

  1. 这是很常见的,一个让Spring处理所有的bean的生命周期,否则可能会有点棘手。未的Spring Bean对象初始化希望的地方。作出这样的初始化一个Spring bean,并使其认识到应用程序上下文

     公共类SpringContextHolder实现了ApplicationContextAware {   私有静态的ApplicationContext的applicationContext = NULL;    公共静态的ApplicationContext getApplicationContext(){
            返回的applicationContext;
        }
        公共无效setApplicationContext(ApplicationContext中的applicationContext)抛出BeansException {
             this.applicationContext =的applicationContext;
        }
        公共无效的init(){        ServiceBean1 SRV1 =(ServiceBean1)applicationContext.getBean(serviceBean1);        myNonSpringObject.setService1(服务器1); // 或者其他的东西
        }
    }


  2. 建立一个独立的应用程序春天是很容易的。只要创建一个Spring XML和电汇你的bean(无论是通过扫描/注释或XML)。它是不是真的建议这样做的主要方法,但你可以很容易地找出如何得到这个安装在独立的应用程序。请记住,你的应用程序本身不应该真正做太多的生命周期逻辑,让Spring这样做。

     公共类StandaloneSpringApp {
      公共静态无效的主要(字串[] args){
        ClassPathXmlApplicationContext的CTX =新的ClassPathXmlApplicationContext(的applicationContext.xml);    SomeBeanType豆=(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.

这篇关于春季:如何让应用程序上下文中举行的webapp和独立程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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