2011-04-24

Tips on Spring

Readings : References, Tutorials, Articles

Schemas for XML based configuration

Name Location Role
beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd defines the elements to define beans.
context http://www.springframework.org/schema/context/spring-context-3.0.xsd defines the configuration elements for the Spring Framework's application.
tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd defines the elements used in the Spring Framework's declarative transaction management infrastructure.
aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd defines the configuration elements for the Spring Framework's AOP support.
mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd defines elements to configure the annotation-driven Spring MVC Controller programming model
jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd defines configuration elements for access to traditional Java EE components such as JNDI resources and EJB session beans.
jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd defines the configuration elements for the Spring Framework's JMS support.
jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd defines the configuration elements for the Spring Framework's embedded database support.
int http://www.springframework.org/schema/integration/spring-integration-2.1.xsd defines the core configuration elements for Spring Integration.
int-ip http://www.springframework.org/schema/integration/ip/spring-integration-ip-2.1.xsd defines the configuration elements for Spring Integration's IP adapters.

Configuring Dependency in Maven to Use Auto-detecting and Name-based Auto-wiring of Beans with Spring 3.0

(2011-04-24)

To apply both bean auto-detecting using streotype such as @Component, @Controller, @Services, or @Repository and name-based (auto) wiring using @Resource annotation, you should manually add cglib into the Maven dependency config.

So, the part of the pom.xml would like this :

    
     org.springframework
     spring-core
     3.0.5.RELEASE
     jar
     compile
    
    
     org.springframework
     spring-context
     3.0.5.RELEASE
     jar
     compile
    
    
     org.springframework
     spring-aop
     3.0.5.RELEASE
     jar
     compile
    
    
     org.springframework
     spring-web
     3.0.5.RELEASE
     jar
     compile
    
    
     org.springframework
     spring-webmvc
     3.0.5.RELEASE
     jar
     compile
    
    
     cglib
     cglib-nodep
     2.2
     jar
     compile
    

Note that you need not or should not add asm into your pom.xml. Adding asm make the behavior of the build very confusing to me.

Hibernate 3.x has explicit dependency on asm. So, when using both Spring 3.0 and Hibernate 3.x together, it may be very difficult to correctly configure the Maven dependencies.
For details on this, refere the following : http://blog.newsplore.com/2009/03/07/upgrading-to-spring-30

Configuring SqlMapClientFactoryBean

(2009-08-13)

SqlMapClientBean of springframework creates SqlMapClient with EXTERNAL transaction management by default and internally wrap the given data source to TransactionAwareDataSourceProxy.

So, the typical setup for iBATIS SqlMapClient bean in spring configuration would be like the following.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
 <property name="username" value="_user_" />
 <property name="password" value="_passwd_" />
 <property name="defaultAutoCommit" value="true" />
 <property name="maxActive" value="10" />
 <property name="maxIdle" value="10" />
 <property name="minIdle" value="0" />
 <property name="initialSize" value="10" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
 <property name="defaultTimeout" value="120" />
</bean>
 
<tx:annotation-driven 
 transaction-manager="transactionManager"
 mode="proxy"
 proxy-target-class="true"
/>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
 <description>default SqlMapClient bean for DAOs</description>
 <property name="configLocation" value="classpath:_pathto_/sql-map.xml" />
  
 <property name="dataSource" ref="dataSource" />
 <!-- Default setting is specified for clarity. -->
 <property name="useTransactionAwareDataSource" value="true"/>
 <!-- Default setting is specified for clarity. -->
 <property name="transactionConfigClass">
  <value type="java.lang.Class">com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig
 </property>

</bean>

At the above sample, the default setup for useTransactionAwareDataSource and transactionConfigClass properties is specified for clarity.

ExternalTransactionConfig would set the "SetAutoCommitAllowed" property of SqlMapClient to false, in contrast to the iBATIS default, to always keep the original autoCommit value as provided by the connection pool.

For more information, refer API documentation of SqlMapClientFactoryBean, specially setDataSource, setUseTransactionAwareDataSource, and setTransactionConfigClass methods.

Annotation Based JMX in Spring Framework 2.5

(2008-08-04)

In spring framework 2.5, to use annotation based JMX

  1. The position of the JMX bean definition in bean config
  2. The type of proxy
is important.

0 comments:

Post a Comment