javascript
Spring笔记2
1. 三種實(shí)例化bean方法:
1)使用類構(gòu)造器實(shí)例化
<bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"></bean> package cn.itcast.service.imp;import cn.itcast.service.PersonService;public class PersonServiceBean implements PersonService {public void save(){System.out.println("我是save方法");} }2)使用靜態(tài)工廠方法實(shí)例化,factory-method="createOrder":代表靜態(tài)方法的方法名
<bean id="personService2" class="cn.itcast.service.imp.PersonServiceBeanFactory" factory-method="createOrder"></bean> package cn.itcast.service.imp;public class PersonServiceBeanFactory {public static PersonServiceBean createOrder(){return new PersonServiceBean();} }3)使用實(shí)例工廠方法實(shí)例化,先配一個(gè)bean的工廠類,然后再配bean,應(yīng)用上一步配置的工廠類
<bean id="personServiceFactory" class="cn.itcast.service.imp.PersonServiceBeanFactory"></bean> <bean id="personService3" factory-bean="personServiceFactory" factory-method="createOrder2"></bean> package cn.itcast.service.imp;public class PersonServiceBeanFactory {public static PersonServiceBean createOrder(){return new PersonServiceBean();}public PersonServiceBean createOrder2(){return new PersonServiceBean();} }2. bean的作用域:
1)scope="singleton"(默認(rèn))每個(gè)Spring Ioc容器中一個(gè)bean定義只有一個(gè)實(shí)例對象,默認(rèn)情況下會(huì)在容器啟動(dòng)時(shí)初始化bean,但是我們可以指定bean節(jié)點(diǎn)的lazy-init="true"來延遲初始化bean,只有第一次獲取bean對象才初始化bean。如:
<bean id='personService' class='cn.itcast.service.imp.PersonServiceBean' lazy-init="true"></bean>如果想對所有bean都應(yīng)用延遲初始化,可以在根節(jié)點(diǎn)beans設(shè)置default-lazy-init="true"。
2)scope="prototype"每次從容器獲取bean都是一個(gè)新的對象
3. bean的初始化方法和銷毀方法:(先實(shí)例化再執(zhí)行初始化方法,即先執(zhí)行構(gòu)造方法,再執(zhí)行init方法)
<bean id='personService' class='cn.itcast.service.imp.PersonServiceBean' init-method="init" destroy-method="destory" ></bean>調(diào)用destory方法用下面的對象:
AbstractApplicationContext ctr = new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); ctr.close();4. 通過屬性set方法注入(在運(yùn)行期,由外部容器動(dòng)態(tài)的將依賴對象注入到組件中)
1)對象的注入
package cn.itcast.service.imp;import cn.itcast.dao.PersonDao; import cn.itcast.service.PersonService;public class PersonServiceBean implements PersonService {PersonDao personDao;public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public void save(){personDao.save();}} <bean id="personDao" class="cn.itcast.dao.imp.PersonDaoImp"></bean> <bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"><property name="personDao" ref="personDao"></property> </bean>?或者
<bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"><property name="personDao"><bean class="cn.itcast.dao.imp.PersonDaoImp"></bean></property> </bean>2)基本數(shù)據(jù)類型的注入,<property name="name" value="itcast"></property>
3)集合類型的注入:
Spring的bean配置 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 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6 <bean id="personDao" class="cn.itcast.dao.imp.PersonDaoImp"></bean> 7 <bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"> 8 <property name="personDao" ref="personDao"></property> 9 <property name="sets"> 10 <set> 11 <value>set1</value> 12 <value>set2</value> 13 <value>set3</value> 14 </set> 15 </property> 16 <property name="lists"> 17 <list> 18 <value>list1</value> 19 <value>list2</value> 20 <value>list3</value> 21 </list> 22 </property> 23 <property name="properties"> 24 <props> 25 <prop key="pro1">prov1</prop> 26 <prop key="pro2">prov2</prop> 27 <prop key="pro3">prov3</prop> 28 </props> 29 </property> 30 <property name="maps"> 31 <map> 32 <entry key="map1" value="mapv1"></entry> 33 <entry key="map2" value="mapv2"></entry> 34 <entry key="map3" value="mapv3"></entry> 35 </map> 36 </property> 37 </bean> 38 </beans> 注入的組件類 1 package cn.itcast.service.imp; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Properties; 9 import java.util.Set; 10 11 import cn.itcast.dao.PersonDao; 12 import cn.itcast.service.PersonService; 13 14 public class PersonServiceBean implements PersonService { 15 PersonDao personDao; 16 17 String name; 18 19 Set<String> sets = new HashSet<String>(); 20 21 List<String> lists = new ArrayList<String>(); 22 23 Properties properties = new Properties(); 24 25 Map<String,String> maps = new HashMap<String,String>(); 26 27 public Set<String> getSets() { 28 return sets; 29 } 30 31 public void setSets(Set<String> sets) { 32 this.sets = sets; 33 } 34 35 public List<String> getLists() { 36 return lists; 37 } 38 39 public void setLists(List<String> lists) { 40 this.lists = lists; 41 } 42 43 public Properties getProperties() { 44 return properties; 45 } 46 47 public void setProperties(Properties properties) { 48 this.properties = properties; 49 } 50 51 public Map<String, String> getMaps() { 52 return maps; 53 } 54 55 public void setMaps(Map<String, String> maps) { 56 this.maps = maps; 57 } 58 59 public String getName() { 60 return name; 61 } 62 63 public void setName(String name) { 64 this.name = name; 65 } 66 67 public PersonDao getPersonDao() { 68 return personDao; 69 } 70 71 public void setPersonDao(PersonDao personDao) { 72 this.personDao = personDao; 73 } 74 75 public void save(){ 76 System.out.println(name); 77 personDao.save(); 78 } 79 80 } 測試類 1 package junit.test; 2 3 import java.util.Set; 4 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.AbstractApplicationContext; 8 import org.springframework.context.support.ClassPathXmlApplicationContext; 9 import org.springframework.context.support.FileSystemXmlApplicationContext; 10 11 import cn.itcast.service.PersonService; 12 13 import junit.framework.TestCase; 14 15 public class SpringTest extends TestCase { 16 17 @Test 18 public void test1(){ 19 AbstractApplicationContext ctr = new ClassPathXmlApplicationContext(new String[]{"beans.xml"}); 20 PersonService personService = (PersonService) ctr.getBean("personService"); 21 for(String value:personService.getSets()){ 22 System.out.println(value); 23 } 24 for(String value:personService.getLists()){ 25 System.out.println(value); 26 } 27 for(Object key :personService.getProperties().keySet()){ 28 System.out.println(key+"_"+personService.getProperties().getProperty((String) key)); 29 } 30 for(Object key :personService.getMaps().keySet()){ 31 System.out.println(key+"_"+personService.getMaps().get(key)); 32 } 33 } 34 }?5. 通過構(gòu)造器的方法注入
Spring的bean配置 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 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6 <bean id="personDao" class="cn.itcast.dao.imp.PersonDaoImp"></bean> 7 <bean id="personService" class="cn.itcast.service.imp.PersonServiceBean"> 8 <constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao"></constructor-arg> 9 <constructor-arg index="1" value="傳智博客"></constructor-arg> 10 </bean> 11 </beans> ServiceBean中的構(gòu)造器 1 package cn.itcast.service.imp; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.HashSet; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Properties; 9 import java.util.Set; 10 11 import cn.itcast.dao.PersonDao; 12 import cn.itcast.service.PersonService; 13 14 public class PersonServiceBean implements PersonService { 15 PersonDao personDao; 16 17 String name; 18 19 public PersonServiceBean(PersonDao personDao, String name) { 20 this.personDao = personDao; 21 this.name = name; 22 } 23 24 public void save(){ 25 System.out.println(name); 26 personDao.save(); 27 } 28 29 }?
轉(zhuǎn)載于:https://www.cnblogs.com/fanglove/archive/2012/12/07/2807312.html
總結(jié)
- 上一篇: E 和 ? extends E的区别
- 下一篇: HDU1054+最小顶点覆盖