Spring understanding, spring
What can Struts and Hibernate Do?
Struts, the control layer solution in Mvc, can automatically encapsulate request data, type conversion, file upload, validation...
Hibernate is a persistent layer solution. You can save objects to the database and retrieve objects from the database.
Traditional development mode
Project development based on mvc mode;
Mvc-based project framework: Entity/dao/service/action
Problems:
1. Can an object be created and written to death?
2. Object creation details
Number of Objects
Multiple action [maintain member variables]
Service (public variables do not need to be maintained]
Dao (public variables do not need to be maintained]
Creation Time
Create during action access
Service creation at startup
Create dao at startup
3. Object dependency
Action depends on service
Service dependency dao
Summary:
Spring solves the above problems!
Simply put, it is to process the creation of an object and the dependency of the object!
Inversion on Control, Control reversal IOC
The creation of the object is handed over to the external container, and the control is reversed.
Dependency injection and dependency injection
Process object Dependencies
Control inversion solves the problem of object creation [object creation to others]
Dependency injection: After an object is created, the processing of the object relationship is dependency injection.Set partyMethodDependency Injection]
AOP
Aspect-Oriented Programming. In short, it can be understood as a class formed by a lot of repeated code.
Example: transactions, logs, and permissions;
Spring framework is a framework for object creation and dependency between objects.
It can also be used with other frameworks; Spring and Struts, Spring and hibernate (a framework for integration (bonding)
Spring provides an all-in-one solution:
1) Core features of Spring Core spring: IOC container, solving object creation and dependency
2) Spring Web Spring supports the web module.
A. It can be integrated with struts to allow struts action creation to be handed over to spring
B. spring mvc Mode
3) support for jdbc operations by Spring DAO Spring [JdbcTemplate template tool class]
4) Support for ORM by Spring orm spring:
A. It can be integrated with hibernate, [session]
B. You can also use spring to encapsulate hibernate operations.
5) Spring AOP aspect Programming
6) SpringEE spring's support for other javaEE modules
1. Import jar package
Commons-logging-1.1.3.jar log
Spring-beans-3.2.5.RELEASE.jar bean Node
Spring-context-3.2.5.RELEASE.jar spring context node
Spring-core-3.2.5.RELEASE.jar spring core functions
Spring-expression-3.2.5.RELEASE.jar spring expressions
2. Core configuration file: applicationContext. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Lead constraint --> <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"> <! -- IOC container configuration. All objects to be created are configured here --> <! -- An object is created using the class name reflection --> <bean id = "user" class = "service. User"> </bean> </beans>
3.
1 package service; 2 3 public class User { 4 private int id ; 5 private String name; 6 public int getId() { 7 return id; 8 } 9 public void setId(int id) {10 this.id = id;11 }12 public String getName() {13 return name;14 }15 public void setName(String name) {16 this.name = name;17 }18 19 }
4.
Spring IoC container is the core content of spring.
Purpose: create objects and process object Dependencies
1 package service; 2 3 import org. junit. test; 4 import org. springframework. beans. factory. beanFactory; 5 import org. springframework. beans. factory. xml. xmlBeanFactory; 6 import org. springframework. core. io. classPathResource; 7 import org. springframework. core. io. resource; 8 9 public class app {10 @ Test11 public void testIOC () {12 13 // User user = new User (); previously created object 14 // now the object is handed over to the IOC container of Spring, in applicationContext. configure 15 // load the configuration container in xml 16 Resource r = new ClassPathResource ("service/applicationContext. xml "); 17 // create an IOC container object, bean Factory 18 // IOC Container = factory class + applicationContext. xml19 BeanFactory f = new XmlBeanFactory (r); 20 // get the object created by the container 21 User user = (User) f. getBean ("user"); 22 23 System. out. println (user. getId (); 24} 25 26}
ApplicationContext. xml
IOC container creation object:
There are several ways to create an object:
1) call the non-parameter Constructor
2) parameter Constructor
3) create a factory object
Factory class, static method to create objects
Factory class, non-static method to create objects
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: p = "http://www.springframework.org/schema/p" 5 xmlns: context = "http://www.springframework.org/schema/context" 6 xsi: schemaLocation = "7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/ SC Hema/context10 http://www.springframework.org/schema/context/spring-context.xsd "> 11 12 <! -- ############## Object creation ###################--> 13 14 <! -- 1. Default No parameter constructor 15 <bean id = "user1" class = "cn. itcast. B _create_obj.User"> </bean> 16 --> 17 18 <! -- 2. --> 19 <bean id = "user2" class = "cn. itcast. B _create_obj.User "> 20 <constructor-arg index =" 0 "type =" int "value =" 100 "> </constructor-arg> 21 <constructor-arg index =" 1" type = "java. lang. string "value =" Jack "> </constructor-arg> 22 </bean> 23 24 <! -- Define a String with the value "Jack"; String s = new String ("jack") --> 25 <bean id = "str" class = "java. lang. string "> 26 <constructor-arg value =" Jacks "> </constructor-arg> 27 </bean> 28 <bean id =" user3 "class =" cn. itcast. B _create_obj.User "> 29 <constructor-arg index =" 0 "type =" int "value =" 100 "> </constructor-arg> 30 <constructor-arg index =" 1" type = "java. lang. string "ref =" str "> </constructor-arg> 31 </bean> 32 33 34 <! -- 3. Create an object for the factory class --> 35 <! -- #3.1 factory class, instance method --> 36 <! -- Create a factory first --> 37 <bean id = "factory" class = "cn. itcast. B _create_obj.ObjectFactory"> </bean> 38 <! -- When creating a user object, use the factory-side instance method --> 39 <bean id = "user4" factory-bean = "factory" factory-method = "getInstance"> </bean> 40 41 42 <! -- #3.2 factory class: Static Method --> 43 <! -- 44 class specifies the factory type 45 factory-method must be the "Static method" in the factory 46 --> 47 <bean id = "user" class = "cn. itcast. B _create_obj.ObjectFactory "factory-method =" getStaticInstance "> </bean> 48 </beans>
In Spring, how do I assign values to object attributes?[DI, dependency injection]
1) constructor
2) inject values to attributes using the set Method
3) p namespace
4) automatic assembly (understanding)
5) Annotation
Set Method
1 <! -- Dao instance --> 2 // create object 3 <bean id = "userDao" class = "cn. itcast. c_property.UserDao"> </bean> 4 5 <! -- Service instance --> 6 <bean id = "userService" class = "cn. itcast. c_property.UserService "> 7 <property name =" userDao "ref =" userDao "> </property> 8 </bean> 9 10 <! -- Action instance --> 11 <bean id = "userAction" class = "cn. itcast. c_property.UserAction "> 12 // process object dependency 13 <property name =" userService "ref =" userService "> </property> 14 </bean>