Introduction to SpringBoot @ Autowired Annotation Injection Rules

  • 2021-12-12 08:41:43
  • OfStack

Directory @ Autowired Annotation Injection Rule Verification Summary 1 Error that @ Autowired Annotation cannot be automatically injected

@ Autowired Annotation Injection Rules

@ Autowired-Injection defaults based on type, if no match, based on bean name

Default beanName generation rules for annotation mode in Spring:

In Spring, when we configure an bean, we can not specify name. In this case, Spring will generate a default beanName

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

2. Special cases-When the first and second letters of the class name are capitalized, the original class name will be used as beanName. CNService--CNService

Validation

1. Declare an service interface:


public interface UserService {
    void login();
}

2. The implementation class for the service interface, where the name bean is userServiceImpl


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

3. Write Controller and inject service


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

STEP 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 success input:

user login...

Modify the code for Step 3:


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

Also test successful input:

user login...

Because @ Autowired 1 is matched by type, there is only one implementation class for UserService interface in IoC container at this time, so it doesn't matter how the attribute name is written, it can be injected into it

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


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

At this time, the test reports an error:

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

Because one interface has multiple implementations, @ Autowired is searched according to the attribute name, that is, an bean with the name test is injected, but there is no bean with the name test in the IoC container, so an error is reported, and the attribute name can be matched by changing it to any one of the following


@Controller
public 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();
    }
}
 

Summary 1

If there is only one implementation class for 1.1 interfaces, it doesn't matter how the attribute name is written, because there is only one bean according to type matching

In the case of multiple implementations of 2.1 interfaces:

The attribute name corresponds to the component name 1, which can be specified at the time of declaration, such as @ Service ("abc")

② The attribute name is different from the component name, so specify the component name with @ Qualifier annotation

@ Autowired annotation error that cannot be injected automatically

When using springboot main method to start the project, I encountered that the annotation of [@ Autowdired] could not be injected. I checked a lot of information on the Internet, and it was also a sentence of a great god on the Internet:

Exception information:

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";
    }
}

Annotations are found to be normal:

The default rule for Bean assembly for the SpringBoot project is to scan from top to bottom according to the package location where the Application class is located! "Application class" refers to the SpringBoot project entry class.

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

Class Application:


@SpringBootApplication
public class Application  extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);     
    }

Related articles: