Summary: many people Spring: (Spring) Spring has brought Spring to the software industry. In 2002, Rod Jahnson first launched the prototype interface21 framework of Spring framework. On March 24, 2004, the Spring framework was redesigned based on the interface 21 framework and released the official version of 1.0. It's hard to imagine Rod Johnson's degree. He is a doctor at the University of Sydney. However, his major is not computer, but musicology.
Spring concept: make the existing technology more practical. Itself is a hodgepodge, integrating the existing framework technology
1, spring's original intention
1. JAVA EE development should be simpler.
2. Using interfaces instead of classes is a better programming habit. Spring reduces the complexity of using interfaces to almost zero.
3. It provides a better application configuration framework for JavaBean s.
4. More emphasis on object-oriented design than current technologies such as JAVA EE.
5. Minimize unnecessary exception capture.
6. Make the application easier to test.
2, spring's goal
1. Spring can be used conveniently and happily.
2. The application code does not depend on Spring APIs.
3. Spring does not compete with existing solutions, but is committed to integrating them.
Basic components of Spring:
1. The most perfect lightweight core framework.
2. Common transaction management abstraction layer.
3. JDBC abstraction layer.
4. Toplink, Hibernate, JDO, and iBATIS SQL Maps are integrated.
5. AOP function.
6. Flexible MVC Web application framework.
3, Advantages of spring
1) It facilitates decoupling and simplifies development. Through the IoC container provided by Spring, the dependencies between objects can be controlled by Spring to avoid excessive coupling caused by hard coding. Users do not have to write code for the very low-level requirements such as singleton pattern class and attribute file parsing, and can focus more on the upper-level applications.
2) AOP programming supports aspect oriented programming through the AOP function of Spring. Many functions that are not easy to be realized with traditional OOP can be easily realized through AOP.
3) The support of declarative transaction can free us from the monotonous and boring transaction management code, flexibly manage transactions through declarative way, and improve the development efficiency and quality.
4) Convenient program testing can use container independent programming to do almost all the testing work. Testing is no longer an expensive operation, but a thing to do at hand.
5) It is convenient to integrate various excellent frameworks. Spring supports various excellent frameworks (Struts, Hibernate, Hessian, Quartz, etc.).
6) Reduce the difficulty of using Java EE APIs. Spring has a thin encapsulation layer for Java EE APIs (such as JDBC, JavaMail, remote call, etc.), which greatly reduces the difficulty of using these APIs.
7) Java source code is a classic learning example. Spring's source code has exquisite design, clear structure and unique ingenuity. It everywhere reflects the master's flexible use of Java design patterns and his profound attainments in Java technology. Its source code is not intended to be an example of best practices in Java technology.
4, Use of spring
1. Add corresponding dependencies
<!--Introduce dependency:--> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.9.RELEASE</version> </dependency> </dependencies>
2. Create a spring configuration file---- Spring container.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--bean:Indicates that the class is handed over to spring Container to manage. id:Unique representation. Make it easy for others to pass through the id Find the corresponding object with the value of. class:Represents the full class name --> <bean id="hello01" class="com.ykq.HelloWorld"></bean> </beans>
3. Test
public static void main(String[] args) { //1. Read the spring configuration file. ApplicationContext app=new ClassPathXmlApplicationContext("spring01.xml"); //2. Get the specified object from the container. HelloWorld hello01 = (HelloWorld) app.getBean("hello01"); //3. Call the corresponding member in the class through the object hello01.show(); hello01.print(); }
5, Dependent injection
-
Dependency injection (DI).
-
Dependency: the creation of a Bean object depends on the container. The dependent resources of a Bean object
-
Injection: refers to the resources that the Bean object depends on, which are set and assembled by the container
6, Injection mode
1. Inject through set method
<bean id="userService" class="com.ykq.service.UserService"> <!--property:Assign a value to a property in the class by set Method assignment. name:Represents the property name. ref:Reference to value --> <property name="userDao" ref="mysql"/> </bean>
2. Through the construction method
<bean id="userService" class="com.ykq.service.UserService"> <!--Inject attributes into the class by constructing methods:Subscript of parameter--> <!-- <constructor-arg index="0" ref="oracle"/>--> <!--Inject attributes into the class by constructing methods:Parameter name--> <!-- <constructor-arg name="userDao" ref="mysql"/>--> <constructor-arg type="com.ykq.dao.UserDao" ref="mysql"/> </bean>
3. Injected data type
(1) Basic data type or string.
(2) Reference type -- object type.
(3) Set List,Set.
(4) map collection.
(5) Array
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="clazz01" class="com.ykq.di.Clazz"> <!--Used when injecting values for basic and string types value--> <property name="cid" value="145"/> <property name="cname" value="QY145"/> </bean> <bean id="stu" class="com.ykq.di.Student"> <property name="name" value="youname"/> <property name="age" value="25"/> <!--If the property type is object type. ref--> <property name="clazz" ref="clazz01"/> <!--Property type is list type <list></list>--> <property name="list"> <list> <value>name</value> </list> </property> <property name="set"> <set> <value>name</value> </set> </property> <!--map Attribute type--> <property name="map"> <map> <entry key="name" value="name"/> <entry key="age" value="age"/> <entry key="sex" value="sex"/> </map> </property> <property name="arr"> <array> <value>110</value> <value>120</value> <value>130</value> <value>119</value> </array> </property> </bean> </beans>