code_junkie

Adding a Pre-constructed Bean to a Spring Application Context

Nov 13th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. public void run(javax.sql.DataSource dataSource);
  2.  
  3. <bean id="dataSource" abstract="true" />
  4.  
  5. <bean id="dao" class="my.Dao">
  6. <property name="dataSource" ref="dataSource" />
  7. </bean>
  8.  
  9. //create parent BeanFactory
  10. DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
  11. //register your pre-fabricated object in it
  12. parentBeanFactory.registerSingleton("dataSource", dataSource);
  13. //wrap BeanFactory inside ApplicationContext
  14. GenericApplicationContext parentContext =
  15. new GenericApplicationContext(parentBeanFactory);
  16.  
  17. //create your "child" ApplicationContext that contains the beans from "beans.xml"
  18. //note that we are passing previously made parent ApplicationContext as parent
  19. ApplicationContext context = new ClassPathXmlApplicationContext(
  20. new String[] {"beans.xml"}, parentContext);
  21.  
  22. public class PlaceholderBean implements BeanNameAware, FactoryBean {
  23.  
  24. public static Map<String, Object> beansByName = new HashMap<String, Object>();
  25.  
  26. private String beanName;
  27.  
  28. @Override
  29. public void setBeanName(String beanName) {
  30. this.beanName = beanName;
  31. }
  32.  
  33. @Override
  34. public Object getObject() {
  35. return beansByName.get(beanName);
  36. }
  37.  
  38. @Override
  39. public Class<?> getObjectType() {
  40. return beansByName.get(beanName).getClass();
  41. }
  42.  
  43. @Override
  44. public boolean isSingleton() {
  45. return true;
  46. }
  47.  
  48. }
  49.  
  50. <bean id="dataSource" class="PlaceholderBean" />
  51.  
  52. public void run(DataSource externalDataSource) {
  53. PlaceholderBean.beansByName.put("dataSource", externalDataSource);
  54. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  55. assert externalDataSource == context.getBean("dataSource");
  56. }
  57.  
  58. public class DataSourceWrapper implements DataSource {
  59.  
  60. DataSource dataSource;
  61.  
  62. public void setDataSource(DataSource dataSource) {
  63. this.dataSource = dataSource;
  64. }
  65.  
  66. @Override
  67. public Connection getConnection() throws SQLException {
  68. return dataSource.getConnection();
  69. }
  70.  
  71. @Override
  72. public Connection getConnection(String username, String password)
  73. throws SQLException {
  74. return dataSource.getConnection(username, password);
  75. }
  76. //delegate to all the other DataSource methods
  77. }
  78.  
  79. ObjectToBeAddedDynamically objectInst = new ObjectToBeAddedDynamically();
  80. DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
  81. parentBeanFactory.registerSingleton("parameterObject", objectInst);
  82.  
  83. GenericApplicationContext parentContext = new GenericApplicationContext(parentBeanFactory);
  84.  
  85. XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(parentContext);
  86. xmlReader.loadBeanDefinitions(new FileSystemResource("beandefinitions.xml"));
  87. parentContext.refresh();
  88.  
  89. ObjectUsingDynamicallyAddedObject userObjectInst= (ObjectUsingDynamicallyAddedObject )parentContext.getBean("userObject");
  90.  
  91. <?xml version="1.0" encoding="UTF-8"?>
  92. <beans xmlns="http://www.springframework.org/schema/beans"
  93. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  94. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  95.  
  96. <bean id="userObject" class="com.beanwiring.ObjectUsingDynamicallyAddedObject"
  97. >
  98. <constructor-arg ref="parameterObject" />
  99.  
  100. </bean>
  101.  
  102. </beans>
Add Comment
Please, Sign In to add comment