在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
        
        
          "
        
        
          )
        
          @Inject(name
        
        
          =
        
        
          "
        
        
          personDao
        
        
          "
        
        
          )
           private
        
        
          GenericDAO
        
        
          
            Person
          
          
            >
          
          
            dao;
        
        
          private
        
        
          GenericDAO
        
        
          
            Person
          
          
            >
          
          
            dao;
             public
          
          
          
          
            void
          
          
            setDao(GenericDAO
          
          
            
              Person
            
            
              >
            
            
              dao)
            
            
            
              
                {
            
          
          
            public
          
          
          
          
            void
          
          
            setDao(GenericDAO
          
          
            
              Person
            
            
              >
            
            
              dao)
            
            
            
              
                {
                 this
              
              
                .dao
              
              
                =
              
              
                dao;
              
              
                this
              
              
                .dao
              
              
                =
              
              
                dao;
                 }
                }
              
            
          
        
      在上面的CrudAction示例中,PersonAction需要一个DAO才能工作,在这里我们声明使用的是GenericDAO<person>,那么这个DAO在程序具体运行的过程中从哪儿来的,存放在哪儿?EasyJWeb都不关心这些事,你只需要通过@Inject这个标签,告诉我们要从超级IOC容器中启一个名叫personDao的对象。这样在程序运行的过程中,EasyJWeb会从超级IOC容器中查找名为personDao的对象,并注入到这个Action中,从而使得我们的Action能正常工作。<br> 实现依赖注入及控制反转,这不是什么奇特的事,每一个IOC框架都能实现这个功能。而不一样的是,EasyJWeb不但能从自己的IOC容器中得到依赖对象,而且也可以从任何其它的IOC容器中得到这个对象。比如我们可以在Spring容器中配置这个personDao,或者是使用Guice来管理这个personDao,乃至直接把这个personDao存放在另外一个服务器的EJB容器中。EasyJWeb会自动到这些地方去查找,并能把他们协调起来。<br> 有了超级IOC容器,系统管理员不再担心我们业务逻辑层组件的管理,老板也不需要担心在需要更换IOC容器所发生的移植成本。<br> EasyJWeb的IOC容器同样实现了自动按名称、按类别等注入,另外还实现了不同生命周期范围的Bean管理。在默认的情况下,支持singleton、prototype、session、request等类型的Bean。<br> 另外,在EasyJWeb中,包括中央处理器RequestProcessor、验证器Validator、异常处理器ExceptionHandler在类的这些底层核心组件,都是通过EasyJWeb的超级容器来管理的。因此,你可以非常容易地根据自己的需要,更换EasyJWeb的一些部件。<br> 下面是在EasyJWeb超级容器中加入Spring容器的配置:<br></person>
 bean
          
          
            name
          
          
            ="springContainer"
        
          
            bean
          
          
            name
          
          
            ="springContainer"
          
          
             class
          
          
            ="org.springframework.web.context.support.XmlWebApplicationContext"
          
          
            >
            class
          
          
            ="org.springframework.web.context.support.XmlWebApplicationContext"
          
          
            >
          
          
             property
            
            
              name
            
            
              ="configLocations"
            
            
              >
          
          
            
              property
            
            
              name
            
            
              ="configLocations"
            
            
              >
            
            
               list
              
              
                >
            
            
              
                list
              
              
                >
              
              
                 value
                
                
                  >
                
                
                  WEB-INF/classes/application.xml
                
                
                
                
                  value
                
                
                  >
              
              
                
                  value
                
                
                  >
                
                
                  WEB-INF/classes/application.xml
                
                
                
                
                  value
                
                
                  >
                
                
                   list
                
                
                  >
                
                
                
                
                  list
                
                
                  >
                
                
                   property
                
                
                  >
                
                
                
                
                  property
                
                
                  >
                
                
                   bean
                
                
                  >
                
                
                
                
                  bean
                
                
                  >
                
                
                   bean
                  
                  
                    name
                  
                  
                    ="innerSpringContainer"
                
                
                  
                    bean
                  
                  
                    name
                  
                  
                    ="innerSpringContainer"
                  
                  
                     class
                  
                  
                    ="com.easyjf.container.impl.SpringContainer"
                  
                  
                    >
                    class
                  
                  
                    ="com.easyjf.container.impl.SpringContainer"
                  
                  
                    >
                  
                  
                     property
                    
                    
                      name
                    
                    
                      ="factory"
                    
                    
                      ref
                    
                    
                      ="springContainer"
                    
                    
                    
                    
                      />
                  
                  
                    
                      property
                    
                    
                      name
                    
                    
                      ="factory"
                    
                    
                      ref
                    
                    
                      ="springContainer"
                    
                    
                    
                    
                      />
                    
                    
                       bean
                    
                    
                      >
                    
                    
                    
                    
                      bean
                    
                    
                      >
                    
                  
                
              
            
          
        
      
      可以在Spring容器中配置EasyJWeb的中央处理器,甚至可以配置事务等,如下面的Spring配置文件:
      
      
    
 xmlversion="1.0"encoding="UTF-8"
        
        
          ?>
        
        
        
          xmlversion="1.0"encoding="UTF-8"
        
        
          ?>
        
        
           beans
          
          
            xmlns
          
          
            ="http://www.springframework.org/schema/beans"
        
        
          
            beans
          
          
            xmlns
          
          
            ="http://www.springframework.org/schema/beans"
          
          
             xmlns:xsi
          
          
            ="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsi
          
          
            ="http://www.w3.org/2001/XMLSchema-instance"
          
          
             xmlns:aop
          
          
            ="http://www.springframework.org/schema/aop"
            xmlns:aop
          
          
            ="http://www.springframework.org/schema/aop"
          
          
             xmlns:tx
          
          
            ="http://www.springframework.org/schema/tx"
            xmlns:tx
          
          
            ="http://www.springframework.org/schema/tx"
          
          
             xsi:schemaLocation
          
          
            ="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
            xsi:schemaLocation
          
          
            ="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd
             http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"
          
          
            >
            http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"
          
          
            >
          
          
             import
            
            
              resource
            
            
              ="jpa-base.xml"
            
            
              />
          
          
            
              import
            
            
              resource
            
            
              ="jpa-base.xml"
            
            
              />
            
            
               import
              
              
                resource
              
              
                ="service.xml"
              
              
                />
            
            
              
                import
              
              
                resource
              
              
                ="service.xml"
              
              
                />
              
              
                 import
                
                
                  resource
                
                
                  ="dao.xml"
                
                
                  />
              
              
                
                  import
                
                
                  resource
                
                
                  ="dao.xml"
                
                
                  />
                
                
                   aop:config
                  
                  
                    >
                
                
                  
                    aop:config
                  
                  
                    >
                  
                  
                     aop:pointcut
                    
                    
                      id
                    
                    
                      ="easyjwebProcessor"
                  
                  
                    
                      aop:pointcut
                    
                    
                      id
                    
                    
                      ="easyjwebProcessor"
                    
                    
                       expression
                    
                    
                      ="execution(*com.easyjf.web.RequestProcessor.process(..))"
                    
                    
                    
                    
                      />
                      expression
                    
                    
                      ="execution(*com.easyjf.web.RequestProcessor.process(..))"
                    
                    
                    
                    
                      />
                    
                    
                       aop:advisor
                      
                      
                        advice-ref
                      
                      
                        ="txEasyjwebProcessorAdvice"
                    
                    
                      
                        aop:advisor
                      
                      
                        advice-ref
                      
                      
                        ="txEasyjwebProcessorAdvice"
                      
                      
                         pointcut-ref
                      
                      
                        ="easyjwebProcessor"
                      
                      
                      
                      
                        />
                        pointcut-ref
                      
                      
                        ="easyjwebProcessor"
                      
                      
                      
                      
                        />
                      
                      
                         aop:config
                      
                      
                        >
                      
                      
                      
                      
                        aop:config
                      
                      
                        >
                      
                      
                         tx:advice
                        
                        
                          id
                        
                        
                          ="txEasyjwebProcessorAdvice"
                      
                      
                        
                          tx:advice
                        
                        
                          id
                        
                        
                          ="txEasyjwebProcessorAdvice"
                        
                        
                           transaction-manager
                        
                        
                          ="transactionManager"
                        
                        
                          >
                          transaction-manager
                        
                        
                          ="transactionManager"
                        
                        
                          >
                        
                        
                           tx:attributes
                          
                          
                            >
                        
                        
                          
                            tx:attributes
                          
                          
                            >
                          
                          
                             tx:method
                            
                            
                              name
                            
                            
                              ="*"
                            
                            
                              propagation
                            
                            
                              ="REQUIRED"
                            
                            
                              read-only
                            
                            
                              ="true"
                            
                            
                            
                            
                              />
                          
                          
                            
                              tx:method
                            
                            
                              name
                            
                            
                              ="*"
                            
                            
                              propagation
                            
                            
                              ="REQUIRED"
                            
                            
                              read-only
                            
                            
                              ="true"
                            
                            
                            
                            
                              />
                            
                            
                               tx:attributes
                            
                            
                              >
                            
                            
                            
                            
                              tx:attributes
                            
                            
                              >
                            
                            
                               tx:advice
                            
                            
                              >
                            
                            
                            
                            
                              tx:advice
                            
                            
                              >
                            
                            
                               bean
                              
                              
                                name
                              
                              
                                ="EasyJWeb-Processor"
                              
                              
                                class
                              
                              
                                ="com.easyjf.web.core.DefaultRequestProcessor"
                              
                              
                                />
                            
                            
                              
                                bean
                              
                              
                                name
                              
                              
                                ="EasyJWeb-Processor"
                              
                              
                                class
                              
                              
                                ="com.easyjf.web.core.DefaultRequestProcessor"
                              
                              
                                />
                              
                              
                                 beans
                              
                              
                                >
                              
                              
                              
                              
                                beans
                              
                              
                                >
                              
                              
                              
                            
                          
                        
                      
                    
                  
                
              
            
          
        
      Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1745130


 
           
					 
					