Overview of using Spring framework

For those who have just learned the spring framework or who have learned but forgot, looking back at this blog will help the
following code to be written under the Maven project:
create and test classes using the spring framework, when using Junit, you can choose to manually create the ApplicationContext Object

    private ApplicationContext applicationContext;
    accountService = applicationContext.getBean("accountService",
                AccountService.class);

A simple case of using spring:

Insert picture description here
It can be seen from this that after we use the spring framework, when we want to create an object, we don't need to use new again, so that it will be directly delivered to the spring framework and let it do this for us, which can reduce the degree of coupling.

You can also use spring's junit integration by using @RunWith() annotation to
eliminate the member variable applicationContext
Insert picture description here

Create bean annotation:

@Controller is used for the class on the control layer
@Service is used for the business layer operation
@Repository is used for the persistence layer operation
@Component Classes other than the three-tier architecture To be
precise, these four annotations are free on any class OK, and these four different annotations are created to distinguish the level of classification. The
four annotations have the same effect. They all let spring use reflection to create instantiated objects and add them to the IOC container.
Insert picture description here

After checking the source code of the annotations, I found that all four annotations have value attributes. The
Insert picture description here
value attribute annotations are translated as:
the value may indicate that the logical component name is recommended. If the component is automatically detected, it will be converted to a Spring bean.
When not assigning a value to the value, the id value of the bean is the current class name and the first letter is changed to lowercase. The
@Bean annotation is also to create a bean, but this annotation can only be placed on methods and annotations
. Use this annotation: Indicates the return value of the method As a bean object and adding it to the IOC container

@Bean annotation source code shows that it is different from the above four annotations
Insert picture description here

Dependency injection (DI)

When creating an instance object, if the class has one or more properties (or member variables) that need to be assigned, at this time, you can ask spring to do this for us.

Insert picture description here
Insert picture description here
It can be seen that the use of Autowired and Qualifier can achieve dependency injection, method parameters or member variables, and Qualifier can achieve the injection of specific marked beans.
Insert picture description here
The @ComponentScan annotation used in the above case
Insert picture description here
is equivalent to the role in the xml file. Is to inform spring to scan the package

Dynamic proxy

Use the newProxyInstance() method of the Proxy class to produce a dynamic proxy object, which can enhance and transform the method of the object ( on the basis of not changing the code of the original object )
Insert picture description here
. Example of use:
Insert picture description here
Test results: The
Insert picture description here
reason for introducing this dynamic proxy is because Related to AOP provided by spring

Five types of notifications

The front
and rear
exceptions are
finally

wrapped . The first four notifications can actually be contracted by surrounding notifications.
Similar to this, the
Insert picture description here
exception notification is in the catch block, and the final notification is in the finally block.

Suppose there is the following notification class
Insert picture description here

Insert picture description here

The operation result Insert picture description here
can be seen from the above figure, we can modify the method without changing the code of the implementation class method.

Guess you like

Origin blog.csdn.net/Valishment/article/details/107577350