preface
As we all know, in our normal development, the Spring framework has been around the whole project, not that we can’t do without Spring, but that it’s too expensive to do so. Spring IOC, AOP, transaction, etc., will always be asked every interview, today this is a job interview question I just met, poor I only know @service. Then he went back to wait for an announcement, which he hasn’t given me yet.
In addition, today this article will talk about several injection methods, as for the principle of good brothers don’t think first (don’t hit my face on the line), this casually take out a speak source to speak again, the main need to rely on the advantages of many things, so find an opportunity willSpring IOC
That piece of the source code to come out about, this article is mainly about.
To the chase
1 @service, @Component, @repository, @controller
@ComponentScan (@ComponentScan); @ComponentScan (@ComponentScan); @Component (@ComponentScan); Why not use @Component? Spring is trying to distinguish between Bean types. @Repository for persistence, @Service for business logic, and @Controller for presentation (spring-MVC notes)
@Component
public class ApproveCenterServiceImpl implements ApproveCenterService {}@Service
public class ApproveCenterServiceImpl implements ApproveCenterService {}@Repository
public class ApproveCenterServiceImpl implements ApproveCenterService {}Copy the code
2 @ Bean and @ Configuration
The simplest example of this is when we use SpringBoot to manually register a Bean called RestTemplate
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(a) {
return newRestTemplate(); }}Copy the code
3 using the @ Import
The @import annotation registers three types of beans: the general class, the DefinitionRegistrar class, and the ImportSelector class
3.1 ordinary class
/ * * *@author xiejianwei
*/
@Import(MessageRecord.class)
public @interface ImportBean {
}
Copy the code
3.2 DefinitionRegistrar
Implements ImportBeanDefinitionRegistrar this way in the implementation class provides a registry, there is a very familiar annotation @ MapperScan good brothers in to look at will understand.
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {
}
public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar.ResourceLoaderAware {
/** * Mainly this method, which provides BeanDefinitionRegistry as an argument for manually injecting beans into containers */
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes mapperScanAttrs = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
if(mapperScanAttrs ! =null) { registerBeanDefinitions(mapperScanAttrs, registry); }}}Copy the code
3.3 ImportSelector
Many Springboot starter uses ImportSelector to enable a function, such as the @enableDiscoveryClient annotation to enable service registration and discovery, Main principle is the selectImports ImportSelector method returns a string array, the string is actually we need the full path of the registered class one by one, similar to the com. XJW. Entity. The pojo. MessageRecord, Spring generates beans based on full path reflection. This piece of source code will not talk about (manual dog head).
public class CommonModelSelector implements ImportSelector {
public CommonModelSelector(a) {}public String[] selectImports(AnnotationMetadata annotationMetadata) {
return newString[]{MessageRecord.class.getName()}; }}Copy the code
4 FactoryBean
FactoryBean
Is a special oneBean
Are many good brothers asked this question in job interviewsFactoryBean
andBeanFactry
I get a tear in my eye when I say this (which, to be fair, is often asked when talking to so-called senior engineers).FactoryBean
Is a specialBean
When we implement this interface, we generate twoBean
Object, the first of which is the implementation class itself, needs to be used inBean
Before the name (normally, the first letter of the class name is lowercase)&
In order to getBean
And the secondBean
In fact, that meansgetObject
Method returnbean
It is important to note that this approach requires coordination@Component
or@Configuration
To implement.BeanFactry
In a nutshellSpring
Context container.
Example code is as follows:
@Component
public class FactoryBeanLearn implements FactoryBean {
@Override
public Object getObject(a) throws Exception {
// This Bean is our own new, so we can control the Bean creation process
return new MessageRecord();
}
/** * the type of the bean ** */
@Override
publicClass<? > getObjectType() {return MessageRecord.class;
}
/** * is the singleton ** */
@Override
public boolean isSingleton(a) {
return true; }}Copy the code
That’s the end of this issue. Welcome to leave your comments in the comments sectionAsk for attention, ask for likes