A Preliminary Study of Spring

Spring provides a lightweight solution for enterprise application development . The solution includes:
core mechanism based on dependency injection, declarative transaction management based on AOP, integration with multiple persistence layer technologies, and excellent MVC framework.

There are two core functions of Spring:
1. Create and manage all java objects (ie Bean, any java class can be used as Spring's Bean)
2. Manage dependencies between beans (through dependency injection)

Conceptual understanding:
Dependency injection (inversion of control):
Using the Spring framework, the caller does not need to actively obtain the dependent object, but only passively accepts the Spring container to assign values ​​to the caller's member variables (sub-elements are configured for the bean).
In this way, the program does not need to create new objects, but creates them through Spring, and only waits for the Spring container to inject when needed.

Two ways of dependency injection:
<bean id="id" class="impl.class">
     //Set value injection: first create a Bean instance through the no-parameter constructor, and then call the corresponding setter method to inject dependencies
     <property name="name" ref="param"/>
     //Construction injection: directly call the constructor with parameters, when the Bean is created, the injection of dependencies has been completed
     <constructor-arg value="param"/>
</bean>

Suggestion: Set the value as the primary, and the construction as the secondary. For injections that do not require changes in dependencies, use construction injection as much as possible; for others, consider setting injection.

Guess you like

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