Talk about spring’s understanding of IOC in detail (recommendation)
1、 IOC control inversion and di dependency injection
1. Control reversal can be literally understood as the transfer of initiative. Originally, the object in an application is actively created and instantiated through new, and the initiative created for the object is in the program code. Programs manage not only business logic, but also object creation and dependencies. This is very tiring, and it is not very consistent with the concept of "low coupling and high cohesion" in software engineering.
With spring's IOC container, the instantiation and dependency management of objects are uniformly managed by the IOC container. As long as the main class depends on the IOC container, the container will inject what it needs. That is, as long as the declared object does not need to take the initiative to new, the IOC container helps inject the corresponding object into the declared object to make it an instantiated object. (similar to the subject class, which provides a body, the IOC container injects the soul into it, turns it into a living body and activates him), so the initiative to create the object is transferred,
2、 Implementing IOC using XML configuration
1. Configure the beans of Dao implementation class and service class in the IOC container. These beans will be instantiated into memory when the container is loaded. (bean.xml configuration is as follows)
2. The service class needs to use an instance of Dao class (normally, a new Dao class object is required), but after taking over with the IOC container, you only need to declare the Dao interface object, and then write a set method of Dao object. (the object to be injected must have a set method, otherwise an error will be reported. Bean property 'bookdao' is not writable or has an invalid setter method) because spring injection is implemented according to the reflection mechanism, it will call the set method of the method name when reflecting the injection. If the set method is written incorrectly or not written at all, the injection will fail. (the bookservice class is as follows)
The above code: the bookservice class needs to use the bookdao object, but there is no new object. There is only one set method. This set method is the entry of IOC container injection (essential),
3. Here, we use ApplicationContext as the container, initialize the configuration file, and then take out the instantiated objects from the container according to the ID name.
4. Dao class and implementation class are as follows:
Interface class:
Implementation class:
5. Operation test results:
6. The general idea is as follows:
In the program, except that the new object is used to initialize the container, there is basically no new.
2、 Configuring IOC in annotation mode
The purpose of annotation configuration is the same as that of XML configuration, which is to create beans. Common notes are as follows:
@Component adds the @ Component annotation before the class definition. It will be recognized by the spring container and turned into a bean.
@The repository annotates Dao implementation classes (special @ component)
@Service is used to annotate the business logic layer (special @ component)
@Controller is used for control layer annotation, (special @ component)
Assembly notes are as follows:
@Autowired is assembled and injected by type by default. If you want to assemble by name, it should be used together with @ quapfier ("name"). You can use @ Autowired annotation without using the set method@ When Autowired annotation is automatically injected, there must be only one matching candidate bean in the spring container
@Name in quapfier ("name") is the name of the bean, that is, ID, and @ Autowired can be used as the name of the qualified configuration object
@By default, the resource assembles the injection by name. When the bean with the corresponding name cannot be found, it matches by type. If it still cannot be found, an error will be reported. @ Autowired is provided by spring and @ resource is provided by JavaEE. Using @ resource can reduce the dependence on spring
example:
1. The example is the same as above, except that the method of configuring beans is transferred from the XML file to the code and reflected with annotations.
2. In addition to changing the < bean id = "" class = "" / > in the configuration file into the corresponding annotation, another difference is that bean The modifications in the XML file need to be configured as follows before the annotation can take effect
Context can be configured as follows:
1. Scan only specific classes under specific packages:
This is to scan all classes beginning with B under the service package.
2. Use < context: include filter... / > And < context: Exclude Filter... / > Configure packages that require and do not require scanning
The above article on spring's understanding of IOC (recommendation) is all that Xiaobian shared with you. I hope it can give you a reference and support more programming tips.