这是我第一本java类的书,在学SSH的时候,感觉spring是最没用的,然而现在实习快一年了,现在才发现spring的精妙,决定看本书来更了解下spring,这两天学习bean装配,别看内容简单,其实平常做项目的时候xml配置是很少用到的(项目搭建的时候xml配置基本上都配置好了,以后都不会动),所以导致自己配的时候往往错误很多。
constructor-arg的用法
先定义一个Juggler接口
package spring.ioc01;
public interface Juggler {
public void perform();
}
再写一个类Pan实现Juggler接口
package spring.ioc01;
public class Pan implements Juggler {
private Poem poem;
private int num;
public Pan() {
}
public Pan(Poem poem, int num) {
this.poem = poem;
this.num = num;
}
public Poem getPoem() {
return poem;
}
public void setPoem(Poem poem) {
this.poem = poem;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public void perform() {
System.out.print("我在耍杂技,丢了" + num + "个球,并且正在 ");
poem.recite();
}
}
写一个Poem接口
package spring.ioc01;
public interface Poem {
public void recite();
}
写一个类Pan2实现Poem接口
package spring.ioc01;
public class Pan2 implements Poem {
@Override
public void recite() {
System.out.print("吟诗");
}
}
最后写一个单元测试
/**
* ioc01
* xml使用构造方法
*/
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("ioc01.xml");
Pan pan = (Pan) context.getBean("pan");
pan.perform();
}
这些代码的意思是pan即是Poem(诗人),又是Juggler(耍杂技的人),他能在耍杂技的时候也能吟诗(执行perform())。那么我们需要装配Pan这个类,再看一下xml怎么写。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
default-lazy-init="true">
<description>Spring Configuration</description>
<bean id="pan2" class="spring.ioc01.Pan2"></bean>
<bean id="pan" class="spring.ioc01.Pan">
<constructor-arg value="15"></constructor-arg>
<constructor-arg ref="pan2"></constructor-arg>
</bean>
</beans>
因为类Pan有一个构造方法,num表示耍杂技时丢球的数目,poem表示将Poem诗人这个身份传入Pan。而正好能帮我们通过构造函数构造好一个Pan实例。这里Spring有几个特性需要注意。
1. 如果bean标签内没有constructor-arg标签,则调用空的构造方法。
2. 如果bean本身有多个构造方法,bean标签内的constructor-arg标签会选择合适的构造方法进行调用,且constructor-arg标签没有先后顺序
比如
<bean id="pan" class="spring.ioc01.Pan">
<constructor-arg value="15"></constructor-arg>
<constructor-arg ref="pan2"></constructor-arg>
</bean>
<bean id="pan" class="spring.ioc01.Pan">
<constructor-arg ref="pan2"></constructor-arg>
<constructor-arg value="15"></constructor-arg>
</bean>
这两种写法都会调用下面的构造方法
public Pan(Poem poem, int num) {
this.poem = poem;
this.num = num;
}
3. constructor-arg标签的value属性表示填值,它会根据构造函数自动转变成int,string等类型,
ref属性表示bean的引用,可以引用id为pan2的bean。
如果前面没有预先设置id为pan2的bean。
xml文件可以这么写
<bean id="pan" class="spring.ioc01.Pan">
<constructor-arg value="15"></constructor-arg>
<constructor-arg>
<bean class="spring.ioc01.Pan2"></bean>
</constructor-arg>
</bean>
最后的输出结果是
我在耍杂技,丢了15个球,并且正在 吟诗