1.练习how2j面向sop的编程呢个,理解面向对象编程的含义
(1)Spring AOP
AOP即面向切面编程,存储着公共方法,不同接口都可以进行操作的事务,若要进行调用公共方法,则设置一个切点(指定需要拦截的方法),定义一个切面(包含了要在切点上进行的具体操作,你要进行的操作,定义在xml文档中)
准备业务类 ProductService
具体代码:
package service;
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
准备日志切面 LoggerAspect
具体代码如下:
package aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="pojo.Category">
<property name="name" value="yyy" />
</bean>
<bean name="p" class="pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
<bean name="s" class="service.ProductService">
</bean>
<bean id="loggerAspect" class="aspect.LoggerAspect"/>
<aop:config> <!-- aop:config是AOP模块中的声明式元素 -->
<!-- aop:pointcut定义一个切入点;每一个aop:aspect定义一个切面 -->
<aop:pointcut id="loggerCutpoint"
expression="execution(* service.ProductService.*(..)) "/> <!-- 匹配ProductService中的所有方法 -->
<aop:aspect id="logAspect" ref="loggerAspect"> <!-- 该切面引用了名为 "loggerAspect" 的 bean,将 id 为 "logAspect" 的切面与 "loggerAspect" 类关联起来,从而实现切面功能的完成。-->
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
<!-- aop:around将目标方法围绕起来,使切面能够在目标方法执行前后,分别执行自己特定的行为 -->
</aop:aspect>
</aop:config>
</beans>
运行测试类:
具体代码如下:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.ProductService;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
ProductService s = (ProductService) context.getBean("s");
s.doSomeService();
}
}
具体结果如下:
具体的切面操作在LoggerAspect文件中进行修改:
我的修改如下:
具体代码如下:
package aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
// System.out.println("start log:" + joinPoint.getSignature().getName());
System.out.println("start log:" + "程序开始运行");
Object object = joinPoint.proceed();
// System.out.println("end log:" + joinPoint.getSignature().getName());
System.out.println("end log:" + "程序结束运行");
return object;
}
}