Do a simple program wang, share dry goods, talk about life.

Wechat public account: Advanced backend architecture

Follow me to find more dry goods, microservices, Spring source, JVM, SpringCloud Alibaba, K8S, etc.

If you found this article helpful, please give me a like and thank you for your support!

In our normal development, using a Bean is more of a matter of handing it over to the Spring container to manage, and then taking our Bean directly to @Resource or @Autowied where needed. So how does this process work?

Get the Bean from the Spring container

We need to get the Bean from the container. How do we do that? Spring provides two classes:

ClassPathXmlApplicationContext based on XML file

AnnotationConfigApplicationContext annotation-based

Spring’s naming is fairly standard.

ClassPathXmlApplicationContext: then reads the configuration from the classpath to find XML beans. For example, I define this XML file under my Resources:

My configuration file looks like this:

** @author AerCool Yang * @date 2021-12-04 11:44:14 */ public class Main {public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Object userService = context.getBean("userService"); System.out.println(userService); }}Copy the code

The running results are as follows:

I'm the constructor of UserService... com.aercool.bean.UserService@2925bf5bCopy the code

This was the way it used to be, but now it’s more annotation-based.

Annotations are straightforward. Details are as follows:

@author AerCool Yang * @date 2021-12-04 11:47:09 */ public class Config {@bean public UserService userService() { return new UserService(); }}Copy the code

Note here that we don’t have the @Configuration annotation. Think about the difference between plus and no plus, and we’ll explain that later.

** @author AerCool Yang * @date 2021-12-04 11:44:14 */ public class Main {public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);  Object userService = context.getBean("userService"); System.out.println(userService); }}Copy the code

The following output is displayed:

I'm the constructor of UserService... com.aercool.bean.UserService@618b19adCopy the code

The difference between @ComponentScan and no

Class (@ComponentScan); class (@Bean); class (@Bean); class (@Bean); class (@Bean); Even if I added an OrderService labeled @service, I wouldn’t be able to read it.

But if I put @ComponentScan on it it’s not going to look the same. It can be read. This means to find the path to the class. Then go under the package and scan for all classes annotated with @Component, @service, etc., and add them to our container. The OrderService mentioned above can then be instantiated.

You can try it if you don’t believe me.

What does getBean do?

  • If scan is added, traverse the scan path
  • Will be added@Component,@ServiceThe annotated class is added to the bean definition MapBeanDefinitionMap
  • Spring will generate the beanName and generated Bean according to the rule, and then store it in our level 1 cache, and then it can be directly retrieved from the cache for later use.
  • GetBean () will fetch it directly from the cache if it finds it has already been created.

This is the process of getting a Bean.

Well, that’s all for today’s article. More Spring articles are working overtime. Like old iron, remember to give a thumbs-up!

Summary of historical articles

conclusion

The above is the summary and sharing of the previous content, thank you for your attention, thumbs up and favorites!

Wechat public account: Advanced backend architecture

More articles are coming, like to remember to give me a 👍, thank you for your support!

Public article synchronous update! Focus on me, don’t get lost!