4

I have some services that need configuration, thus these services is associated to a configuration class.

for example:

class ExampleService {
    @Autowired
    ExampleConfiguration conf;

    ...
}

class ExampleConfiguration {
    private String exampleProperty;

    public void setExampleProperty(String exampleProperty) {
        this.exampleProperty = exampleProperty;
    }

    public String getExampleProperty() {
        return exampleProperty;
    }
}

So does ExampleConfiguration must be declared as a @Component or @Configuration (or another annotation :))? And this class does not define any bean, it's just a basic configuration.

4

1 回答 1

4

我认为您误解了@Configuration注释的用法。它应该用于基于注释的配置,而不是基于 xml 的配置。它必须与@Bean注解结合使用,才能定义运行时可用的 bean。

看起来您实际上想要定义将存储一些配置属性的 bean。要自动装配它,你可以用@Component标记它。您还必须告诉 spring 上下文它可以通过component-scan.

于 2013-09-24T09:38:45.827 回答