Spring笔记2

1.  三种实例化bean方法:

    1)使用类构造器实例化

<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)使用静态工厂方法实例化,factory-method="createOrder":代表静态方法的方法名

<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)使用实例工厂方法实例化,先配一个bean的工厂类,然后再配bean,应用上一步配置的工厂类

<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"(默认)每个Spring Ioc容器中一个bean定义只有一个实例对象,默认情况下会在容器启动时初始化bean,但是我们可以指定bean节点的lazy-init="true"来延迟初始化bean,只有第一次获取bean对象才初始化bean。如:

<bean id='personService' class='cn.itcast.service.imp.PersonServiceBean' lazy-init="true"></bean>

如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init="true"。

    2)scope="prototype"每次从容器获取bean都是一个新的对象

3.    bean的初始化方法和销毁方法:(先实例化再执行初始化方法,即先执行构造方法,再执行init方法)

<bean id='personService' class='cn.itcast.service.imp.PersonServiceBean'  init-method="init" destroy-method="destory" ></bean>

    调用destory方法用下面的对象:

AbstractApplicationContext ctr = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
ctr.close();

4.    通过属性set方法注入(在运行期,由外部容器动态的将依赖对象注入到组件中)

    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)基本数据类型的注入,<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.    通过构造器的方法注入

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中的构造器
 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 }

 

posted @ 2012-12-07 17:26  柱柱  阅读(212)  评论(0编辑  收藏  举报