myeclips下spring的开发1

本文详细介绍了如何使用Spring框架创建并测试两个简单的Action类,包括工程搭建、导入Spring包、接口和类的定义、配置applicationContext.xml文件及测试过程。通过实例演示了Spring框架的基本应用。

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

根据他人提供的代码演示了一遍,再自己总结(学会总结并成为自己的东西)----理解spring是什么?

步骤:

第一步:建工程
   File -> New -> Project ->Web Project,"Project Name":MySpringTest,然后"Finish";

第二步:导入spring包
   选中MySpringTest,右击,MyEclipse -> Add Spring Capabilities……,都默认即可;

第三步:
   建立项目所需类;MySpringTest -> src -> New ...(以下三个都这样建)

Spring 的开发没法自动生成 Bean, 这里大家只好手工来写了, 也很简单。

1、接口Action:(MySpringTest -> src -> New -> interface ,取名为Action)

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

2、实现接口Action的类UpperAction:将其 message 属性与输入字符串相连接,并返回其大写形式。

   (MySpringTest -> src -> New -> class ,取名为UpperAction
 1 
 2 public class UpperAction implements Action{
 3     private String message;
 4 
 5     public String getMessage() {
 6         return message;
 7     }
 8 
 9     public void setMessage(String message) {
10         this.message = message;
11     }
12     public String execute(String str){
13         return (getMessage()+str).toUpperCase();
14     }
15 }
16 

3、实现接口Action的类LowerAction:

将其 message 属性与输入字符串相连接,并返回其小写形式。
   (MySpringTest -> src -> New -> class ,取名为LowerAction

 1 
 2 public class LowerAction implements Action{
 3     private String message;
 4 
 5     public String getMessage() {
 6         return message;
 7     }
 8     public void setMessage(String message) {
 9         this.message = message;
10     }
11     public String execute(String str){
12         return(getMessage()+str).toLowerCase();
13     }
14 }
15 

4、做测试用的SimpleTest类:
MySpringTest -> src -> New -> class ,取名为SimpleTest

 1 import org.springframework.context.ApplicationContext;
 2 import org.springframework.context.support.FileSystemXmlApplicationContext;
 3 
 4 public class SimpleTest {
 5     public static void main(String args[])
 6     {
 7         SimpleTest test=new SimpleTest();
 8         test.testQuickStart();
 9     }
10     public void testQuickStart(){
11         ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
12         Action action=(Action)ctx.getBean("action1");
13         System.out.println(action.execute("Rod Johnson"));
14         action=(Action)ctx.getBean("action2");
15         System.out.println(action.execute("jeckj"));
16     }
17 }
18 

第四步:配置applicationContext.xml文件

 1      <?xml version="1.0" encoding="UTF-8"?>
 2      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3      <beans>
 4      <description>Spring Quick Start</description>
 5      
 6      <!--该处bean中的name值必须是 其对应的class中的私有成员名
 7      -->
 8      <bean id="action1" class="UpperAction">
 9      <property name="message"> 
10      <value>HeLLo</value> 
11      </property> 
12      </bean>
13        
14      <bean id="action2" class="LowerAction">
15      <property name="message"> 
16      <value>HeLLo</value> 
17      </property> 
18      </bean> 
19      
20      </beans>

 第四步:调试
   双击 Package Explorer 下 MySpringTest/src/TestAction.java 打开源代码,点击菜单 Run -> Run As -> 1. Java Application, 如果没有错误的话将会出现如下

1 log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
2 log4j:WARN Please initialize the log4j system properly.
3 HELLOROD JOHNSON
4 hellojeckj

总结:spring提供了bean:applicationContext,xml,这里spring提供了什么样的作用?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值