pringIoC详解下篇文档格式.docx
《pringIoC详解下篇文档格式.docx》由会员分享,可在线阅读,更多相关《pringIoC详解下篇文档格式.docx(7页珍藏版)》请在冰豆网上搜索。
![pringIoC详解下篇文档格式.docx](https://file1.bdocx.com/fileroot1/2022-11/27/dfb2073f-3f4a-431e-9064-d98453cc402c/dfb2073f-3f4a-431e-9064-d98453cc402c1.gif)
applicationContext.xml"
);
TelePhonet=ac.getBean("
TelePhone.class);
t.show();
}
org.junit.Testpublicvoidtest5(){ApplicationContextac=newClassPathXmlApplicationContext("
bean对应的java类TelePhone.java
/**
*手机类,模拟构造器注入
*authorhaifeng
*
*/publicclassTelePhone{privateStringcpu;
privateStringram;
publicTelePhone(){
super();
publicTelePhone(Stringcpu,Stringram){
this.cpu=cpu;
this.ram=ram;
publicvoidshow(){
System.out.println("
TelePhone[cpu:
"
+cpu+"
ram:
+ram+"
]"
}
/***手机类,模拟构造器注入*authorhaifeng**/publicclassTelePhone{privateStringcpu;
publicTelePhone(){super();
}publicTelePhone(Stringcpu,Stringram){this.cpu=cpu;
}publicvoidshow(){System.out.println("
}}
SET方式注入对象:
注入的对象需要在beans中声明,使用ref进行注入
--声明puter-->
student"
model.Student"
propertyname="
name"
haydn"
/property>
puter"
ref="
publicvoidtest6(){
Studentstu=ac.getBean("
Student.class);
stu.show();
org.junit.Testpublicvoidtest6(){ApplicationContextac=newClassPathXmlApplicationContext("
bean的对应JAVA类,Student.java
*student实体类,模拟注入类对象
*/publicclassStudent{privateStringname;
privateputerputer;
privateTelePhonephone;
publicvoidsetName(Stringname){
this.name=name;
publicvoidsetputer(puterputer){
this.puter=puter;
publicvoidsetPhone(TelePhonephone){
this.phone=phone;
System.out.println("
学生XX:
+name);
puter.show();
phone.show();
/***student实体类,模拟注入类对象*authorhaifeng**/publicclassStudent{privateStringname;
publicvoidsetName(Stringname){this.name=name;
}publicvoidsetputer(puterputer){this.puter=puter;
}publicvoidsetPhone(TelePhonephone){this.phone=phone;
}publicvoidshow(){System.out.println("
Listsetmappropetries注入
--注入集合-->
message"
bean.Message"
--字符串注入null值,方式一:
不写该配置,方式2:
不写value,加个null标签-->
>
null/>
age"
23"
--注入list-->
friends"
list>
value>
摩严<
/value>
白子画<
杀阡陌<
花千骨<
洛十一<
糖宝<
/list>
--注入set-->
cities"
set>
XX<
/set>
--注入MAP-->
score"
map>
entrykey="
语文"
98"
/entry>
数学"
99"
英语"
100"
/map>
--注入配置文件-->
dbParams"
props>
propkey="
url"
localhost<
/prop>
username"
root<
password"
123<
/props>
--表达式注入,类似EL表达式用#{bean对象名.属性}标记,-->
#{dbProps.password}"
--spring标签注入,其他bean中使用ref调用,setMapPropties都可以,使用时需要加入命名空间-->
util:
listid="
someList"
/util:
--加载properties文件创建Properties对象,location="
classpath:
dbcp.properties"
location表示指定文件路径
classpath表示绝对路径-->
propertiesid="
dbProps"
location="
properties>
location表示指定文件路径classpath表示绝对路径-->
测试类JAVA代码:
publicvoidtest7(){
Messagemsg=ac.getBean("
Message.class);
msg.show();
org.junit.Testpublicvoidtest7(){ApplicationContextac=newClassPathXmlApplicationContext("
bean的对应JAVA类,Message.java
*消息类,模拟各种类型对象注入
*/publicclassMessage{privateStringname;
privateintage;
privateList<
String>
friends;
privateSet<
cities;
privateMap<
String,Integer>
score;
privatePropertiesdbParams;
privateStringpassword;
publicvoidsetAge(intage){
this.age=age;
publicvoidsetFriends(List<
friends){
this.friends=friends;
publicvoidsetCities(Set<
cities){
this.cities=cities;
publicvoidsetScore(Map<
String,Integer>
score){
this.score=score;
publicvoidsetDbParams(PropertiesdbParams){
this.dbParams=dbParams;
publicvoidsetPassword(Stringpassword){
this.password=password;
Message[name:
+name+"
age:
+age+"
friends"
+
friends.toString()+"
cities:
+cities.toString()+"
score:
+score.toString()+
"
properties:
+dbParams.toString()+"
password:
+password+"
/**
*map的遍历
*/publicvoidshowMap(){
Set<
Entry<
map=score.entrySet();
for(Entry<
e:
map){
System.out.println(e.getKey()+e.getValue());
*protries的遍历
*/publicvoidshowProp(){
Object>
keys=dbParams.keySet();
for(Objecto:
keys){
System.out.println(dbParams.get(o));
/***消息类,模拟各种类型对象注入*authorhaifeng**/publicclassMessage{privateStringname;
}publicvoidsetAge(intage){this.age=age;
}publicvoidsetFriends(List<
friends){this.friends=friends;
}publicvoidsetCities(Set<
cities){this.cities=cities;
}publicvoidsetScore(Map<
score){this.score=score;
}publicvoidsetDbParams(PropertiesdbParams){this.dbParams=dbParams;
}publicvoidsetPassword(Stringpassword){this.password=password;
+friends.toString()+"
+score.toString()+"
}/***map的遍历*/publicvoidshowMap(){Set<
map){System.out.println(e.getKey()+e.getValue());
}}/***protries的遍历*/publicvoidshowProp(){Set<
keys){System.out.println(dbParams.get(o));
}}}
自动装配(bean对象注入简配)
autowire:
byName:
按名称匹配,beanid名与setXXX名字匹配植入,否则不注入
byType:
按类型匹配,bean的class与类型待注入的bean属性类型一致时注入,否则不注入
--自动装配,autowire:
byName根据名称,byType根据类型,不写默认会自动装配,但可读性很差-->
--自动装配的bean的id需要和待注入的bean的属性name一致,注入的名字需要和set方法的名字一致(不含set,首字母小写)-->
--byType和名字没关系,根据类型自动装配-->
--但是byType的bean中有相同类型的bean出现则会出错,而且还要以单例模式声明要注入的bean-->
autowire="
byName"