myeclipse配置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)

public   class  UpperAction  implements  Action {
    
private  String message;

    
public  String getMessage() {
        
return  message;
    }

    
public   void  setMessage(String message) {
        
this .message  =  message;
    }

    
public  String execute(String str) {
        
return  (getMessage()  +  str).toUpperCase();
    }
}

3、实现接口Action的类LowerAction:

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

public   class  LowerAction  implements  Action {
    
private  String message;

    
public  String getMessage() {
        
return  message;
    }

    
public   void  setMessage(String message) {
        
this .message  =  message;
    }

    
public  String execute(String str) {
        
return  (getMessage()  +  str).toLowerCase();
    }
}
4、做测试用的SimpleTest类:
(MySpringTest -> src -> New -> class ,取名为SimpleTest)
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(
                
" src/applicationContext.xml " );
        Action action 
=  (Action) ctx.getBean( " action1 " );
        System.out.println(action.execute(
" Rod Johnson " ));
        action 
=  (Action) ctx.getBean( " action2 " );
        System.out.println(action.execute(
" jeckj " ));
    }
}
5、配置applicationContext.xml文件
<? xml version="1.0" encoding="UTF-8" ?>
< beans  xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >

    
< description > Spring Quick Start </ description >

    
<!-- 该处bean中的name值必须是 其对应的class中的私有成员名
    
-->
    
< bean  id ="action1"  class ="UpperAction" >
        
< property  name ="message" >
            
< value > HeLLo </ value >
        
</ property >
    
</ bean >

    
< bean  id ="action2"  class ="LowerAction" >
        
< property  name ="message" >
            
< value > HeLLo </ value >
        
</ property >
    
</ bean >
</ beans >
5、在WEB-INF/class 目录下建立一个log4j.propertie
log4j.rootLogger = ERROR , stdout 
log4j.appender.stdout
= org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout
= org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern
= %d %5p (%F:%L) - %m%n 
第四步:调试
双击 Package Explorer 下 MySpringTest/src/TestAction.java 打开源代码,点击菜单 Run -> Run As -> 1. Java Application, 如果没有错误的话将会出现如下
HELLOROD JOHNSON
hellojeckj

转载于:https://my.oschina.net/u/1271120/blog/164289

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值