This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
Instead of finding or creating other objects associated with them, objects are “assembled” by the container, which is the essence of dependency injection in Spring. We can do this by adding some annotations to the code or configuring the autowiring Bean:
- Create beans and register them in the container
- Automatically satisfy dependencies between beans
Create beans and register them in the container
To create a Bean that can be found in the container, first use the @Component (or @Controller, @Repository, or @Service) annotation on the corresponding class to call it a Spring Bean, which is called the class name. For example, we add the @Service annotation to the following implementation class
@Service
public class EmployeeServiceImpl implements EmployeeService {
// ...
}
Copy the code
See what classes are loaded in the Spring container
@Test public void getAllBeans() { String[] beans = applicationContext.getBeanDefinitionNames(); for (String beanName : beans) { Class<? > beanType = applicationContext.getType(beanName); System.out.println("BeanName:" + beanName); System.out.println("Bean type: "+ beanType); System.out.println("Bean package: "+ beanType.getPackage()); }}Copy the code
You get the following output
BeanName: employeeServiceImpl Bean types: class com. At meituan. Finance, the biz. Impl. EmployeeServiceImpl Bean's package: package com.meituan.finance.account.biz.implCopy the code
You can see that the Bean name defaults to the small camel name of the class name. We can also customize the Bean to be Named empService by using @service (“empService”) or Named(“empService”)
After beans are generated, we need to enable component scanning to be discovered and managed by the Spring container at startup. Using the @ComponentScan annotation, we can enable scanning of bean components, by default, for packages of the same class, or configured in XML
<context:component-scan base-package="com.meituan.finance.account"/>
Copy the code
Automatically satisfy dependencies between beans
Spring provides the @Autowried annotation for auto-assembly of beans, which can be used on properties, constructors, and set methods. When to start the spring IoC container automatically loaded with a rear AutowiredAnnotationBeanPostProcessor processor, When a container scanning to @ Autowied, @ the Resource (rear CommonAnnotationBeanPostProcessor processor processing) or @ Inject, will needs in the IoC container automatically find bean, When using @AutoWired, the properties that are assembled to the object are first queried in the container for the bean of the corresponding type
- If the query results in exactly one, the bean is assembled to the data specified by @AutoWired
- If the query yields more than one result, @AutoWired looks it up by name.
- If the result of the query is empty and required=false (more), an exception is thrown.
Spring officially recommends applying this to constructors. After defining a full-parameter constructor, annotate it with the @autowired annotation, which can actually be omitted:
When a class has only one constructor, you can omit the @autowired annotation.
Adding annotations to a constructor has the following advantages
- Allows the declaration of immutable fields, that is, using
final
Modification. - Easy to follow single responsibility design principles.
- The injection sequence is higher
Java variables are initialized in the following order:
- Static variable or static statement block
- Instance variable or initialization statement block
- A constructor
- Autowired Assembles objects