Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in Spring I am not allowed to annotate a final class with @Configuration?

I am studying for the Spring Core certification and I have some doubts related to the answer of this question founded on the study material stuff.

Why are you not allowed to annotate a final class with @Configuration

My reasoning is the following one for substantiate this assertion:

Consider the following configuration class:

@Bean
public AccountRepository accountRepository() {
    return new JdbcAccountRepository(); 
}

@Bean
public TransferService transferService() {
    TransferServiceImpl service = new TransferServiceImpl();
    service.setAccountRepository(accountRepository());
    return service;
}

@Bean
public AccountService accountService() {
    return new AccountServiceImpl(accountRepository());
}

At first look this situation could appear strange because the first method (accountRepository()) instantiates an JdbcAccountRepository object as a bean having id=AccountRepository that, following the Spring default behavior, is a singleton

The second and the third method call twice more time the accountRepository() method that should instantiate twice more JdbcAccountRepository objects and this is not possibile because it is singleton !!!

So, to solve this situation Spring use the Inheritance-based Proxies strategy that expect to create a child class of my configuration class (the one annoted by @Configuration) and it is does:

  • For each bean, an instance is cached in the child class

  • Child class only calls super at first instantiation

So the child class is the entry point because the following behavior is implemented by this child class:

public class AppConfig$$EnhancerByCGLIB$ extends AppConfig {

public AccountRepository accountRepository() {
    // if bean is in the applicationContext
    // return bean
    // else call super.accountRepository() and store bean in context
}

public TransferService transferService() {
    // if bean is in the applicationContext, return bean
    // else call super.transferService() and store bean in context
}

.....................................................
.....................................................
.....................................................
}

So if I annotate a configuration class with final Spring can't have this behavior because in Java a final class cannot be subclassed

Is it correct?

Using the same reasoning can I also assert that in Spring I can't have a final method annoted with @Bean annotation?

Because, as shown in the previous example, I have that when at startup time is created the child class (the proxy) of my configuration class happens that for each bean, an instance is cached in the child class and if it is final it is not possible (but I am absolutly not sure about this assertion)

Am I missing something? Can you give me the exact explaination?

Tnx

like image 288
AndreaNobili Avatar asked Mar 16 '15 10:03

AndreaNobili


People also ask

How do I mark a configuration class in Spring boot?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

How do @configuration annotated classes support Singleton beans?

If you use @Configuration, all methods marked as @Bean will be wrapped into a CGLIB wrapper which works as if it's the first call of this method, then the original method's body will be executed and the resulting object will be registered in the spring context.

What is the difference between @configuration and @EnableAutoConfiguration?

@EnableAutoConfiguration : enable Spring Boot's auto-configuration mechanism. @ComponentScan : enable @Component scan on the package where the application is located (see the best practices) @Configuration : allow to register extra beans in the context or import additional configuration classes.


1 Answers

Spring creates dynamic proxies for classes annotated with @Configuration classes. Spring uses CGLIB to extend your class to create proxy. Hence, configuration classes cannot be final.

Regarding accountRepository() being invoked twice:

If you invoke accountRepository() method to create an instance, it is no more a Spring managed bean. Spring will not have any idea of the instances created in this manner. Hence, you will end up with multiple instances of JdbcAccountRepository

You can preserve the singleton behavior if you configure as below:

@Bean
public TransferService transferService(JdbcAccountRepository jdbcAcctRepo) {
    TransferServiceImpl service = new TransferServiceImpl();
    service.setAccountRepository(jdbcAcctRepo);
    return service;
}

@Bean
public AccountService accountService(JdbcAccountRepository jdbcAcctRepo) {
    return new AccountServiceImpl(jdbcAcctRepo);
} 
like image 181
Mithun Avatar answered Nov 01 '22 00:11

Mithun