Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 2.64 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to simplify adding multiple beans in the xml file which uses the DataSource in Spring
  2. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">      
  3.         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
  4.         <property name="url" value="#{T(java.lang.System).getenv(DB_URL')}"/>
  5.         <property name="username" value="#{T(java.lang.System).getenv('DB_USER')}"/>
  6.         <property name="password" value="#{T(java.lang.System).getenv('DB_PASS')}"/>        
  7.     </bean>
  8.  
  9.  
  10. <bean id="classA" class="com.example.ClassA">
  11.     <property name="dataSource" ref="dataSource"/>
  12. </bean>
  13.  
  14. <bean id="classB" class="com.example.ClassB">
  15.     <property name="dataSource" ref="dataSource"/>
  16. </bean>
  17.        
  18. public static void main(String[] args) {        
  19.  
  20.         ApplicationContext context = new ClassPathXmlApplicationContext("spring_bean_file.xml");
  21.             ClassA classA = (ClassA) context.getBean("classA");
  22.             ClassB classB = (ClassB) context.getBean("classB");
  23.             try {
  24.                 rrRpi.generateRrRpi();
  25.                 rrSpi.generateRrSpi();
  26.             } catch (SQLException e) {
  27.  
  28.                 e.printStackTrace();
  29.             } catch (IOException e) {
  30.  
  31.                 e.printStackTrace();
  32.             }
  33.  
  34.          //close the context            
  35.         ((ClassPathXmlApplicationContext) context).close();
  36.  
  37.     }
  38.        
  39. @Service
  40. public class ClassA {
  41.     @Resource
  42.     private DataSource dataSource;
  43.  
  44.     //...
  45. }
  46.  
  47. @Service
  48. public class ClassB {
  49.     @Resource
  50.     private DataSource dataSource;
  51.  
  52.     //...
  53. }
  54.        
  55. <beans xmlns="http://www.springframework.org/schema/beans"
  56. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  57. xmlns:context="http://www.springframework.org/schema/context"
  58. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  59.                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  60.  
  61.     <context:component-scan base-package="com.example" />
  62.  
  63.     <!-- ... -->
  64. </beans
  65.        
  66. @Configuration
  67. public class Cfg {
  68.  
  69.     @Bean
  70.     public DataSource dataSource() {
  71.         BasicDataSource ds = new BasicDataSource();
  72.         ds.setDriverClassName("oracle.jdbc.driver.OracleDriver);
  73.         //...
  74.         return ds;
  75.     }
  76.  
  77.     @Bean
  78.     public ClassA classA() {
  79.         ClassA ca = new ClassA()
  80.         ca.setDataSource(dataSource());
  81.         return ca;
  82.     }
  83.  
  84.     @Bean
  85.     public ClassB classB() {
  86.         ClassB cb = new ClassB()
  87.         cb.setDataSource(dataSource());
  88.         return cb;
  89.     }
  90.  
  91. }
  92.        
  93. new AnnotationConfigApplicationContext(Cfg.class);