原文链接地址是:http://www.cnitblog.com/gavinkin555/articles/35973.html

1 新建一个项目

File----->New ----->Project  在出现的对话框中选择 MyEclipse 下的 Web Project,在Project Name 输入mySpring1,其他的选项默认,再点Finish完成;

2 加入Spring 包

在myspring1 项目上点右键,选MyEclipse ---> Add Spring Capabities,再点Finish完成;

3 加入Log4j 包

菜单--->Project -->Properties-----Java Build Path 点Libraries 选项卡,再选Add External Jars 加入log4j 的jar 包 (可从网上下)http://archive.apache.org/dist/logging/log4j/1.2.8/

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

最新文章

  1. [原创]Centos7 从零编译配置Redis
  2. [转]ORACLE中Like与Instr模糊查询性能大比拼
  3. http权威指南
  4. centos7 添加图形界面的功能
  5. LDS,LES,LFS,LGS,LSS指令
  6. CHROME下去掉保存密码后输入框变成黄色背景样式
  7. JavaEE Tutorials (27) - Java EE的并发工具
  8. C# - 使用皮肤
  9. [置顶] 学习JDK源码:可进一步优化的代码
  10. sql server mdf碎片级数据库修复,数据库碎片级提取
  11. Python 任务队列 Celery
  12. 在Spring Boot中使用数据缓存
  13. PS调出韩式米黄色室内婚纱照片
  14. HDU2082母函数模板题
  15. Shell与Bash
  16. python之SQLAlchemy ORM
  17. torch7入门(安装与使用)
  18. _variant_t与其他数据类型的转换
  19. Hadoop基础-Apache Avro串行化的与反串行化
  20. 去除icon图标特效,阴影,反光

热门文章

  1. 使用psftp向服务器上传文件
  2. Qt Creator 常用快捷键 详细总结
  3. 让textarea根据文本的长度自动调整它的高度
  4. myeclipse自带tomcat
  5. [luogu7207]Sob
  6. 三、MapReduce编程实例
  7. html图片动态增加文字
  8. mybatis避免sql的like注入
  9. IDEA远程快速部署SpringBoot项目到Docker环境
  10. static关键字相关内容