Monday, February 18, 2013

Get spring bean objects With java code


Here My applicationContext.xml:






<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<!-- ...............DATASOURCE CONFIGURATION BEGIN.............. -->
<bean id="itDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
<!-- <property name="jdbcUrl"
   value="jdbc:jtds:sqlserver://XXXXX/ICD_TOOL_SCHEMA" /> -->

<property name="jdbcUrl"
   value="jdbc:jtds:sqlserver://XXX/ICD_TOOL_SCHEMA" />

<property name="properties">
<props>
<prop key="c3p0.acquire_increment">10</prop>
<prop key="c3p0.idle_test_period">100</prop>
<prop key="c3p0.max_size">500</prop>
<prop key="c3p0.max_statements">0</prop>
<prop key="c3p0.min_size">10</prop>
<prop key="c3p0.timeout">120</prop>
<prop key="c3p0.testConnectionOnCheckout">true</prop>
<prop key="c3p0.maxConnectionAge">14400</prop>
<prop key="c3p0.maxIdleTime">10000</prop>
<prop key="c3p0.maxIdleTimeExcessConnections">120</prop>
<!-- <prop key="user">asmanicd</prop>
<prop key="password">asman.123</prop> -->
<prop key="user">icddev</prop>
<prop key="password">icddev</prop>

</props>
</property>
</bean>
<bean id="icdDatabase" class="com.asman.itrans.action.OrganizationsAction">
<property name="asmanDataSource" ref="itDataSource"/>
</bean>
<!--
  .........................DATASOURCE CONFIGURATION
  END..................
 -->

<!--
  .......................HIBERNATE CONFIGURATION BEGIN.................
 -->
<bean id="itSessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="itDataSource" />
<property name="annotatedClasses">

<list>
<value>com.asman.itrans.to.common.Users</value>
<value>com.asman.itrans.to.common.Icd10DrgMapper</value>
<value>com.asman.itrans.to.common.FinancialReport</value>
<value>com.asman.itrans.to.common.Icd10ReimbursementMap</value>
<value>com.asman.itrans.to.common.MasterNoMap</value>
<value>com.asman.itrans.to.common.UserType</value>


</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect</prop>
<!-- <prop key="hibernate.show_sql">true</prop> -->
<!--  <prop key="hibernate.cache.provider_class"> net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/classes/ehcache.xml</prop> -->
</props>
</property>
</bean>
<!-- ............. HIBERNATE CONFIGURATION END................ -->

<!-- .................. ACTION *** BEGIN....................... -->
<bean id="importAction" class="com.asman.itrans.action.ImportDataAction"
  singleton="false">
<property name="organizationService" ref="organizationService">
</property>
<property name="serviceLineService" ref="serviceLineService">
</property>  
</bean>
<bean id="loginAction" class="com.asman.itrans.action.LoginAction">

<property name="usersService" ref="usersService">
</property>

<property name="organizationService" ref="organizationService">
</property>

<property name="loginService" ref="loginService">
</property>

<property
            name="databaseService"
            ref="databaseService" >
</property>


</bean>

<
   
     
   
     


 <!--
  ................... ACTION *** END...............................
 -->

<!-- SERVICE *** BEGIN -->
<!-- <bean id="importservice" class="com.asman.itrans.service.ImportDataService"
  singleton="false">
<property name="importdao" ref="importdao"></property>
<property name="sessionFactory" ref="itSessionFactory"></property>
</bean> -->
<bean id="loginService" class="com.asman.itrans.service.LoginService">

<property name="usersHome" ref="usersHome">
</property>

<property name="sessionFactory" ref="itSessionFactory">
</property>
</bean>

<bean id="usersService" class="com.asman.itrans.service.UsersService">

<property name="usersHome" ref="usersHome">
</property>

<property name="sessionFactory" ref="itSessionFactory">
</property>
</bean>

<bean id="adminService" class="com.asman.itrans.service.AdminService">

<property name="usersHome" ref="usersHome">
</property>

<property name="sessionFactory" ref="itSessionFactory">
</property>
</bean>

<
     

     
 <!-- SERVICE *** END -->

<!-- ...........DAO *** BEGIN................................ -->
<!--  <bean id="importdao" class="com.asman.itrans.dao.ImportDataDao" /> -->

<bean id="usersHome" class="com.asman.itrans.common.dao.UsersHome" />

<bean id="organisationHome" class="com.asman.itrans.common.dao.OrganisationHome" />

<bean id="serviceLineHome" class="com.asman.itrans.common.dao.ServiceLineHome" />


<!-- ..... DAO *** END...... -->

</beans>





Now We want get any spring bean object.just integrate this code

YourJava Class:

ApplicationContext appContext = new ClassPathXmlApplicationContext();
Resource resource;
resource = appContext.getResource("classpath:/applicationContext.xml");

XmlBeanFactory factory = new XmlBeanFactory(resource);

System.out.println(factory.getBean("icdDatabase"));


Here We get OrganizationsAction object
OrganizationsAction orgaction=(OrganizationsAction)factory.getBean("icdDatabase")

Here we get OrganizationService object
OrganizationService source = (OrganizationService) factory
.getBean("organizationService");

like we will get any object from spring application context




</div>

No comments:

Post a Comment