将bean从春季转换为春季靴

时间:2019-06-27 18:26:03

标签: spring spring-boot spring-bean

我的问题陈述如下: 我想连接到数据库,并从表中获取某些值,并将其分配给Bean。在当前的春季项目中,操作如下:

com.some.DbPropertyPlaceholderConfigurer类位于jar文件中。

    <bean id="propertyConfigurer"
                class="com.some.DbPropertyPlaceholderConfigurer">
                <property name="dataSourceName" value="dataSource" />
                <property name="locations">
                    <list>
                        <value>classpath:resources/context.properties</value>
                    </list>
                </property>
            </bean>

<bean id="someObject" class="com.some.beans.SomeObject">
        <property name="someprop" value="${prefix.someprop}" />
        <property name="someprop1" value="${prefix.someprop1}" />
        <property name="someprop2" value="${prefix.someprop2}" />
        ....
    </bean>

DbPropertyPlaceholderConfigurer在jar文件中,它从db表读取。 Db表具有键值列。 密钥包含“ prefix.someprop”,并且具有某些值。

现在,我正在使用注释将Spring Bean转换为Spring Boot Bean。 接下来是春天的豆子。 我想出了以下用于春季引导的方法,但是bean没有被初始化。 我不确定如何映射属性“ dataSourceName”和“ locations”

    @Configuration
    public class AppConfig {

        @Bean
        public DbPropertyPlaceholderConfigurer propertyConfigurer() {
            return new DbPropertyPlaceholderConfigurer();
        }

        @Bean
        public SomeObject someObject() {
           return new SomeObject();
        }
    }

它引发以下异常:

[localhost-startStop-1] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.lang.NullPointerException
    at com.some.configuration.DbPropertyPlaceholderConfigurer.processProperties(DbPropertyPlaceholderConfigurer.java:130)

如果它被初始化了,我将如何实际使用它?

0 个答案:

没有答案