首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Spring中的AOP面临切面编程和Spring对JDBC的支持

2012-10-16 
Spring中的AOP面向切面编程和Spring对JDBC的支持package cn.itcast.cc.spring.aspectjpublic interface A

Spring中的AOP面向切面编程和Spring对JDBC的支持

package cn.itcast.cc.spring.aspectj;public interface ArithmeticCalculator {void add(int i, int j);// 无返回值,用于测试void sub(int i, int j);// 无返回值,用于测试int mul(int i, int j);int div(int i, int j);}
?

package cn.itcast.cc.spring.aspectj;import org.springframework.stereotype.Component;@Component(value="calc")public class ArithmeticCalculatorImpl implements ArithmeticCalculator {@Overridepublic void add(int i, int j) {int result = i + j;System.out.println(result);}@Overridepublic void sub(int i, int j) {int result = i - j;System.out.println(result);}@Overridepublic int div(int i, int j) {int result = i / j;return result;}@Overridepublic int mul(int i, int j) {int result = i * j;return result;}}
?

package cn.itcast.cc.spring.aspectj;import org.aspectj.lang.*;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;@Aspect // AspectJ切面Bean@Component // Beanpublic class ArithmeticCalculatorAspect {// 前置通知@Before(value = "execution (* cn.itcast.cc.spring.aspectj.ArithmeticCalculator.* (..))")public void beforeMethodLoggin(JoinPoint jp) {System.out.println("before " + jp.getSignature().getName());}// 后置通知@After(value = "execution (* cn.itcast.cc.spring.aspectj.ArithmeticCalculator.* (..))")public void afterMethodLoggin(JoinPoint jp) {System.out.println("after " + jp.getSignature().getName());}// 返回通知@AfterReturning(value = "pointCut()", returning = "result")public void afterMethodLogginReturn(JoinPoint jp, Object result) {System.out.println("after " + jp.getSignature().getName() + " return " + result);}// 异常通知@AfterThrowing(value = "pointCut()", throwing = "e")public void errorMethodLoggin(JoinPoint jp, Throwable e) {System.out.println("method " + jp.getSignature().getName()+ " throwing " + e);}// 环绕通知,通过它可以实现上面所有的通知。@Around("execution (* cn.itcast.cc.spring.aspectj.ArithmeticCalculator.* (..))")public Object aroundMethodLoggin(ProceedingJoinPoint pjp) {System.out.println("around_before " + pjp.getSignature().getName());Object result = null;try {result = pjp.proceed();} catch (Throwable e) {e.printStackTrace();System.out.println("around_error " + pjp.getSignature().getName());}System.out.println("around_after " + pjp.getSignature().getName());System.out.println("around_after " + pjp.getSignature().getName()+ " return " + result);return result;}// 重用切入点,其他 “通知方法”可以引用此方法名称“pointCut()”。@Pointcut(value = "execution (* cn.itcast.cc.spring.aspectj.ArithmeticCalculator.* (..))")public void pointCut() {}}
?

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="cn.itcast.cc.spring.aspectj" /><aop:aspectj-autoproxy /></beans>
?

package cn.itcast.cc.spring.aspectj;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("beans-aspect.xml");ArithmeticCalculator calc = (ArithmeticCalculator) ac.getBean("calc");calc.add(1, 2);// calc.sub(2, 1);// calc.mul(3, 3);// calc.div(10, 2);}}
?

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean id="arithmeticCalculatorAspect" /><bean id="arithmeticCalculatorImpl" /><aop:config><aop:pointcutexpression="execution(* cn.itcast.cc.spring.xml.ArithmeticCalculator.* (..))"id="pointcut" /><aop:aspect order="1" ref="arithmeticCalculatorAspect"><aop:before method="beforeMethodLoggin" pointcut-ref="pointcut"/><aop:after method="afterMethodLoggin" pointcut-ref="pointcut"/><aop:after-returning method="afterMethodLogginReturn" returning="result" pointcut-ref="pointcut"/><aop:after-throwing method="errorMethodLoggin" throwing="e" pointcut-ref="pointcut"/><aop:around method="aroundMethodLoggin" pointcut-ref="pointcut"/></aop:aspect></aop:config></beans>
?

@DeclareParents(value="cn.itcast.cc.spring.aspectj.ArithmeticCalculator*",defaultImpl=MinCalculatorImpl.class)public MinCalculator minCalculator;@DeclareParents(value="cn.itcast.cc.spring.aspectj.ArithmeticCalculator*", defaultImpl=MaxCalculatorImpl.class)public MaxCalculator maxCalculator;
?

<aop:declare-parents types-matching="cn.itcast.cc.spring.xml.ArithmeticCalculator*" implement-interface="cn.itcast.cc.spring.xml.MaxCalculator"default-impl="cn.itcast.cc.spring.xml.MaxCalculatorImpl"/><aop:declare-parents types-matching="cn.itcast.cc.spring.xml.ArithmeticCalculator*" implement-interface="cn.itcast.cc.spring.xml.MinCalculator"default-impl="cn.itcast.cc.spring.xml.MinCalculatorImpl"/>
?

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-xml.xml");Object obj = ctx.getBean("calc");ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) obj;// 可以转换MinCalculator minCalculator = (MinCalculator) obj;// 可以转换MaxCalculator maxCalculator = (MaxCalculator) obj;?

<bean id="jdbcTemplate" ref="dataSource"/></bean>
?

package cn.itcast.cc.spring.jdbc;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;public class JdbcTest {private ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");private JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");@Testpublic void testInsert() {String sql = "insert into customers(customer_name,home_address,mobile_phone) VALUES (?,?,?)";Object[] args = { "changcheng", "DaLian", "1398639955" };this.jdbcTemplate.update(sql, args);}}
?

?

Spring还为我们提供了一个更简单的CURD类——SimpleJdbcTemplate,它使用起来比JdbcTemplate简单些,在此也不多做介绍了。

热点排行