JSP之Spring架构

本文介绍了一个简单的Spring框架应用示例,包括创建项目、定义Action接口及其实现类,并通过Spring配置文件进行依赖注入。通过切换不同的Action实现展示了Spring的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.创建项目,通过Myeclipse导入Spring包.

二、构建 Spring 基础代码 
1. Action接口:
Action 接口定义了一个 execute 方法
execute方法,以完成目标逻辑。 

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


2. Action接口的两个实现UpperAction、LowerAction 

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();
  }


UpperAction将其message属性与输入字符串相连接,并返回其大写形式。 
  SpringFrameWork Developer’s Guide  Version 0.6
 
October 8, 2004     So many open source projects. Why not Open your Documents?
  public String getMessage() {
   return message;
  }
 
  public void setMessage(String string) {
    message = string;
  }
 
  public String execute(String str) {
   return (getMessage()+str).toLowerCase();
  }
}
 
LowerAction将其message属性与输入字符串相连接,并返回其小写形式。 
 
3. Spring配置文件(bean.xml)
<beans>
     <description>Spring Quick Start</description>
     <bean id="TheAction"
 class="net.xiaxin.spring.qs.UpperAction">
   <property name="message">
<value>HeLLo</value>
</property>
  </bean>
</beans>
(请确保配置bean.xml位于工作路径之下,注意工作路径并不等同于CLASSPATH ,eclipse
的默认工作路径为项目根路径,也就是.project文件所在的目录,而默认输出目录/bin是项目
CLASSPATH的一部分,并非工作路径。 ) 
 
4. 测试代码 
  public void testQuickStart() {
 
    ApplicationContext ctx=new 
FileSystemXmlApplicationContext("bean.xml");
   
    Action action = (Action) ctx.getBean("TheAction");
   
    System.out.println(action.execute("Rod Johnson"));
 
 }
可以看到,上面的测试代码中,我们根据"bean.xml"创建了一个ApplicationContext实
例,并从此实例中获取我们所需的Action实现。
 
   SpringFrameWork Developer’s Guide  Version 0.6
 
October 8, 2004     So many open source projects. Why not Open your Documents?
运行测试代码,我们看到控制台输出:
……
HELLO ROD JOHNSON
 
 
我们将bean.xml中的配置稍加修改:
<bean id="TheAction" 
class="net.xiaxin.spring.qs.LowerAction"/>
 
再次运行测试代码,看到:
……
hello rod johnson
 
 
示例完成! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天傲设计小组

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值