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.

1 Answer 1

4

I think you misunderstand the usage of @Configuration annotation. It should be used for annotation-based configuration, as opposed to xml-based configuration. And it must be used in conjunction with @Bean annotation, to define beans available at runtime.

It looks like you actually want to define bean that will store some configuration properties. To autowire it, you can just mark it with @Component. You will also have to tell spring context where it could discover this bean via component-scan.

Sign up to request clarification or add additional context in comments.

1 Comment

Oki thanks, I was not sure about that. Now it's clear thanks to you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.