欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 前端技术 > javascript >内容正文

javascript

spring笔记2 -尊龙游戏旗舰厅官网

发布时间:2024/1/17 javascript 34 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 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)基本数据类型的注入,

    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>set1value> 12 <value>set2value> 13 <value>set3value> 14 set> 15 property> 16 <property name="lists"> 17 <list> 18 <value>list1value> 19 <value>list2value> 20 <value>list3value> 21 list> 22 property> 23 <property name="properties"> 24 <props> 25 <prop key="pro1">prov1prop> 26 <prop key="pro2">prov2prop> 27 <prop key="pro3">prov3prop> 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 sets = new hashset(); 20 21 list lists = new arraylist(); 22 23 properties properties = new properties(); 24 25 map maps = new hashmap(); 26 27 public set getsets() { 28 return sets; 29 } 30 31 public void setsets(set sets) { 32 this.sets = sets; 33 } 34 35 public list getlists() { 36 return lists; 37 } 38 39 public void setlists(list 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 getmaps() { 52 return maps; 53 } 54 55 public void setmaps(map 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 }

 

转载于:https://www.cnblogs.com/fanglove/archive/2012/12/07/2807312.html

总结

以上是尊龙游戏旗舰厅官网为你收集整理的spring笔记2的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图