Spring day1 Monday 2007.4.9
1. Spring 学习实践第一例
1).写一个接口
package spring.firstday.ioc;
public interface HelloIF {
String sayHello();
}
2).写一个实现类
package spring.firstday.ioc;
import java.util.Calendar;
public class HelloIFImpl implements HelloIF{
private String user;
private Calendar cal;
/*
<!-- 构造注入属性 -->
<constructor-arg>
<value>world</value>
</constructor-arg>
<constructor-arg>
<ref local="calendar"/>
</constructor-arg>
*/
public HelloIFImpl(String user, Calendar cal) {
this.user = user;
this.cal = cal;
}
public HelloIFImpl() {
super();
}
public void setCal(Calendar cal) {
this.cal = cal;
}
/*
<!-- setter方法注入属性,复杂类型只能用setter注入-->
<property name="user">
<value>world</value>
</property>
*/
public void setUser(String user) {
this.user = user;
}
public String sayHello() {
if(cal.get(Calendar.AM_PM) == Calendar.AM)
return "Good morning, " + user;
else if(cal.get(Calendar.HOUR_OF_DAY) < 18)
return "Good afternoon, " + user;
return "Good evening, " + user;
}
}
3).配置 beans.xml(名字可变),重要
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
<beans>
<bean id="hello" class="spring.firstday.ioc.HelloIFImpl">
<!-- 构造注入属性 -->
<constructor-arg>
<value>world</value>
</constructor-arg>
<constructor-arg>
<ref local="calendar"/>
</constructor-arg>
<!-- setter方法注入属性,复杂类型只能用setter注入-->
<!--引用当前xml中id为calendar-->
<!--
<property name="user">
<value>world</value>
</property>
<property name="cal">
<ref local="calendar"/>
</property>
-->
</bean>
<bean id="calendar" class="java.util.GregorianCalendar">
</bean>
<bean id="collections" class="spring.firstday.ioc.CollectionBean">
<property name="list">
<list>
<value>1</value>
<ref local="calendar"/>
</list>
</property>
<property name="set">
<set>
<value>red</value>
<bean class="java.util.Date"></bean>
</set>
</property>
<property name="map">
<map>
<entry key="1">
<value>red</value>
</entry>
<entry>
<key>
<ref local="hello"/>
</key>
<ref local="calendar"/>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="2">blue</prop>
<prop key="3">green</prop>
</props>
</property>
</bean>
</beans>
4).写个test类
package spring.firstday.ioc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import spring.firstday.ioc.traditional.HelloIFImpl;
import static java.lang.System.*;
public class SayHello {
public static void main(String[] args) {
//HelloIF helloTra = new spring.firstday.ioc.traditional.HelloIFImpl();
//out.println(helloTra.sayHello());
//Bean工厂
BeanFactory bf =
new XmlBeanFactory(
new ClassPathResource("spring/firstday/ioc/beans.xml")
);
HelloIF helloIoc = (HelloIF)bf.getBean("hello");
CollectionBean cb = (CollectionBean)bf.getBean("collections");
//HelloIF helloIoc = new spring.firstday.ioc.HelloIFImpl();
out.println(helloIoc.sayHello());
for(Object v : cb.getSet())
out.println(v);
}
}
=========================================================================
Spring day2 Tuesday 2007.4.10