超级IOC容器SuperContainer

news/2024/7/7 14:17:17

  在JavaEE乃至其它的java应用程序中,容器显得非常重要。web容器、applet容器、EJB容器等,可谓容器无处不在。
  从程序员的角度来说,IOC容器是一个非常好的东西,他能使得我们非常灵活的管理组件及依赖关系。可以毫不夸张地说,Spring就是靠着一套功能全面、灵活的IOC容器发家致富的。作为EasyJWeb特性系列的第五篇文章,我们来看看EasyJWeb中的IOC容器的特点。
  如果您看过EasyJWeb1.0的简介,就会发现他不仅仅是一个MVC。为了实现业务对象的很好管理,供核心MVC调用,在EasyJWeb中,我们提供了一个超级IOC容器。这个“超级”体现在,他属于容器中的容器,他可以容纳其它各种优秀的容器,并把这些各自独立的容器中的个体根据需要有机衔接配合起来,完成我们所需要做的事。
  当然,EasyJWeb自己也提供了一个简单的IOC容器,如果你不想或者没接触过其它的容器,那么你完全可以只用EasyJWeb的IOC容器,一样能写出非常优雅、松藕合的JavaEE应用。 
容器的使用非常简单,而且是完全可配置的,你可以根据需要把Spring容器、Guice容器、甚至EJB容器都纳入到EasyJWeb的SuperContainer中来。让他们在各自专业的领域里,为你工作。看代码:

 @Inject(name = " personDao " )
 
private  GenericDAO < Person >  dao;
public   void  setDao(GenericDAO < Person >  dao)  {
  
this.dao = dao;
 }

  在上面的CrudAction示例中,PersonAction需要一个DAO才能工作,在这里我们声明使用的是GenericDAO ,那么这个DAO在程序具体运行的过程中从哪儿来的,存放在哪儿?EasyJWeb都不关心这些事,你只需要通过@Inject这个标签,告诉我们要从超级IOC容器中启一个名叫personDao的对象。这样在程序运行的过程中,EasyJWeb会从超级IOC容器中查找名为personDao的对象,并注入到这个Action中,从而使得我们的Action能正常工作。
  实现依赖注入及控制反转,这不是什么奇特的事,每一个IOC框架都能实现这个功能。而不一样的是,EasyJWeb不但能从自己的IOC容器中得到依赖对象,而且也可以从任何其它的IOC容器中得到这个对象。比如我们可以在Spring容器中配置这个personDao,或者是使用Guice来管理这个personDao,乃至直接把这个personDao存放在另外一个服务器的EJB容器中。EasyJWeb会自动到这些地方去查找,并能把他们协调起来。
  有了超级IOC容器,系统管理员不再担心我们业务逻辑层组件的管理,老板也不需要担心在需要更换IOC容器所发生的移植成本。
  EasyJWeb的IOC容器同样实现了自动按名称、按类别等注入,另外还实现了不同生命周期范围的Bean管理。在默认的情况下,支持singleton、prototype、session、request等类型的Bean。
  另外,在EasyJWeb中,包括中央处理器RequestProcessor、验证器Validator、异常处理器ExceptionHandler在类的这些底层核心组件,都是通过EasyJWeb的超级容器来管理的。因此,你可以非常容易地根据自己的需要,更换EasyJWeb的一些部件。
  下面是在EasyJWeb超级容器中加入Spring容器的配置:

< bean  name ="springContainer"
   class
="org.springframework.web.context.support.XmlWebApplicationContext" >
   
< property  name ="configLocations" >     
    
< list >
     
< value > WEB-INF/classes/application.xml value>
    
list>
   
property>
  
bean>
  
<bean name="innerSpringContainer"
   class
="com.easyjf.container.impl.SpringContainer">
   
<property name="factory" ref="springContainer" />
  
bean>

 

可以在Spring容器中配置EasyJWeb的中央处理器,甚至可以配置事务等,如下面的Spring配置文件:
 

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop
="http://www.springframework.org/schema/aop"
 xmlns:tx
="http://www.springframework.org/schema/tx"
 xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
> 
 
<import resource="jpa-base.xml"/>
 
<import resource="service.xml"/>
 
<import resource="dao.xml"/>
 
<aop:config>
  
<aop:pointcut id="easyjwebProcessor"
   expression
="execution(* com.easyjf.web.RequestProcessor.process(..))" />
  
<aop:advisor advice-ref="txEasyjwebProcessorAdvice"
   pointcut-ref
="easyjwebProcessor" />
 
aop:config>
 
<tx:advice id="txEasyjwebProcessorAdvice"
  transaction-manager
="transactionManager">
  
<tx:attributes>
   
<tx:method name="*" propagation="REQUIRED" read-only="true" />
  
tx:attributes>
 
tx:advice>
 
<bean name="EasyJWeb-Processor" class="com.easyjf.web.core.DefaultRequestProcessor"/>
beans> 




http://www.niftyadmin.cn/n/3648086.html

相关文章

如何在Ubuntu 20.04上将Postfix安装和配置为仅发送SMTP服务器

介绍 (Introduction) Postfix is a mail transfer agent (MTA), an application used to send and receive email. It can be configured so that it can be used to send emails by local application only. This is useful in situations when you need to regularly send em…

想动就“动”起来

只要你愿意&#xff0c;严肃规矩的java也同样可以变得“动态”灵活起来。动有动的好处&#xff0c;静有静的好处。俗话说得好&#xff0c;“没有规矩不成方圆”&#xff0c;但“生命诚可贵&#xff0c;爱情价更高&#xff0c;若为自由故&#xff0c;两者皆可抛”。那么作为忙碌…

如何在Ubuntu 20.04上使用Nginx反向代理使用SSL配置Jenkins

介绍 (Introduction) By default, Jenkins comes with its own built-in Winstone web server listening on port 8080, which is convenient for getting started. It’s also a good idea, however, to secure Jenkins with SSL to protect passwords and sensitive data tra…

让代码与视图模板的分离

需要什么样的mvc在基于B/S的应用程序开发中&#xff0c;从基本的技术分工上来说就是两大块&#xff0c;一是软件显示界面&#xff0c;另一个是程序逻辑。在N年前的脚本语言时代&#xff0c;无论是asp、php还是jsp&#xff0c;我们基本是都是把这两者柔和在一起的。尽管我们想方…

灵活的视图切换及导向

在基于请求转发型的MVC框架中&#xff0c;给用户提供一个简单、灵活的视图切换及页面导向功能是非常关键的。作为EasyJWeb特性介绍系统的一篇文章&#xff0c;本篇主要介绍EasyJWeb中的页面切换及导向机制。EasyJWeb引入了纯模板的机制&#xff0c;通过其提供结构清晰的Module、…

【linux】

1、Linux系统介绍 不同于Windows&#xff0c;Linux是一个开源的操作系统。 Linux一切皆文件&#xff0c;对文件的操作有&#xff1a;创建文件、编辑文件、保存文件、关闭文件、重命名文件、删除文件、恢复文件。 1.1 目录结构 根目录 / bin:全称binary&#xff0c;含义是二进…

给网页添加背景图片html_如何使用HTML将背景图像添加到网页的顶部

给网页添加背景图片htmlHow To Build a Website With HTML 如何使用HTML构建网站This tutorial series will guide you through creating and further customizing this website using HTML, the standard markup language used to display documents in a web browser. No pri…

docker本地开发和测试_如何使用Docker和DDEV在本地计算机上开发Drupal 9网站

docker本地开发和测试The author selected the Diversity in Tech Fund to receive a donation as part of the Write for DOnations program. 作者选择了“技术多元化”基金来接受捐赠&#xff0c;这是Write for DOnations计划的一部分。 介绍 (Introduction) DDEV is an ope…