Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic assembly with Spring @ Autowired

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use Spring @ Autowired to achieve automatic assembly", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Spring @ Autowired to realize automatic assembly.

Before learning about automatic assembly, let's talk about a concept: Component, that is, components. Components can also be understood as bean objects, but usually the composition of Component is a little more complicated, for example, one or more individual bean objects are referenced in a component, and components are defined in a different way. Automatic assembly seems to be made for components.

The concept of Autowired, if you have used SpringMVC or SpringBoot for development, you will find that the @ Autowired annotation is used very often. For example, you will often use it in your Controller and Service related classes. If you look at the definition of @ Controller or @ Service, you will find that these two annotations also use the @ Component annotation, which means that these two annotations are also Component.

Auto-assembly means that you do not have to manually implement the composition relationship between bean, as long as you use the @ Autowired annotation, the program will automatically inject the required bean, as long as your Spring container has the bean. Let's look at an example.

This example goes like this. I define a Car interface with a run () method, and then I define a BaoMaCar that implements the Car interface, and then I define a Test.java class, and I use the @ Component annotation on BaoMaCar and Test, which shows that my two classes need to create bean objects managed by Spring, which is a bit roundabout. It's better to say this: after using @ Component. Spring uses these two classes to create the corresponding bean objects and put them in the container. There is also a MyConfig.java class, which uses @ Configuration and @ ComponentScan annotations. This class is a Spring configuration class, because I need this configuration class to start the Spring context, and I also need to let Spring find these Component that I declare, so I also need to use @ Component annotation (Chinese meaning: component scanning). The parameter is the current package, and all classes under the current package are scanned by default. See which class is managed by Spring using the @ Component annotation. Just look at the code!

Package demo2;public interface Car {void move ();} package demo2;import org.springframework.stereotype.Component;@Componentpublic class BaoMaCar implements Car {private String driver = "small white open"; public void move () {System.out.println (driver+ "driving");}} package demo2;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan ("demo2") public class MyConfig {} package demo2 Import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class Test {@ Autowired private Car car; public Car getCar () {return car;} public static void main (String [] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (MyConfig.class); Test test = context.getBean (Test.class); test.getCar () .move () Context.close ();}}

You might think what does half a day have to do with automatic configuration? Take a look at the last class, Test.java, which uses the @ Autowired annotation. Guess what happens when I start the main () method? The console will print: "Xiaobai is driving". Do you think it's weird? How do you do that? Let me take my time for you. I got an object of the Test class from the context, and my Test class itself uses the @ Component annotation, so the Test object has been created in the container after the context is started. So how can the Car car property of the Test object not report a null pointer error? Yes, when creating an object of Test, Spring found that the Car car attribute of this class uses @ Autowired annotation, so it looked for an object in the container that implemented the Car interface. After a search, it found the object of BaoMaCar, and injected this object into the car property of the object of Test. Isn't that amazing? Of course, this is problematic when there are multiple objects in the container that implement the Car interface, because Spring knows that you need an object that implements the Car interface, but because there are multiple objects that meet this condition, it cannot be automatically injected for you, so an exception occurs. There are several ways to solve the problem.

At this point, I believe you have a deeper understanding of "how to use Spring @ Autowired to achieve automatic assembly". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report