当前位置: 首页 > 知识库问答 >
问题:

配置类中的Spring bean未在.xml文件中定义的bean中自动连接

林龙野
2023-03-14

我有一个config类,我在其中创建了一个bean,在xml文件中声明的另一个bean中使用这个bean。当我试图从xml定义的bean中使用它时,它会抛出一个NPE,因为它从来没有自动连线过。我已经扫描了我要自动生成配置bean的包,但即使这样,也没有得到它。

MyPropertyPlaceHolderConfigurer.java

package org.util;

public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    @Autowired
    private FileBasedConfigurationBuilder<FileBasedConfiguration> propertiesConfigurationBuilder;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        //breaks due to NPE
        FileBasedConfiguration configuration = propertiesConfigurationBuilder.getConfiguration();
    }
}

applicationcontext.xml

    <bean id="myBean" class="org.util.MyPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <ref bean="appProps" />
            </list>
        </property>
    </bean>

    <context:component-scan base-package="org.util" />
package org.configuration;

@Configuration
public class PropertiesFileConfiguration {
    @Bean
    public FileBasedConfigurationBuilder<FileBasedConfiguration> propertiesConfigurationBuilder() {
        PropertiesBuilderParameters properties = new Parameters().properties();
        properties.setFileName("application.properties");

        FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
                new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
        builder.setAutoSave(true);

        return builder.configure(properties);
    }
}

我使用的是Spring4.3.26。不使用Springboot。

共有1个答案

弓俊晖
2023-03-14

您还可以尝试在XML中指定所需的自动连接依赖项:

如果您在MyPropertyPlaceHolderConfigurer中为PropertiesConfigurationBuilder添加了一个公共设置器,那么您可以在XML中提供配置生成器bean作为属性占位符bean的设置器参数,如下所示(refbean的名称,name是要设置的字段):

<bean id="myBean" class="org.util.MyPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <ref bean="appProps" />
            </list>
        </property>
        <property name="propertiesConfigurationBuilder" ref="propertiesConfigurationBuilder" />
    </bean>
 类似资料:
  • 我想提供一个特定的Bean,以便这个Bean覆盖Spring Cloud AutoConfiguration类中的Bean。 我还尝试使用自动配置。但是甚至注释也被忽略。 我的配置类bean总是在KubernetesClientAutoConfiguration类中的bean之后实例化。因此,AutoConfiguration类不使用我的bean。 文档说:在任何时候,您都可以开始定义自己的配置,

  • 基本上,我想问的是:将Spring Boot自动配置的bean自动导入XML配置文件的等价物是什么? 下面是我的主要Spring Boot入口点,它只是所有文档中列出的标准类: 我主要在一个Spring集成应用程序中使用它,在这个应用程序中Java配置还没有得到很好的支持,框架的核心是基于XML配置的,但是我希望在一些集成元素中使用Spring Boot自动配置的和bean。 https://gi

  • 我试图在Spring Boot中创建一个简单的REST服务。在我使用CrudRepository之前,一切都很好。现在我得到了这个错误- ***应用程序启动失败 描述: 公司中的现场er。Spring靴。io。受雇者EmployeeService需要“company”类型的bean。Spring靴。io。受雇者找不到EmployeeRepo“”。 措施: 考虑定义一个“company”类型的bea

  • 我正在做SpringBoot项目,并遵循一些测试SpringBoot的说明。 当我尝试将mysql DB与项目连接时,服务找不到映射器。 我不知道为什么它找不到映射器... 这是服务代码和 这是映射程序代码 下面的错误是 我将发布我的包裹设置的图片。。。

  • 我正在使用Spring配置文件(xml配置)进行Spring自动布线。我想根据一个条件注射豆子。让我详述一下。 有两个类“”和“”,它们实现了接口。在配置文件中为这两个类配置了bean。 > 我有另一个类,其中有一个类型为的实例变量。 我想根据SenderUser.getType()的值将注入到SenderUser的bean中。这意味着首先应该设置sernderuser.type,然后基于它的值(