Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void run(javax.sql.DataSource dataSource);
- <bean id="dataSource" abstract="true" />
- <bean id="dao" class="my.Dao">
- <property name="dataSource" ref="dataSource" />
- </bean>
- //create parent BeanFactory
- DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
- //register your pre-fabricated object in it
- parentBeanFactory.registerSingleton("dataSource", dataSource);
- //wrap BeanFactory inside ApplicationContext
- GenericApplicationContext parentContext =
- new GenericApplicationContext(parentBeanFactory);
- //create your "child" ApplicationContext that contains the beans from "beans.xml"
- //note that we are passing previously made parent ApplicationContext as parent
- ApplicationContext context = new ClassPathXmlApplicationContext(
- new String[] {"beans.xml"}, parentContext);
- public class PlaceholderBean implements BeanNameAware, FactoryBean {
- public static Map<String, Object> beansByName = new HashMap<String, Object>();
- private String beanName;
- @Override
- public void setBeanName(String beanName) {
- this.beanName = beanName;
- }
- @Override
- public Object getObject() {
- return beansByName.get(beanName);
- }
- @Override
- public Class<?> getObjectType() {
- return beansByName.get(beanName).getClass();
- }
- @Override
- public boolean isSingleton() {
- return true;
- }
- }
- <bean id="dataSource" class="PlaceholderBean" />
- public void run(DataSource externalDataSource) {
- PlaceholderBean.beansByName.put("dataSource", externalDataSource);
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
- assert externalDataSource == context.getBean("dataSource");
- }
- public class DataSourceWrapper implements DataSource {
- DataSource dataSource;
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- @Override
- public Connection getConnection() throws SQLException {
- return dataSource.getConnection();
- }
- @Override
- public Connection getConnection(String username, String password)
- throws SQLException {
- return dataSource.getConnection(username, password);
- }
- //delegate to all the other DataSource methods
- }
- ObjectToBeAddedDynamically objectInst = new ObjectToBeAddedDynamically();
- DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
- parentBeanFactory.registerSingleton("parameterObject", objectInst);
- GenericApplicationContext parentContext = new GenericApplicationContext(parentBeanFactory);
- XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(parentContext);
- xmlReader.loadBeanDefinitions(new FileSystemResource("beandefinitions.xml"));
- parentContext.refresh();
- ObjectUsingDynamicallyAddedObject userObjectInst= (ObjectUsingDynamicallyAddedObject )parentContext.getBean("userObject");
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="userObject" class="com.beanwiring.ObjectUsingDynamicallyAddedObject"
- >
- <constructor-arg ref="parameterObject" />
- </bean>
- </beans>
Add Comment
Please, Sign In to add comment