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

What is the @ Autowired annotation injection rule for SpringBoot

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

Share

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

This article mainly explains "what are the @ Autowired annotation injection rules of SpringBoot". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the @ Autowired annotation injection rules of SpringBoot?"

@ Autowired comment injection rule

@ Autowired-injection defaults to type, and if it doesn't match, it depends on bean name.

Default beanName generation rules for annotations in Spring:

In Spring, when we configure a bean, we can not specify name, so that Spring will generate a default beanName

1. Hump form class name initials lowercase: UserService--userService

two。 Special case-when the first and second letters of the class name are capitalized, the original class name will be used as the beanName. CNService--CNService

Verification

1. Declare a service interface:

Public interface UserService {void login ();}

2. The implementation class of service interface. In this case, the name of bean is userServiceImpl.

@ Servicepublic class UserServiceImpl implements userService {@ Override public void login () {System.out.println ("user login...");}}

3. Write Controller and inject service

@ Controllerpublic class UserController {@ Autowired private UserService userService; public void userLogin () {userService.login ();}}

4. test

Public class AppTest {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); UserController controller = (UserController) context.getBean ("userController"); controller.login ();}}

Test successful input:

User login...

Modify the code of step 3:

@ Controllerpublic class UserController {@ Autowired private UserService test; public void userLogin () {test.login ();}}

The same test was successfully entered:

User login...

Since @ Autowired is matched by type first, there is only one implementation class for the UserService interface in the IoC container, so it doesn't matter how to write the attribute name, it can be injected into it.

5. Add an implementation class, where the name of bean is userServiceImpl2

@ Servicepublic class UserServiceImpl2 implements userService {@ Override public void login () {System.out.println ("user login...2");}}

At this point, the test reports an error:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:

Because an interface has multiple implementations, @ Autowired looks for it according to the attribute name, that is, to find a bean injection whose name is test. However, the IoC container does not have a bean named test, so an error is reported. Change the attribute name to any of the following to match it.

@ Controllerpublic class UserController {@ Autowired private UserService userServiceImpl; @ Autowired private UserService userServiceImpl2; @ Autowired @ Qualifier ("userServiceImpl") private UserService test; public void userLogin () {userServiceImpl.login (); userServiceImpl2.login (); test.login ();}} summarize

1. When there is only one implementation class for an interface, it doesn't matter how the attribute name is written, because there is only one bean by type matching.

two。 In the case of multiple implementations of an interface:

The ① attribute name is the same as the component name, which can be specified when declared, such as @ Service ("abc").

The ② attribute name is not consistent with the component name. Specify the component name with the @ Qualifier annotation

@ Autowired comment cannot be automatically injected

When using the springboot main method to start the project, we encountered that the [@ Autowdired] annotation could not be injected. After looking up a lot of information on the Internet, it was a sentence from a great god on the Internet to make sense:

Exception message:

Action:Consider defining a bean of type 'com.boot.app.service.bootService' in your configuration.

Controller layer:

RestController @ Autowired private BootService bootService; @ RequestMapping (value= "/ query", method=RequestMethod.POST) public String queryByStatus () {return "query";}}

It is found that the comments are normal:

The default rule for Bean assembly for SpringBoot projects is to scan from top to bottom based on the package location of the Application class! "Application class" refers to the SpringBoot project entry class.

The location of this class is critical: if the package of the Application class is: com.boot.app, only the com.boot.app package and all its subpackages will be scanned, and if the service or dao package is not under com.boot.app and its subpackages, it will not be scanned! That is, it is critical for com.boot.Application to know that the Application class is placed at the parent of the package where dao and service are located.

Application class:

@ SpringBootApplicationpublic class Application extends SpringBootServletInitializer {public static void main (String [] args) {SpringApplication.run (Application.class, args);} Thank you for reading. The above is the content of "what are the @ Autowired annotation injection rules of SpringBoot". After the study of this article, I believe you have a deeper understanding of what the @ Autowired annotation injection rules of SpringBoot are, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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