SpringX: use Spring without biz bean config

how-to
Oct 22, 20093 mins

  SpringX gives an easy and agile method to use Spring framework. It provides zero-bean config mechanism eliminating hundreds and thousands of business bean config in JEE dev. SpringX also provides a simpler approach to use Spring AOP with biz bean. The latest version is SpringX 1.2.2.

Source&Download on google code

Framework Aims:

* Eliminate XML config for business bean. * Eliminate Java annotation for business bean.(Compare with Spring ‘component-scan’ feature) * Easier than

<context:component-scan/>

, because of no annotation need. * Integrate legacy application written in Java without any modification.

Feature List:

* . Dinamyical Bean Register and autowire; (v1.0.0) * . Strong type bean creation; * . Compatible with spring standard usage,id naming; * . Interface Injection, wildcard surrport. (v1.1.1) * . Supporting delegate to existing applicationContext. (v1.2.0) * . Much simpler AutoProxyCreator? config bean for AOP.

1. SpringX Code sample – autowire and strong type getting bean.

given a business bean OrderService,it’s dependent on OrderDAO,UserService?.

  package org.bamboo.springx.test.demo.service;
     public class OrderService {
            private UserService userService;
            private OrderDAO orderDAO;
            public void setOrderDAO(OrderDAO orderDAO) {
                    this.orderDAO = orderDAO;
            }

            public void setUserService(UserService userService) {
                    this.userService = userService;
            }
          //... other code
        }

“orderDAO”,”userService” is autowired to “orderService” without need to config them.


           import org.bamboo.springx.*;
           //instead of weak type get bean:  context.getBean("orderService");
           
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml" );
            BeanFactory beanFactory = BeanFactory.wrapApplicationContext( applicationContext );

           OrderService orderService = (OrderService) beanFactory.createBean(OrderService.class);
           orderService.createOrder("AA001");
           orderService.deleteOrder("AA002"); 

—–

//prototype demo:
   //instead of weak type get bean:  context.getBean("orderService");
   OrderService orderService = (OrderService) beanFactory.createBean(OrderService.class, false); 

2. SpringX config. – wildcard config for all biz beans.

But how does SpringX autowire orderDAO,userService to OrderService? The key is DynamicAutoWireInjector bean and its ‘autowireFilters’ property. It uses a wildcard string to indicate which beans need to be auto injected. applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
        <!-- Dynamic Dependency Autowire Injector.
                autowireFilters is use to set which class can't be injected as dependent bean. Regex also surrported. -->
        <bean id="org.bamboo.springx.DynamicAutoWireInjector" class="org.bamboo.springx.DynamicAutoWireInjector">
                <property name="autowireFilters">
                        <list>
                                <value>org.bamboo.springx.test.*.*Service</value> <!-- injectable bean's class must be business *Service class. --> 
                                <value>org.bamboo.springx.test.*.*DAO</value>     <!-- injectable bean's class must be business *DAO class.  --> 
                        </list>
                </property>
        </bean>

But if OrderDAO is an interface, impl class is OrderImpl,how does SpringX know which bean to be created and injected. SpringX provided Interface2ImplMapper to do this. It uses a wildcard interface to impl class mapping to indicate the rule, call Interface Injection.

  <!-- Interface Injection Mapping Tool.
                 used to find default impl class of specified interfaces.
                 API: implClass = interface2ImplMapper.getImplClass(InterfaceClass); but only used in framework itself.
        -->
        <bean id="org.bamboo.springx.Interface2ImplMapper" class="org.bamboo.springx.Interface2ImplMapper">
                <property name="interfaceClassMapping">                 
                        <props>
                                <prop key="java.util.Map">java.util.HashMap</prop>                              
                                <prop key="java.util.List">java.util.ArrayList</prop>                           
                                <prop key="org.bamboo.*.dao.*DAO">org.bamboo.*.dao.*DAOImpl</prop>
                                <prop key="org.bamboo.*.service.*Service">org.bamboo.*.service.*ServiceImpl</prop>
                        </props>
                </property>
        </bean>

Test code, you can download source and run


org.bamboo.spring.test.BeanFactoryTest 

Raymond KK He is a principal software engineer and architect in telecom field in Guangzhou,China. He wrote blogs and articles on javaeye.com community previousely. His interests are in JavaEE, framework design and development.

More from this author