Spring notes (1), Spring notes (
Spring framework:
Reference: http://www.cnblogs.com/cyjch/archive/2012/02/06/2340415.html
The Spring framework depends on two jar packages: spring. jar and commons-logging.jar.
Added Junit. jar to facilitate testing.
I. A simple example:
Interface Class:
1 package com.imooc.myinterface;2 3 public interface PersonInterface {4 void show();5 }
Implementation class:
1 package com. imooc. mybean; 2 3 import com. imooc. myinterface. personInterface; 4 5/** 6 * UserBean implements the PersonInterface interface 7 */8 public class UserBean implements PersonInterface {9 @ Override10 public void show () {11 System. out. println ("UserBean implements the show method of the PersonInterface interface"); 12} 13}
Configure the bean of the implementation class in spring. xml:
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:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd10 http://www.springframework.org/schema/aop11 http://www.springframework.org/schema/aop/spring-aop.xsd12 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">13 14 15 <bean id="userBean" class="com.imooc.mybean.UserBean"></bean>16 17 </beans>
Test code:
1 package com.imooc.test; 2 3 import com.imooc.myinterface.PersonInterface; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class TestBean { 9 @Test10 public void testUserBean(){11 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");12 PersonInterface person = (PersonInterface) context.getBean("userBean");13 person.show();14 }15 }
Test code output:
1. UserBean implements the show method of the PersonInterface interface.
Ii. Three Methods of Bean instantiation:
Class constructor, static factory, and instance factory instantiate bean.
BeanFactory class:
1 package com. imooc. factory; 2 3 import com. imooc. mybean. userBean; 4 import com. imooc. myinterface. personInterface; 5 6 public class BeanFactory {7 8 // use static factory instantiation use 9 public static PersonInterface UserBeanService () {10 return new UserBean (); 11} 12 13 public PersonInterface getUserBeanService () {14 return new UserBean (); 15} 16}
Spring. xml:
1 <! -- Use the class constructor to directly instantiate --> 2 <bean id = "userBean" class = "com. imooc. mybean. UserBean"> </bean> 3 <! -- Use static factory instantiation --> 4 <bean id = "userBean2" class = "com. imooc. factory. beanFactory "factory-method =" UserBeanService "> </bean> 5 <! -- Instance factory instantiation --> 6 <bean id = "factory" class = "com. imooc. factory. beanFactory "> </bean> 7 <bean id =" userBean3 "factory-bean =" factory "factory-method =" getUserBeanService "> </bean>
Iii. Bean node information:
By default, the Spring IOC container will initialize the bean at startup, but you can setBean lazy-init = "true"To delay bean startup.The bean is initialized only when the bean is obtained for the first time..
If you want to set the initial latency for all beans, you can set default-lazy-init = "true" in the root node beans ".
The initialization method can add the init_method = "init" attribute to the bean, where init is a method of the bean, and the destory-method attribute should exist.
Get the instance through the getBean method in Spring, You can use "=" to test whether it is one or more instances.
1 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");2 PersonInterface person = (PersonInterface) context.getBean("userBean3");3 PersonInterface person2 = (PersonInterface) context.getBean("userBean3");4 person.show();5 System.out.println(person == person2);
Result:
1 UserBean implements the show method 2 true of the PersonInterface Interface
GetBean is called twice, but only once: UserBean implements the show method of the PersonInterface interface, and returns true to indicate that it is an instance.
After Bean is handed over to the container, it is a single instance by default. How can I get a new instance object?
Scope attributes:
Make scope = "prototype.
Iv. Core Mechanism of Spring --- dependency Injection)
Dependency injection organizes beans and beans directly through the configuration file;
Dependency injection and Inversion of Control are the same concept;
Reference: http://www.cnblogs.com/xdp-gacl/p/4249939.html
When object A needs to use object B, object A is dependent on object B. Traditionally, we have B (the initiative is on our own );
However, the job of creating B in Spring is completed by the Spring container and then injected to A (control in the Spring container)
Control reversal: control of the created object is transferred (self --> spring container)
What is reversed? The method for obtaining dependent objects is reversed !!!
UserDao interface:
1 package com.imooc.myinterface;2 3 public interface UserDao {4 void show();5 }
Implementation class of UserDao: UserDao4MysqlImpl and UserDao4OracleImpl
1 package com.imooc.impl; 2 3 import com.imooc.myinterface.UserDao; 4 public class UserDao4MysqlImpl implements UserDao { 5 @Override 6 public void show() { 7 System.out.println("UserDao4MysqlImpl"); 8 } 9 }10 11 12 package com.imooc.impl;13 14 import com.imooc.myinterface.UserDao;15 16 public class UserDao4OracleImpl implements UserDao{17 @Override18 public void show() {19 System.out.println("UserDao4OracleImpl");20 }21 }
UserService interface:
1 package com. imooc. impl; 2 3 import com. imooc. myinterface. userDao; 4 import com. imooc. myinterface. userService; 5 6/** 7 * the implementation of the UserService interface calls the show method of userDao. Generally, You need to instantiate a UserDao object: 8 * UserDao userDao = new UserDao4MysqlImpl (); 9 * This will increase coupling; 10 * you can configure spring. xml injection. 11 */12 public class UserServiceImpl implements UserService {13 private UserDao userDao; // private, used to inject 14 15 @ Override16 public void show () {17 userDao. show (); 18} 19 20 public UserDao getUserDao () {21 return userDao; 22} 23 24 public void setUserDao (UserDao userDao) {25 this. userDao = userDao; 26} 27}
Spring. xml:
1 <bean id="mysqlDao" class="com.imooc.impl.UserDao4MysqlImpl"></bean>2 <bean id="oracleDao" class="com.imooc.impl.UserDao4OracleImpl"></bean>3 <bean id="userService" class="com.imooc.impl.UserServiceImpl">4 <property name="userDao" ref="mysqlDao"></property>5 </bean>
Test code:
1 @Test2 public void testUserService(){3 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");4 UserService userService = (UserService) context.getBean("userService");5 userService.show();6 }
Result:
1 UserDao4MysqlImpl
In this way, the injection is implemented!