Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > Spring 学习总结
【标  题】:Spring 学习总结
【关键字】:Spring
【来  源】:http://blog.csdn.net/Ranen/archive/2007/04/10/1559636.aspx

Spring 学习总结

Your Ad Here

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


 

common-fileupload:【上一篇】
java培训笔记二:【下一篇】
【相关文章】
  • 解读超轻量级DI容器-Guice与Spring框架的区别
  • spring+struts+hibernate开发实例(三)
  • 用Spring 更好地处理Struts 动作,三种整合Struts应用程序与 Spring 的方式
  • jsf和spring的整合
  • 在spring+hibernate3框架中使用HibernateDaoSupport应注意的问题
  • Oracle开放Oracle App Server与Spring Framework的集成代码
  • Webservices with Spring, XFire and jsr181
  • Spring JDBC抽象框架简化Web数据库开发
  • Spring的远程服务调用
  • Spring2.0新特性几相关点评
  • 【随机文章】
  • 用Rational Rose和UML开发J2EE应用(一)
  • 我的SlackWare设置
  • 汇编(二十三)——汇编伪指令四
  • 如何认识、预防和杜绝病毒
  • Unit Test 研究报告
  • construct multi-dimension array with vector<>
  • VC++在应用程序中使用标准窗口控件初步
  • AIX---02系统管理工具
  • 软件测试实践 之 测试环境的规划与管理
  • 用Photoshop帮MM做纹身
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.