3

I have the following bean xml from spring framework. Now we are moving to spring boot with all annotations. How do I convert the following bean to annotations ?

<bean id="testPool" class="com.v1.testPoolImpl" init-method="init" destroy-method="shutdown"> <property name="configurations"> <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:database.properties"/> </bean> </property> </bean> 

com.v1.testPoolImpl (is some other library)

2 Answers 2

4

You can create new Configuration class. You can rewrite <bean> with annotation @Bean and for then create new instance of class and using setters you set variables.

@Configuration public class Config { @Bean(initMethod = "init" , destroyMethod = "shutdown") public Object testPool(){ testPoolImpl testPool = new testPoolImpl(); PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation( new ClassPathResource("database.properties")); propertiesFactoryBean.afterPropertiesSet(); testPool.setConfigurations(propertiesFactoryBean.getObject()); return testPool; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

testPool.setConfigurations(propertiesFactoryBean); should be testPool.setConfigurations(propertiesFactoryBean.getObject()) and before that you should call the propertiesFactoryBean.afterPropertiesSet() method to honor the lifecycle.
0

With Spring boot, you can specify properties file through command-line when running jar file, using command:

java -jar app.jar --spring.config.location=classpath:/another-location.properties 

Alternatively, you can mention property files using @PropertySource Annotation on javaConfig file. So, it would be something like this:

@PropertySource("classpath:my-app.properties") public class PropertiesWithJavaConfig { //... } 

Hope this helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.