欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring入门小例子

程序员文章站 2022-04-09 09:15:06
...

Spring 入门小例子

下面的这个小例子来自《Spring开发指南》:

一、程序结构

 

     程序结构参照附件中的图片。该例子是一个java project.

 

二、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

 

三、net.xiaxing.spring.qs.Action

   

package net.xiaxing.spring.qs;

/**
 * Action 接口
 * 
 * @author duyanjun
 * @version 1.0
 */
public interface Action {
	
	/**
	 * execute 方法
	 * 
	 * @param str 需显示的字付串
	 * @return String :显示的字付串
	 */
	public String execute(String str);
}

 

四、net.xiaxing.spring.qs.UpperAction

package net.xiaxing.spring.qs;

/**
 * 转换大写Action
 * 
 * @author duyanjun
 * @version 1.0
 */
public class UpperAction implements Action {

	// message
	private String message;
	
	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}
	
	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * execute 方法
	 * 
	 * @param str 需显示的字付串
	 * @return String :显示的字付串
	 */
	public String execute(String str) {
		return (this.getMessage() + str).toUpperCase();
	}

}

 

 

五、net.xiaxing.spring.qs.LowerAction

package net.xiaxing.spring.qs;

/**
 * 转换小写Action 
 * 
 * @author duyanjun
 * @version 1.0
 *
 */
public class LowerAction implements Action {

	// message
	private String message;
	
	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}
	
	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
	
	/**
	 * execute 方法
	 * 
	 * @param str 需显示的字付串
	 * @return String :显示的字付串
	 */
	public String execute(String str) {
		return (this.getMessage() + str).toLowerCase();
	}

}

 

 

 

 六、test.TestQuickStart

package test;

import net.xiaxing.spring.qs.Action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * 测试程序
 * 
 * @author duyanjun
 * @version 1.0
 */
public class TestQuickStart {
	
	/**
	 * 测试方法
	 */
	public void testQuickStart(){
		ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
		Action action = (Action) ctx.getBean("theAction");
		System.out.println(action.execute("Rod Johnson"));
	}
	
	/**
	 * main方法
	 * 
	 * @param args : 需在运行程序时传入的参数
	 */
	public static void main(String[] args){
		TestQuickStart testQuickStart = new TestQuickStart();
		testQuickStart.testQuickStart();
	}
}

 

 

七、bean.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" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-2.5.xsd   
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<bean id="theAction" class="net.xiaxing.spring.qs.UpperAction">
		<property name="message">
			<value>Hello</value>
		</property>
	</bean>
</beans>

  

 

八、运行结果

HELLOROD JOHNSON 

如果将bean.xml中的

<bean id="theAction" class="net.xiaxing.spring.qs.UpperAction">

改为

<bean id="theAction" class="net.xiaxing.spring.qs.LowerAction">

 运行结果为:

 hellorod johnson