文章目录 1.获取bean的两种方式 1.通过id获取bean(前面用过) 2.通过类型获取bean(单例时使用) 1.案例 2.代码 1.beans.xml 2.SpringBeanTest.java 3.结果 3.注意事项 2.三种基本依赖注入方式 1.通过属性配置bean(前面用过) 2.通过构造器配置bean 1.通过index配置 2.通过name配置 3.通过type配置 4.验证 3.通过p命名空间配置bean 1.配置 2.注意需要alt + enter引入命名空间定义 3.属性注入bean对象的两种方式 1.通过ref注入 1.dao/MemberDao.java 2.MemberDaoImpl.java 3.service/MemberServiceImpl.java 4.beans.xml 5.测试SpringBeanTest 6..解释 2.通过内部bean注入 4.不同类型的属性注入方式 1.List 1.Master.java 2.beans.xml 3.测试 2.Map 3.Set 4.Array 5.Properties 6.util:list 1.BookStore.java 2.beans.xml 3.测试 7.级联属性赋值 1.Dept.java 2.Emp.java 3.beans.xml 4.测试
1.获取bean的两种方式
1.通过id获取bean(前面用过)
2.通过类型获取bean(单例时使用)
1.案例
2.代码
1.beans.xml
< bean class = " com.sxs.spring.bean.Car" > < property name = " name" value = " 奔驰" /> < property name = " id" value = " 100" /> < property name = " price" value = " 10.2" /> </ bean>
2.SpringBeanTest.java
@Test public void getBeanByType ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Car car = ioc. getBean ( Car . class ) ; System . out. println ( car) ; }
3.结果
3.注意事项
2.三种基本依赖注入方式
1.通过属性配置bean(前面用过)
2.通过构造器配置bean
1.通过index配置
< bean class = " com.sxs.spring.bean.Car" id = " car1" > < constructor-arg index = " 0" value = " 100" > </ constructor-arg> < constructor-arg index = " 1" value = " 奔驰1" > </ constructor-arg> < constructor-arg index = " 2" value = " 300.12" > </ constructor-arg> </ bean>
2.通过name配置
< bean class = " com.sxs.spring.bean.Car" id = " car2" > < constructor-arg name = " id" value = " 200" > </ constructor-arg> < constructor-arg name = " name" value = " 宾利" > </ constructor-arg> < constructor-arg name = " price" value = " 2000000" > </ constructor-arg> </ bean>
3.通过type配置
< bean class = " com.sxs.spring.bean.Car" id = " car3" > < constructor-arg type = " java.lang.Integer" value = " 300" > </ constructor-arg> < constructor-arg type = " java.lang.String" value = " 宝马" > </ constructor-arg> < constructor-arg type = " java.lang.Double" value = " 2332233223" > </ constructor-arg> </ bean>
4.验证
@Test public void Construct ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Object car1 = ioc. getBean ( "car1" ) ; Object car2 = ioc. getBean ( "car2" ) ; Object car3 = ioc. getBean ( "car3" ) ; System . out. println ( car1) ; System . out. println ( car2) ; System . out. println ( car3) ; }
3.通过p命名空间配置bean
1.配置
< bean class = " com.sxs.spring.bean.Car" id = " car4" p: id = " 100" p: name = " 路虎" p: price = " 121212" />
2.注意需要alt + enter引入命名空间定义
3.属性注入bean对象的两种方式
1.通过ref注入
1.dao/MemberDao.java
package com. sxs. spring. dao ;
public interface MemberDao { public void add ( ) ;
}
2.MemberDaoImpl.java
package com. sxs. spring. dao ;
public class MemberDaoImpl implements MemberDao { @Override public void add ( ) { System . out. println ( "MemberDaoImpl的add方法被调用!" ) ; }
}
3.service/MemberServiceImpl.java
package com. sxs. spring. service ; import com. sxs. spring. dao. MemberDao ;
public class MemberServiceImpl { MemberDao memberDao; public void add ( ) { memberDao. add ( ) ; } public MemberDao getMemberDao ( ) { return memberDao; } public void setMemberDao ( MemberDao memberDao) { this . memberDao = memberDao; }
}
4.beans.xml
< bean class = " com.sxs.spring.dao.MemberDaoImpl" id = " memberDao" /> < bean class = " com.sxs.spring.service.MemberServiceImpl" id = " memberService" > < property name = " memberDao" ref = " memberDao" > </ property> </ bean>
5.测试SpringBeanTest
@Test public void refBean ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; MemberServiceImpl bean = ioc. getBean ( MemberServiceImpl . class ) ; bean. add ( ) ; }
6…解释
这里MemberDaoImpl有一个add方法,MemberServiceImpl需要调用这个方法,所以需要这个MemberDao类型的属性 通过spring反射创建MemberDaoImpl的实例(使用默认构造方法) 再通过spring反射创建MemberServiceImpl的实例(使用默认构造方法),然后将MemberDao这个属性也通过spring进行依赖注入 注意:进行依赖注入时如果是属性注入,需要给属性setter方法 在使用MemberServiceImpl的实例时,不能自己创建实例,因为自己创建的实例没有被依赖注入,是不能正常运行的!!!必须通过spring的ApplicationContext来获取被依赖注入的MemberServiceImpl对象
2.通过内部bean注入
beans.xml
< bean class = " com.sxs.spring.service.MemberServiceImpl" id = " memberService2" > < property name = " memberDao" > < bean class = " com.sxs.spring.dao.MemberDaoImpl" /> </ property> </ bean>
4.不同类型的属性注入方式
1.List
1.Master.java
package com. sxs. spring. bean ; import org. springframework. objenesis. instantiator. perc. PercInstantiator ; import java. util. * ;
public class Master { private String name; private List < Monster > monstersList; private Map < String , Monster > monsterMap; private Set < Monster > monsterSet; private String [ ] monsterName; private Properties pros; public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public List < Monster > getMonstersList ( ) { return monstersList; } public void setMonstersList ( List < Monster > monstersList) { this . monstersList = monstersList; } public Map < String , Monster > getMonsterMap ( ) { return monsterMap; } public void setMonsterMap ( Map < String , Monster > monsterMap) { this . monsterMap = monsterMap; } public Set < Monster > getMonsterSet ( ) { return monsterSet; } public void setMonsterSet ( Set < Monster > monsterSet) { this . monsterSet = monsterSet; } public String [ ] getMonsterName ( ) { return monsterName; } public void setMonsterName ( String [ ] monsterName) { this . monsterName = monsterName; } public Properties getPros ( ) { return pros; } public void setPros ( Properties pros) { this . pros = pros; } @Override public String toString ( ) { return "Master{" + "name='" + name + '\'' + ", monstersList=" + monstersList + ", monsterMap=" + monsterMap + ", monsterSet=" + monsterSet + ", monsterName=" + Arrays . toString ( monsterName) + ", pros=" + pros + '}' ; }
}
2.beans.xml
< bean class = " com.sxs.spring.bean.Master" id = " master" > < property name = " name" value = " list" /> < property name = " monstersList" > < list> < ref bean = " monster" /> < ref bean = " monster2" /> < bean class = " com.sxs.spring.bean.Monster" > < property name = " name" value = " 111" /> < property name = " skill" value = " 111" /> < property name = " monsterId" value = " 111" /> </ bean> </ list> </ property> </ bean>
3.测试
@Test public void List_ ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Master master = ioc. getBean ( "master" , Master . class ) ; System . out. println ( master) ; }
2.Map
1.beans.xml
< bean class = " com.sxs.spring.bean.Master" id = " master2" > < property name = " name" value = " map" /> < property name = " monsterMap" > < map> < entry> < key> < value> monster</ value> </ key> < ref bean = " monster" /> </ entry> < entry> < key> < value> monster1</ value> </ key> < bean class = " com.sxs.spring.bean.Monster" id = " monster1" > < property name = " name" value = " 222" /> < property name = " monsterId" value = " 222" /> < property name = " skill" value = " 222" /> </ bean> </ entry> </ map> </ property> </ bean>
2.测试
@Test public void Map_ ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Master master = ioc. getBean ( "master2" , Master . class ) ; System . out. println ( master) ; }
3.Set
1.beans.xml
< bean class = " com.sxs.spring.bean.Master" id = " master3" > < property name = " monsterSet" > < set> < ref bean = " monster" /> < bean class = " com.sxs.spring.bean.Monster" > < property name = " name" value = " 222" /> < property name = " monsterId" value = " 222" /> < property name = " skill" value = " 222" /> </ bean> </ set> </ property> </ bean>
2.测试
@Test public void Set_ ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Master master = ioc. getBean ( "master3" , Master . class ) ; System . out. println ( master) ; }
4.Array
1.beans.xml
< bean class = " com.sxs.spring.bean.Master" id = " master4" > < property name = " monsterName" > < array> < value> 小妖怪</ value> < value> 中妖怪</ value> < value> 大妖怪</ value> </ array> </ property> </ bean>
2.测试
@Test public void Array_ ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Master master = ioc. getBean ( "master4" , Master . class ) ; System . out. println ( master) ; }
5.Properties
1.beans.xml
< bean class = " com.sxs.spring.bean.Master" id = " master5" > < property name = " pros" > < props> < prop key = " username" > 111111</ prop> < prop key = " psssword" > 222222</ prop> < prop key = " ip" > 127.0.0.1</ prop> </ props> </ property> </ bean>
2.测试
@Test public void Properties_ ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Master master = ioc. getBean ( "master5" , Master . class ) ; System . out. println ( master) ; }
6.util:list
1.BookStore.java
package com. sxs. spring. bean ; import java. util. List ;
public class BookStore { private List < String > bookList; public BookStore ( ) { } public List < String > getBookList ( ) { return bookList; } public void setBookList ( List < String > bookList) { this . bookList = bookList; }
}
2.beans.xml
< util: list id = " myBookList" > < value> 三国演义</ value> < value> 水浒传</ value> < value> 红楼梦</ value> < value> 西游记</ value> </ util: list> < bean class = " com.sxs.spring.bean.BookStore" id = " bookStore" > < property name = " bookList" ref = " myBookList" /> </ bean>
3.测试
@Test public void UtilList ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; BookStore bean = ioc. getBean ( BookStore . class ) ; System . out. println ( bean) ; }
7.级联属性赋值
1.Dept.java
package com. sxs. spring. bean ;
public class Dept { private String name; public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } @Override public String toString ( ) { return "Dept{" + "name='" + name + '\'' + '}' ; }
}
2.Emp.java
package com. sxs. spring. bean ;
public class Emp { private String name; private Dept dept; public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public Dept getDept ( ) { return dept; } public void setDept ( Dept dept) { this . dept = dept; } @Override public String toString ( ) { return "Emp{" + "name='" + name + '\'' + ", dept=" + dept + '}' ; }
}
3.beans.xml
< bean class = " com.sxs.spring.bean.Dept" id = " dept" /> < bean class = " com.sxs.spring.bean.Emp" id = " emp" > < property name = " name" value = " 王二虎" /> < property name = " dept" ref = " dept" /> < property name = " dept.name" value = " 财务部" /> </ bean>
4.测试
@Test public void cascadingAttributes ( ) { ApplicationContext ioc = new ClassPathXmlApplicationContext ( "beans.xml" ) ; Emp bean = ioc. getBean ( Emp . class ) ; System . out. println ( bean. getDept ( ) . getName ( ) ) ; }