One, spring two core content
1, control reversal Ioc/di: The application itself is not responsible for the creation and maintenance of objects, objects and the creation of dependent objects are completely given to the container management.
2, AOP (aspect-oriented programming): Through the way of precompilation, in the run time through the way of dynamic agent to achieve a technology
(1) using AOP to manage transactions in a project, opening, committing, and rolling back transactions
(2) Manage the communication characteristics of the transaction in the project.
Second, the spring environment construction
1. Guide Package:
2. Create a spring configuration file Applicationcontext.xml
1 <?xml version= "1.0" encoding= "UTF-8"?> 2 3 4 5 6 http:// 7 8 9 "> </beans>
3. Creating beans
class= "Com.tx.model.Person" >
4. Create a spring container
1 @Test 2 Public void test1 () {3 New Classpathxmlapplicationcontext ("Applicationcontext.xml"); 4 Person P = (person) ctx.getbean ("person"); 5 System.out.println (p); 6 }
Iii. how beans are created
1. Create using constructors (common)
Creating a Bean,bean using the constructor must have a default constructor
2, the use of static Factory mode
Provide a static factory:
1 Public classFactorybean {2 /**3 * Must be a static method4 * @return5 */6 Public StaticUser CreateUser () {7 return NewUser ();8 }9}
To configure:
1 <!-- ID: uniquely identifies 2class : Static Factory classes 3 factory- method: Methods of the Static factory 4 --5class= "Com.rl.spring.factory.FactoryBean" factory-method= "CreateUser" ></bean >
3. Example Factory mode
Instance Factory:
Public class FactoryBean1 { public User createUser () { returnnew User () ; }}
Configuration:
1 <!--define instance factory by spring--2 class= "Com.rl.spring.factory.FactoryBean1" > </bean>3 <!-- Specify the bean4 factory-bean to create: Specify instance factory class,5 factory-Method: How to create a bean in a factory 6-- 7 <bean id= "user" factory-bean= " Factorybean "factory-method=" CreateUser "></bean>
4. Delayed loading
By default, all beans are not lazily loaded, and when the spring container is created, the bean is created and all Getbean can be removed directly. Can be controlled by Lazy-init , default is False, if set to true, the bean is created at Getbean
5. Scope of Bean
Scope:singleton Single-instance: can set delay load and non-lazy load
Scope:prototype multiple cases: only delay loading
6. Bean Life cycle
Consistent with containers, destruction of containers is destroyed
7. Dependency Injection:
(1), Constant injection
1 class= "Com.tx.model.Person" >2 <!--constant injected--3 <property name= " ID "value=" 1 "></property>4 <property name=" name "value=" Zhang San "></property>5 </bean>
(2), constructor injection
1<bean id= "User"class= "Com.rl.spring.model.User" >2<!--3 Index: The indexed order of the parameters of the construction method4 type: Types of parameters that construct the method (not required)5 Value : Values6-7<!--<constructor-arg index= "0" type= "Java.lang.Integer" value= "2"/>8<constructor-arg index= "1" type= "java.lang.String" value= "Zhang San"/>9<constructor-arg index= "2" type= "java.lang.String" value= "666"/>--Ten<constructor-arg index= "0" value= "2"/> One<constructor-arg index= "1" value= "Zhang San"/> A<constructor-arg index= "2" value= "666"/> -</bean>
(3), external bean injection, must provide a set method
1 class= "Com.tx.spring.dao.impl.UserDaoImpl" >2 <property name= "DataSource" ref= " DataSource "></property>3 </bean>4class=" Com.tx.spring.service.impl.UserServiceImpl ">5 <property name=" Userdao "ref=" Userdao "></ Property>
6 </bean>
(4), internal bean injection, must provide a set method
(5), set injection, must provide a set method
1<bean id= "CI"class= "Com.rl.spring.model.CollectionInjection" >2<property name= "Set">3<set>4<value>football</value>5<value>basketball</value>6</set>7</property>8<property name= "List">9<list>Ten<value>male</value> One<value>female</value> A</list> -</property> -<property name= "Map"> the<map> -<entry key= "Key1" value= "value1" ></entry> -<entry key= "Key2" value= "value2" ></entry> -</map> +</property> -<property name= "prop"> +<props> A<prop key= "Name" > Zhang San </prop> at<prop key= "Job" > Programmers </prop> -</props> -</property> -</bean>
8. Annotation Method Injection
(1), Primer package: Annocation Package, if 4.2 also need to introduce the AOP package
(2), introduce the constraint file spring-context-4.2.xsd
(3), open note driver
<!--drive--><context:annotation-config/> to open annotations
@Resource Note: Put on a property or on a set method. You do not need to provide a set method. The default is based on the name, if not according to the interface and implementation of the relationship between the class, if there are multiple implementation classes need to specify the name
@Autowired Note: Placed on the property, can also be placed on the set method, depending on the interface and the implementation of the relationship between the class to find, if more than one implementation class needs to match @Qulifier annotations to specify value
9. Spring Scanner annotations
@Controller: Class of the control layer
@Service: Classes for service Tiers
@Repository: Classes for the data tier
@Component: On classes that cannot be layered
Spring basic knowledge of environment building, bean creation, dependency injection, annotation injection