我们创建一个案例
public class Cat {
//这是一只什么样的猫猫
private Integer age;
private String name;
private String active;
//无参构造器
public Cat(){
}
//有参构造器
public Cat(Integer age, String name, String active) {
this.age = age;
this.name = name;
this.active = active;
}
//get和set方法
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
//toString方法
@Override
public String toString() {
return "Cat{" +
"age=" + age +
", name='" + name + '\'' +
", active='" + active + '\'' +
'}';
}
}
相关的xml配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat01" class="com.spring.Cat">
<property name="age" value="10"/>
<property name="name" value="小花"/>
<property name="active" value="喵喵叫"/>
bean>
beans>
以下是通过类型获取对象的代码
@Test
public void getCatByClass(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
Cat bean = ioc.getBean(Cat.class);
//输出对象
System.out.println("bean=" + bean);
}
需要注意的是:按类型来获取bean,要求ioc容器中的同一个类型的bean只能有一个,否则会抛异常
应用场景:单例情况,不需要同一类型对象有多个
我们继续使用上面的对象代码
通过构造器配置bean
<bean id="cat02" class="com.spring.Cat">
<constructor-arg value="20" index="0"/>
<constructor-arg value="小绿" index="1"/>
<constructor-arg value="喵喵喵" index="2"/>
bean>
获取这个bean
@Test
public void getCat(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
Cat cat02 = ioc.getBean("cat02", Cat.class);
//输出对象
System.out.println("cat02 =" + cat02 );
}
我们继续使用上面的对象代码
通过p名称空间来配置bean
<bean id="cat03" class="com.spring.Cat"
p:age = "10"
p:name="大黄"
p:active="喵!"
/>
获取这个bean
@Test
public void getCat(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
Cat cat03 = ioc.getBean("cat03", Cat.class);
//输出对象
System.out.println("cat03 =" + cat03 );
}
当我们需要从容器中获得的bean对象,与其它对象有依赖时,我们需要构建它们之间的依赖
首先我们写一个DAO类
public class CatDAOImpl {
public CatDAOImpl() {
System.out.println("CatDAO的构造器");
}
public void add(){
System.out.println("CatDAO的add()方法");
}
}
然后我们写一个Service类,Service类中含有DAO类的实例对象,并且调用了DAO类的方法
public class CatServiceImpl {
private CatDAOImpl catDAO;
public CatDAOImpl getCatDAO() {
return catDAO;
}
public void setCatDAO(CatDAOImpl catDAO) {
this.catDAO = catDAO;
}
public void add(){
System.out.println("CatServiceImpl add 被调用");
catDAO.add();
}
}
在配置Spring时,我们需要先配置DAO类
<bean id = "CatDAO" class="com.spring.DAO.CatDAOImpl"/>
然后配置Service类和它们之间的依赖
<bean id = "CatService" class="com.spring.Service.CatServiceImpl">
<property name="catDAO" ref="CatDAO"/>
bean>
让我们来测试一下吧
@Test
public void getCatByRef(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
CatServiceImpl catService = ioc.getBean("CatService", CatServiceImpl.class);
catService.add();
}
}
在这段代码中,调用了catService的add方法,add方法内部,又调用的catDAO的add方法,首先执行的是catService的无参构造器,然后是catDAO的构造器,此时会输出"catDAO的构造器",然后执行catService的输出语句,输出"CatServiceImpl add 被调用",最后执行catDAO的add方法,输出"CatDAO的add()方法"。
结果如下
这就是通过ref实现bean与外部对象的相互引用的一个实例。
继续使用上面的DAO类与Service类,这次我们这样配置
<bean class="com.spring.Service.CatServiceImpl" id="catService2">
<property name="catDAO">
<bean class="com.spring.DAO.CatDAOImpl"/>
property>
bean>
这样,我们也可以完成对象之间的依赖关系
@Test
public void getCatByRef(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
CatServiceImpl catService = ioc.getBean("catService2", CatServiceImpl.class);
catService.add();
}
首先我们创建两个类,People类和Dog类
这是Dog类
public class Dog {
private Integer age;
private String name;
private String active;
public Dog() {
}
public Dog(Integer age, String name, String active) {
this.age = age;
this.name = name;
this.active = active;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getActive() {
return active;
}
public void setActive(String active) {
this.active = active;
}
@Override
public String toString() {
return "Dog{" +
"age=" + age +
", name='" + name + '\'' +
", active='" + active + '\'' +
'}';
}
}
这是People类
public class People {
private String name;
private List<Dog> dogList;
private Map<String,Dog> dogMap;
private Set<Dog> dogSet;
private String[] dogName;
//Properties是Hashtable的子类,是key-value的形式
private Properties pros;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Dog> getDogList() {
return dogList;
}
public void setDogList(List<Dog> dogList) {
this.dogList = dogList;
}
public Map<String, Dog> getDogMap() {
return dogMap;
}
public void setDogMap(Map<String, Dog> dogMap) {
this.dogMap = dogMap;
}
public Set<Dog> getDogSet() {
return dogSet;
}
public void setDogSet(Set<Dog> dogSet) {
this.dogSet = dogSet;
}
public String[] getDogName() {
return dogName;
}
public void setDogName(String[] dogName) {
this.dogName = dogName;
}
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", dogList=" + dogList +
", dogMap=" + dogMap +
", dogSet=" + dogSet +
", dogName=" + Arrays.toString(dogName) +
", pros=" + pros +
'}';
}
}
我们通过xml配置这两个类
首先是Dog类
<bean class="com.spring.Dog" id="dog1"
p:age = "10"
p:name="大黄"
p:active="汪汪"
/>
<bean class="com.spring.Dog" id="dog2">
<property name="age" value="15"/>
<property name="name" value="小白"/>
<property name="active" value="嗷呜"/>
bean>
测试Dog类
@Test
public void getDog(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
Dog dog1 = ioc.getBean("dog1", Dog.class);
System.out.println("第一只小狗" + dog1);
Dog dog2 = ioc.getBean("dog2", Dog.class);
System.out.println("第二只小狗" + dog2);
}
得到如下输出,证明配置成功
接下来是People类
先配置xml
<bean class="com.spring.People" id="people1">
<property name="name" value="张三"/>
<property name="dogList">
<list>
<ref bean="dog1"/>
<ref bean="dog2"/>
list>
property>
bean>
这样,我们就配置好了dog集合,并引入了依赖
测试一下看看
@Test
public void getPeople(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
People people1 = ioc.getBean("people1", People.class);
System.out.println("人类相关信息:" + people1);
}
太好了,我们得到了以下数据
人类相关信息:People{name='张三', dogList=[Dog{age=10, name='大黄', active='汪汪'},
Dog{age=15, name='小白', active='嗷呜'}],
dogMap=null, dogSet=null, dogName=null, pros=null}
说明我们完成了对集合的配置
接下来是对Map的配置
<property name="dogMap">
<map>
<entry>
<key>
<value>dog_01value>
key>
<ref bean="dog1"/>
entry>
map>
property>
看看输出
dogMap={dog_01=Dog{age=10, name='大黄', active='汪汪'}}
得到了这条输出,证明我们对Map的配置也成功了
接下来是数组
<property name="dogName">
<array>
<value>小绿value>
<value>小蓝value>
array>
property>
执行后得到以下输出结果
dogName=[小绿, 小蓝]
说明对数组的配置也成功了
然后我们来配置Properties
<property name="pros">
<props>
<prop key="小狗">小白prop>
<prop key="大狗">大白prop>
props>
property>
执行代码,得到以下结果
pros={小狗=小白, 大狗=大白}
太好了,我们又配置成功了
首先我们创建一个类,类里面有集合属性
public class Book {
private List<String> bookList;
public Book() {
}
public List<String> getBookList() {
return bookList;
}
public void setBookList(List<String> bookList) {
this.bookList = bookList;
}
@Override
public String toString() {
return "Book{" +
"bookList=" + bookList +
'}';
}
}
然后开始配置
<util:list id="MyBook">
<value>java基础value>
<value>数据库value>
<value>算法分析value>
util:list>
<bean class="com.spring.Book" id="book">
<property name="bookList" ref="MyBook"/>
bean>
<bean class="com.spring.Book" id="book2">
<property name="bookList" ref="MyBook"/>
bean>
可以看出,通过util我们实现了数据的复用,book和book2应该拥有相同的集合
接下来我们测测看
@Test
public void getBook(){
//创建容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
//得到对象
Book book = ioc.getBean("book", Book.class);
System.out.println("book = " + book);
Book book1 = ioc.getBean("book2", Book.class);
System.out.println("book2 = " + book);
}