2

I am new in Spring. I understand process of Dependency Injection and Inversion Of Control as well. But in few days ago I found one source code which compel me thinking about it.

If I am not wrong, Beans can be registered by Stereotype annotations - @Component, @Service, etc.

In code which I found will be defined class with some logic, but without annotation. Next that same class will be initialized in some @Configuration class as been like that:

@Bean
public Foo fooBean() {
   return new Foo();
}

Can you tell me what is different between these options and when they use? Thanks in advice.

2 Answers 2

6

The greatest benefit of @Configuration and @Bean is that allows you to create spring beans that are not decorated with @Component or any of its children (@Service, @Repository and those). This is really helpful when you want/need to define spring beans that are defined in an external library that has no direct interaction with Spring (maybe written by you or somebody else).

E.g.

You have a jar created by an external provider that contains this class:

public class EmailSender {

    private String config1;
    private String config2;
    //and on...

    public void sendEmail(String from, String to, String title, String body, File[] attachments) {
        /* implementation */
    }
}

Since the class is in an external jar, you cannot modify it. Still, Spring allows you to create spring beans based on this class (remember, the bean is the object, not the class).

In your project, you'll have something like this:

import thepackage.from.externaljar.EmailSender;

@Configuration
public class EmailSenderConfiguration {

    @Bean
    public EmailSender emailSender() {
        EmailSender emailSender = new EmailSender();
        emailSender.setConfig1(...);
        emailSender.setConfig2(...);
        //and on...
        return emailSender;
    }
}

And then you can inject the bean as needed:

@Service
public class MyService {

    @Autowired
    private EmailSender emailSender;
}
4
  • It's nice explanation. But that class is written by author in same project. So both options are correct in that situation(Use stereotype or Bean) ? Jan 31, 2018 at 14:33
  • @DenisStephanov assume EmailSender class is not written by the same author. It's an external jar that arrives to you, maybe downloaded as a dependency via maven. Jan 31, 2018 at 14:45
  • I am understand what you mean, but if class in same jar is ok to use both solutions? Jan 31, 2018 at 15:00
  • @Configuration and @Bean is a tool offered by Spring. It's not about if it's a good practice or not. It is ok to use both solutions, as long as it works. Now, if you want to take into account code readability, maintainability and other things, I'll suggest to try to keep it simple. If for you, using @Component is already quite simple, then that's it. I'll suggest using the tools only if needed. Jan 31, 2018 at 15:03
0

@Configuration is used to define your configuration of your application. In the end @Bean, @Service, @Component will all register a bean, but using @Configuration with all beans (services, components) defined in a single place makes your app more organized and easier to troubleshoot.

1
  • So I also don't mistake when I mark that class as @Component and next I will use @Autowired? Both options are correct? Jan 31, 2018 at 14:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.