spring entry: @Component @Configuration @Bean difference

1, why have @Component, @Bean need it?
If you want a third-party library components fitted to your application, in this case, there is no way to add annotations @Component in its class, so you can not use the automated assembly programs, but we You can use @Bean, of course, you can use XML configuration.

2, component, and configuration differences

@Autowired Student student;

3, generally only in @Configuration in Bean

4, @ Component and @Configuration difference

https://blog.csdn.net/isea533/article/details/78072133

5、

@Bean usage

@Bean is a comment on a method level, mainly used in @Configuration annotated class , it can also be used in @Component annotated class . Add the bean id is the method name

Bean definitions

Here is an example of where @Configuration

  1. @Configuration
    
    public class AppConfig {
    
    
    @Bean
    
    public TransferService transferService() {
    
    return new TransferServiceImpl();
    
    }
    
    
    }
    
    这个配置就等同于之前在xml里的配置
    
    <beans>
    
    <bean id="transferService" class="com.acme.TransferServiceImpl"/>
    
    </beans>

     

  2. @Configuration's marker must meet the following requirements:

Configuring class must provide as a class (not instance factory method is returned), allowing enhanced by generating sub-classes at runtime (CGLIB dynamic proxies).
Configuration class can not be a final class (not dynamic proxy).
Annotations typically configured for generating annotation by @Bean Spring container class,
configuration class must be non-local (i.e., in the process can not be declared, it is not private).
Any nested configuration class must be declared as static.
@Bean method may not in turn create further configuration class (that is, if the bean returned with @Configuration, will not be special treatment, just as ordinary bean).
----------------
Disclaimer: This article is CSDN blogger "isea533 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/isea533/article/details/78072133

 

Published 23 original articles · won praise 10 · views 120 000 +

Guess you like

Origin blog.csdn.net/gui694278452/article/details/104379099