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

最佳答案

您可以创建新的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;
    }
}

09-27 14:41