首页 > 解决方案 > 将bean xml转换为spring boot注解

问题描述

我有以下来自 spring 框架的 bean xml。现在我们正在转向带有所有注释的 Spring Boot。如何将以下 bean 转换为注释?

<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 (是其他一些库)

标签: javaspringspring-boot

解决方案


您可以创建新Configuration课程。<bean>您可以使用注释重写@Bean,然后创建new类的实例并使用设置变量的设置器。

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

推荐阅读