根据他人提供的代码演示了一遍,再自己总结(学会总结并成为自己的东西)----理解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);
}
public String execute(String str);
}
2、实现接口Action的类UpperAction:(将其 message 属性与输入字符串相连接,并返回其大写形式。)
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
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
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
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 >
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
2 log4j:WARN Please initialize the log4j system properly.
3 HELLOROD JOHNSON
4 hellojeckj
总结:spring提供了bean:applicationContext,xml,这里spring提供了什么样的作用?