In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how SpringBoot is injected into Bean through annotations", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "how SpringBoot is injected into Bean through annotations".
1. Background
When we talk about Spring, we must mention the IOC container and DI dependency injection. Spring achieves the effect of control inversion by injecting the methods labeled Bean one by one into the IOC container. So when we first came into contact with Bean, we must have used xml files to inject them one by one, such as the following.
If our project is generally very large, it will need hundreds of Bean to use, so it is very tedious to write. Then Spring helps us implement a method of injection through annotations. Just add comments to the classes you need to inject, and Spring will help us scan them to implement the injection.
The way xml scans the package 2. General form of injection through annotations
In general, the injection Bean has a most straightforward, the most easy to understand way to achieve injection, the following nonsense first say, first paste the code.
2.1.Bean class public class MyBean {} 2.2, configuration class / / create a class configuration file @ Configurationpublic class MyConfiguration {/ / hand a Bean to Spring to manage @ Bean public MyBean myBean () {configuration ();}} 2.3, Test class
Unlike xml, here in Test, what is instantiated is no longer the ClassPathXmlApplicationContext, but the acquired AnnotationConfigApplicationContext instance.
ApplicationContext context = new AnnotationConfigApplicationContext (MyConfiguration.class); MyBean myBean = cotext.getBean ("myBean", MyBean.class); System.out.println ("myBean =" + myBean)
MyBean in the above code is a Bean that we need Spring to manage, it is just a simple class. In MyConfiguration, we first mark the class with the @ Configuration annotation to indicate that the class is a configuration class of a Spring and will load it when the configuration is loaded.
In MyConfiguration, we can see that a method returns an instance of MyBean, and the method is annotated with @ Bean, indicating that this is a method for injecting Bean, which will inject the Bean returned below into IOC.
3. Inject Bean through construction method
When we generate an instance of Bean, we can inject the Bean implementation using the constructor of Bean. Look directly at the code.
3.1.Bean class @ Component public class MyBeanConstructor {private AnotherBean anotherBeanConstructor; @ Autowired public MyBeanConstructor (AnotherBean anotherBeanConstructor) {this.anotherBeanConstructor = anotherBeanConstructor;} @ Override public String toString () {return "MyBean {" + "anotherBeanConstructor=" + anotherBeanConstructor +'}' } 3.2,AnotherBean class @ Component (id of value= "Bean, default is class name Little Hump") public class AnotherBean {} 3.3,configuration class @ Configuration @ ComponentScan ("com.company.annotationbean") public class MyConfiguration {}
Here we can find that the code injected in the usual way is different, let's take a look at what the new annotations mean:
@ AutoWired
Simple and rude, which translates directly to automatic assembly: wrench:, does not understand why it is called automatic assembly: wrench:? You'll see after reading the explanation of the next note. If you specify an id of Bean when injecting here, you will use the @ Qualifier annotation
@ Component (default singleton mode)
What?? This is translated as a part. Why does it feel like repairing a car? Yes, the way Spring manages Bean is the way it repairs cars. We add the annotated part @ Conmonent when we need to turn a class into a Bean that Spring can inject, so we can assemble it like a part when loading Bean: wrench: to this IOC car
Here we have several other annotations that can also implement this function, namely the refined @ Component:
@ Controller is annotated at the Controller layer
@ Service is annotated at the Service layer
@ Repository is annotated at the dao layer
@ ComponentScan ("")
Or translation, parts scanning, let's see which "parts" (classes) need to be loaded in the "parts warehouse" in parentheses, Spring will scan the package and inject all the classes marked @ Component in it.
The injection through the construction method here is easy to understand. When we were assembling the part MyBean, we suddenly found that it had to be installed in IOC on the basis of AnotherBean, so we automatically assembled it every time we assembled MyBean: wrench: an AnotherBean goes in. For example: chestnut:
Or take cars as an example, do we have to start before we step on the accelerator? The content of the AutoWired here is like starting. If you don't start, it's useless for you to step on the throttle and he won't go.
4. Bean is injected by set method.
We can inject the Bean implementation in the set method of a property. Look at the code.
MyBean class @ Component public class MyBeanSet {private AnotherBean anotherBeanSet; @ Autowired public void setAnotherBeanSet (AnotherBean anotherBeanSet) {this.anotherBeanSet = anotherBeanSet;} @ Override public String toString () {return "MyBeanSet {" + "anotherBeanSet=" + MyBean +'}';}} 4.2, configuration class and Test class
If it is the same as the previous one, it will not be posted.
Here we find that we have a @ AutoWired on the setter method, unlike above, we don't automatically assemble when we instantiate the class: wrench: this object, but when we explicitly call setter.
5. Use attributes to inject Bean
Our first two ways of injection, such as different times and more code, if through attributes, that is,
@ Component public class MyBeanProperty {@ Autowired private AnotherBean anotherBeanProperty; @ Override public String toString () {return "MyBeanProperty {" + "anotherBeanProperty=" + anotherBeanProperty +'}';}}
Here we can see that we need to use the instance object AnotherBean in our class, and we can automatically assemble it through @ AutoWired.
6. Inject Bean6.1 through List, MyBeanList class @ Component public class MyBeanList {private List stringList; @ Autowired public void setStringList (List stringList) {this.stringList = stringList;} public List getStringList () {return stringList;}} 6.2, MyConfiguration class @ Configuration @ ComponentScan ("annoBean.annotationbean") public class MyConfiguration {@ Bean public List stringList () {List stringList = new ArrayList () StringList.add ("List-1"); stringList.add ("List-2"); return stringList;}}
Here we inject the MyBeanList, and the elements in the List are injected one by one. Here is another way to inject List
6.3.MyConfiguration class @ Bean// sets the priority of Bean injection through this annotation, not necessarily the consecutive number @ Order (34) public String string1 () {return "String-1";} @ Bean@Order (14) public String string2 () {return "String-2";}
Injecting the same type as the generics in List will automatically match the type, even if there is no sense of List here, just the type of String, but he will inject it through List's Bean.
The priority of the second method is higher than the first. If you want to force the use of the first method when both exist, you can specify the id of the Bean.
7. Use Map to inject Bean @ Component public class MyBeanMap {private Map integerMap; public Map getIntegerMap () {return integerMap;} @ Autowired public void setIntegerMap (Map integerMap) {this.integerMap = integerMap;}} @ Bean public Map integerMap () {Map integerMap = new HashMap (); integerMap.put ("map-1", 1); integerMap.put ("map-2", 2) Return integerMap;} @ Bean public Integer integer1 () {return 1;} @ Bean public Integer integer2 () {return 2;}
There are also two ways to inject Map type Bean, and the second has a higher priority than the first.
The above is all the content of the article "how to inject SpringBoot into Bean through annotations". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.