I want to create datasource bean programmatically depending on args passed from user then
public class Main {
public static void main( String[] args ) throws IOException, InterruptedException {
//TODO load proper configs depending on args
//TODO initiate dataSource bean with UN and PS in args
ApplicationContext context = new ClassPathXmlApplicationContext( "config/applicationContext-common.xml" );
}
}
I've done a little googling already and found that I have to create a post processor bean that creates a data source bean and programmatically configures it using the fetched data. What I'd like to see is a real example.
You can try the code below, it should inject dataSource before wiring.
public class Main {
public static void main( String[] args ) throws IOException, InterruptedException {
//TODO load proper configs depending on args
//TODO initiate dataSource bean with UN and PS in args
ApplicationContext context = new ClassPathXmlApplicationContext("config/applicationContext-common.xml") {
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
super.prepareBeanFactory(beanFactory);
beanFactory.registerSingleton("dataSource", dataSource);
}
};
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.