用MyEclipse开发spring入门

 关于spring 的电子书笔者下载了好几个,包括 spring in action 中文版  、 spring live   中文版、 spring 开发指南等 。其中spring 开发指南中使用 Eclipse作为开发工具,而笔者刚好在用myEclipse,所以就选择了spring 开发指南作为学习重点。

    笔者的开发环境 myeclipse 4.1 +eclipse 3.12  +jdk 1.5 +tomcat 5.5

     编写Spring开发指南中的第一个例子,这个用例子实现了大小写的转换

     1 新建一个项目

file----->new ----->project  在出现的对话框中选择 myeclipse 下的 webproject 如图
 
project name 写入mySpring1 其他的选项默认 下面点finish完成

2 加入spring 包

 在myspring1 项目上点右键 选myeclipse ---> add spring capabities

3 加入log4j 包

菜单---project --properties-----java build path 点libraries 选项卡 再

选 add external jars 加入 log4j 的jar 包 (可从网上下)

最后的效果如下

4 新建 Action接口文件

右键点src ----new ----interface (包名net.xiaxin.spring.qs)

Action.java代码

package net.xiaxin.spring.qs;

public interface Action {
 public String execute(String str);

}
5 建立Action接口的两个实现UpperAction、LowerAction

LowerAction.java

package net.xiaxin.spring.qs;

public class LowerAction implements Action {
 
   private String message;
   public String getMessage() {
     return message;
    }
  
    public void setMessage(String string) {
      message = string;
    }
   
    public String execute(String str) {
     return (getMessage()+str).toLowerCase();
    }
  }

/
UpperAction.java

package net.xiaxin.spring.qs;

public class UpperAction implements Action {
  
   private String message;
  
   public String getMessage() {
    return message;
   }
 
   public void setMessage(String string) {
     message = string;
   }
 
   public String execute(String str) {
    return (getMessage() + str).toUpperCase();
   }
 }

6  新建log4j.properties配置文件,
内容如下:

log4j.rootLogger=DEBUG, stdout
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n

7  Spring配置文件(bean.xml)

在myspring 上右击 --->new--->file  文件名 bean.xml

bean.xml 内容如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
     <description>Spring Quick Start</description>
     <bean id="TheAction"
 class="net.xiaxin.spring.qs.UpperAction">
    <property name="message">
<value>HeLLo</value>
</property>
  </bean> 
 <bean id="action2" class="net.xiaxin.spring.qs.LowerAction">
<property name="message">
<value>HeLLo</value>
</property>
  </bean>
</beans>


8  建立测试文件SimpleTest.java

package test;

import net.xiaxin.spring.qs.Action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SimpleTest {
 public static void main(String args[])
 {
  SimpleTest test=new SimpleTest();
  test.testQuickStart();
  
 }
 public void testQuickStart() {
  
     ApplicationContext ctx=new 
 FileSystemXmlApplicationContext("bean.xml");
    
     Action action = (Action) ctx.getBean("TheAction");
    
     System.out.println(action.execute("Rod Johnson"));
     action = (Action) ctx.getBean("action2");
     System.out.println(action.execute("jecKj"));
 
  }

}

9 把SimpleTeat.java 设置成主类就可以运行了

 运行结果 (红色部分是想要的结果)

。。。。。。

FileSystemXmlApplicationContext - Publishing event in context [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=12694833]: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.FileSystemXmlApplicationContext: display name [org.springframework.context.support.FileSystemXmlApplicationContext;hashCode=12694833]; startup date [Wed May 31 13:30:25 CST 2006]; root of context hierarchy]
 DefaultListableBeanFactory - Returning cached instance of singleton bean 'TheAction'
 HELLOROD JOHNSON
DefaultListableBeanFactory - Returning cached instance of singleton bean 'action2'
 hellojeckj

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值