user2736812
user2736812

Reputation: 1331

convert bean xml to spring boot annotation

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)

Upvotes: 3

Views: 2333

Answers (2)

Matej Marconak
Matej Marconak

Reputation: 1413

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;
    }
}

Upvotes: 4

Nitin1706
Nitin1706

Reputation: 659

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.

Upvotes: 0

Related Questions