Spring's IoC

   Spring 's IOC container explanation

       Inversion of Control IoC (Inversion of Control): IOC is a programming idea, mainly to coordinate the mutual dependencies between components. Ioc is to control the dependencies between business objects by the container, rather than directly controlled by the code in the traditional way. The essence of the inversion of control is that the control right is transferred from the application code to the external container, and the transfer of the control right is the so-called Inversion, the benefit of the transfer of control rights is to reduce the degree of dependence between business objects, that is, to achieve decoupling.

       There are three types of object creation: self-created, factory pattern creation, and external injection. Corresponds to three verbs: new , get , set

 
Spring IoC annotations
Spring's dependency configuration method is designed to be loosely coupled with the core of the Spring framework itself. However, until Spring 3.0, using XML for dependency configuration was pretty much the only option. The emergence of Spring 3.0 has changed this situation, which provides a series of annotations for dependency injection, which makes Spring IoC a viable alternative to XML files. The following describes how to use these annotations for dependency configuration management.
I divided annotations into two categories, the first for attribute assembly and the second for class registration.
There are generally two types of annotations used in attribute assembly, Autowired and Resource.
 
@Autowired
1. @Autowired is injected according to the type matching method (byType) by default
3. @Autowired annotation can be used for member variables, setter methods, constructor functions, etc.
4. The @Autowired annotation must have and only one matching bean. When no matching bean is found or there are multiple matching beans, the Spring container will throw an exception
5. Spring allows us to specify the name of the injected bean through the @Qualifier annotation. When @Autowired is used in combination with @Qualifier, the auto-injection strategy is changed from byType to byName
 
@Resource
1. The role of @Resource is equivalent to @Autowired, except that @Autowired is automatically injected according to byType, and @Resource is automatically injected according to byName by default.
2. @Resource has two attributes, namely name and type. Spring parses the name attribute of the @Resource annotation as the name of the bean, and the type attribute parses it as the type of the bean. So if the name attribute is used, the automatic injection strategy of byName is used, and the automatic injection strategy of byType is used when the type attribute is used.
When a class needs to be registered in the IoC container, you can use @Component, @Repository, @Service and @Controller 
 
@Component
1. @Component is the general form of all Spring-managed components, while @Repository, @Service, and @Controller are refinements of @Component to represent more specific use cases (corresponding to persistence layer, service layer, and presentation, respectively). Floor)
2. For beans defined with the @Component annotation, the default name (id) is an unqualified class name starting with lowercase. For example, the bean name defined by the UserDao class is userDao. You can also specify the name of the bean: @Component("abc")

 

The form of annotation configuration.  
First, you need to configure in the spring configuration file:
<context:component-scan base-package="com.boya.spring.ioc" />

     The base-package attribute of <context:component-scan /> specifies the class package to be scanned, and the annotations used by all classes in the class package and its recursive subpackages will be processed. 
   2. Add annotations to the class
UserDao class:
@Repository
public class UserDao {
    public String getName() {
        return "boya";
    }
}
ExampleBean class:
@Service
public class ExampleBean {
    @Resource
    @Value("boya")
    private String name;
    @Resource
    private UserDao userDao;
    
    public void print(){
        System.out.println("Name is :"+name);
    }
    
    public void userPrint(){
        System.out.println("User name is :"+userDao.getName());
    }
}
As mentioned earlier, the annotations @Autowired and @Service for assembly properties can be used for member variables, so when we set these two annotations on member variables, the setter method can be removed.
3. IoC container testing
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ExampleBean exampleBean = context.getBean("exampleBean", ExampleBean.class);
exampleBean.print();
exampleBean.userPrint();

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326618724&siteId=291194637