spring入例(1)
来源:互联网 发布:ubuntu软件源 编辑:程序博客网 时间:2024/05/18 00:24
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 设置成主类就可以运行了可以运行了
- spring入例(1)
- Spring入門
- Spring入門
- mongodb整合入spring
- Spring核心学习-AOP(7) 织入和代理
- spring从头开始(二)---bean织入
- spring从头开始(三)----bean织入进阶
- struts2入門(1)
- spring里加入xml提示
- Spring初入配置~十四
- MVC应用(登入1)
- 浪潮记(1):初入浪潮
- 初入机器学习(1)
- web前端面试入坑(1)
- 关于spring中Assert的应用(方法入参检测工具类)
- Spring Security 结合CAS登入登出配置文件详解(备忘)
- 关于spring中Assert的应用(方法入参检测工具类)
- spring mvc项目从简入奢全过程记录(一):第一个Bean
- 如何成为一个受欢迎的项目经理
- 软件系统开发中的组件框架技术研究、设计和应用
- SQL2000出现未能建立与(local)的连接的解决办法
- PLC及PC与RFID射频识别读写器串行通讯的实现
- google苏州地图
- spring入例(1)
- 局部刷新
- Windows多线程间同步事件的控制方法
- 学会放弃
- VC缺省链接库的顺序不对导致链接错误
- 关于查找内存泄漏
- 关于英文名
- 构建基于forms的验证机制过程如下
- C++-namespace(命名空间)