Monday, February 18, 2013

Spring bean syntax




1. Import resources

Below is the syntax to import resources from various files. It is helpful when you want to bifurcated beans and database configuration in separate files and merger everything into single servlet. Here you can provide absolute or relative path of the source file.

<beans>
<import resource="file1.xml" />
<import resource="../file2.xml" />
</beans>

2. Spring MVC Dispatcher Servlet, syntax to load XMLs using init param

You can use it in web.xml file to XMLs using init param. You can load various XMLs like  beans and database configuration etc.

<web-app>
    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>            
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/test/*.xml
            </param-value>        
        </init-param>        
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/test/*.do</url-pattern>
    </servlet-mapping>
</web-app>

3. Url Handler Mapping syntax

Below is the example for Mapping URLs with respective beans, this is useful when you want to keep all URLs mapping together at one place.

<beans>

    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
        <props>
            <prop key="/sfc.do">simpleFormController</prop>
            <prop key="/mac.do">multiActionController</prop>
        </props>
        </property>
    </bean>


    <bean id="simpleFormController" class="com.javaxp.FormController" autowire="byName">
        <property name="commandName" value="formBean"></property>
        <property name="commandClass" value="com.javaxp.FormBean"></property>
        <property name="formView" value="form"></property>
        <property name="successView" value="submit"></property>        
    </bean>

    <bean id="multiActionController" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action" />
<property name="defaultMethodName" value="defaultPage" />
    </bean>

</beans>


4. XmlViewResolver and InternalResourceViewResolver

Below is the syntax for XmlViewResolver and InternalResourceViewResolver

<beans>
<bean id="viewResolver1" class="org.springframework.web.servlet.view.XmlViewResolver">
   <property name="location">
<value>/WEB-INF/test/views.xml</value>
   </property>
   <property name="order" value="0" />
</bean>

<bean id="viewResolver2" class=" org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="1" />
</bean>
</beans>

5. ResourceBundleMessageSource

Below is the syntax used to load resource bundle i.e. properties file. Below syntax shows how to load data from test.propeties

<beans>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
   <property name="basename" value="test" />
</bean>
</beans>

6. ParameterMethodNameResolver

This is used with view resolver. Here you provide the paramName, which can be used in url of multi-action controller. if paramName is not provided default method will be loaded i.e. defaultPage().

<beans>
<bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
   <property name="paramName" value="action" />
   <property name="defaultMethodName" value="defaultPage"/>
</bean>
</beans>

7. DWR settings

Below is the syntax to load DWR settings in Spring. In javascript you can call dwrService.method1(); and get the values from DwrServiceImpl.java

<beans>
<dwr:configuration />
<dwr:controller id="dwrController" debug="true" />
<bean id="iDwrService" class="com.javaxp.DwrServiceImpl" autowire="byName">
   <dwr:remote javascript="dwrService">
<dwr:include method="method1" />
<dwr:include method="method2" />
   </dwr:remote>
</bean>
</beans>

8. Locale Resolver

Below is the syntax to load Locale Resolver, here defaultLocale is english

<beans>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
   <property name="defaultLocale" value="en" />
</bean>
</beans>

9. Load data source and hibernate connection pooling

Below is the syntax to load data source, load hibernate pojo file and connection pooling. Spring IOC will inject serviceBean in Java.

<beans>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@10.10.10.10:1521:test" />
    <property name="username" value="TEST" />
    <property name="password" value="password" />
</bean>

<bean id ="hbmFiles" class="java.util.ArrayList">
    <constructor-arg index="0">
    <list>
    <value>com/javaxp/pojo/table1.hbm.xml</value>
    <value>com/javaxp/pojo/table2.hbm.xml</value>
    </list>
    </constructor-arg>
</bean>

<bean id="oracleHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
   <props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.default_schema">TEST</prop>                
<prop key="show_sql">true</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">600</prop>
   </props>
</property>
</bean>

<bean id="serviceBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" autowire="byName">
<property name="dataSource"><ref bean="dataSource" /></property>
<property name="mappingResources"><ref bean="hbmFiles"/></property>
<property name="hibernateProperties"><ref bean="oracleHibernateProperties"/></property>
</bean>

</beans>


10. Reference in same file

Reference to another bean in same file

<beans>
    <bean id="Queries" class="java.util.HashMap">
    <constructor-arg>
        <ref bean="ParentQueries" />
    </constructor-arg>
    </bean>

    <bean id="ParentQueries" class="java.util.HashMap">
    <constructor-arg index="0">
   <map>
   <entry key="key1" value="query1" />
   <entry key="key2" value="query2" />
   </map>
    </constructor-arg>
    </bean>
</beans>


11. Reference from parent file

Reference to another bean in parent file and override parent properties. Here it will import all values from parent files and override key1 value.

<import resource="parentFile.xml" />

<bean id="TestQueries" class="java.util.HashMap" parent="ParentQueries">
<constructor-arg index="0">
<map merge="true">
<entry key="key1" value="testQuery1" />
</map>
</constructor-arg>
</bean>



No comments:

Post a Comment