没有Spring之前,代码原来是这样写的!

        最近在看一位前辈的《Spring 开发指南》,才明白原来没有Spring之前,代码是这样写的。这对于我这个一开始学习Java EE编程就使用Spring的菜鸟级程序员来说,是很难理解Spring所带来的巨大便利的。以下是从这本电子书上几乎原封不动搬写下来的代码,作为自己学习Spring 原理的积累。

      首先,该项目是由用Myeclipse工具编写的,看一下整个Java 项目的结构图:

Action接口:

/**
 * Action接口.
 */
public interface Action {
	String execute(String name);
}

UpperAction类:

/**
 * 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 name){
		return (this.getMessage() + name).toUpperCase();
	}
}
LowerAction类:

/**
 * LowerAction用来显示单词测小写形式.
 */
public class LowerAction implements Action {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	@Override
	public String execute(String name) {
		return (this.getMessage() + name).toLowerCase();
	}
}

ActionFactory类:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
import org.apache.commons.beanutils.BeanUtils;
/**
 * ActionFactory Action生成类.
 */
public class ActionFactory {
	public static Action getAction(String actionName){
		Properties pro = new Properties();
		try {
			pro.load(new FileInputStream(new File("config.properties")));
			String actionImplName = pro.getProperty(actionName);
			String actionMsg = pro.getProperty(actionName + "_msg");
			Object obj = Class.forName(actionImplName).newInstance();
			BeanUtils.setProperty(obj, "message", actionMsg);
			return (Action)obj;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
}

TestActionFactory类:

/**
 * ActionFactory测试类.
 */
public class TestActionFactory {
	public static void main(String[] args) {
		Action action = ActionFactory.getAction("TheAction");
		System.out.println(action.execute("\tno!"));
	}
}

config.properties文件:

TheAction=UpperAction
TheAction_msg=Red Hat

注:项目中要用的的commons-beanutils.jar 和 commons-logging.jar可以在 http://www.findjar.com/index.x站点搜到

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一段简单的Spring AOP代码: 1. 首先,我们需要定义一个切面类,用于实现AOP的逻辑。 ```java package com.example.demo.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class); @Before("execution(* com.example.demo.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { logger.info("Before method " + joinPoint.getSignature().getName() + " is called."); } @AfterReturning(pointcut = "execution(* com.example.demo.service.*.*(..))", returning = "result") public void logAfter(JoinPoint joinPoint, Object result) { logger.info("After method " + joinPoint.getSignature().getName() + " is called. Result is " + result); } } ``` 2. 在上面的代码中,我们使用了@Aspect注释来标识这是一个切面类,并使用@Before和@AfterReturning注释来定义切入点和AOP逻辑。具体来说: - 在@Before注释中,我们使用了一个execution表达式来定义切入点,它表示在任何一个com.example.demo.service包下的任何方法被调用之前都会执行logBefore方法。 - 在@AfterReturning注释中,我们同样使用了一个execution表达式来定义切入点,并指定了返回值类型为Object。这意味着只有在com.example.demo.service包下的任何方法被调用并返回一个非null值时,才会执行logAfter方法。 3. 最后,我们需要在Spring配置文件中配置该切面类。 ```xml <bean id="loggingAspect" class="com.example.demo.aop.LoggingAspect"/> <aop:config> <aop:aspect ref="loggingAspect"> <aop:before pointcut="execution(* com.example.demo.service.*.*(..))" method="logBefore"/> <aop:after-returning pointcut="execution(* com.example.demo.service.*.*(..))" returning="result" method="logAfter"/> </aop:aspect> </aop:config> ``` 在上面的配置中,我们首先定义了一个loggingAspect bean,然后使用<aop:config>标签来定义整个AOP配置。在<aop:aspect>标签中,我们指定了切面类的引用,并使用<aop:before>和<aop:after-returning>标签来定义切入点和AOP逻辑。注意,这些标签中的pointcut和method属性分别对应切面类中的@Before和@AfterReturning注释中的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值