Spring 教程(二)
- public class User {
- private Integer id;
- private String name;
- }
- public class UserDao {
- public void save(User user){
- System.out.println("save user....");
- }
- public void delete(int id){
- System.out.println("delete user....");
- }
- public void update(User user) {
- System.out.println("update user ....");
- }
- public User query(String name) {
- System.out.println("getUser ....");
- return new User();
- }
- }
- public class UserBeforeAdvice implements MethodBeforeAdvice {
- public void before(Method method, Object[] args, Object target) {
- System.out.println("调用方法:"+method.getName() + "()前拦截处理");
- }
- }
- public class UserAfterAdvice implements AfterReturningAdvice {
- public void afterReturning(Object returnValue, Method method, Object[] args, Object target) {
- System.out.println("方法:"+method.getName() + "()返回后拦截处理");
- }
- }
- public class UserAroundAdvice implements MethodInterceptor {
- public Object invoke(MethodInvocation invocation) throws Throwable {
- System.out.println("调用方法:"+invocation.getMethod().getName() + "()前拦截处理");
- Object o = invocation.proceed();
- System.out.println("调用方法:"+invocation.getMethod().getName() + "()后拦截处理");
- return o;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
- <bean id="userDaoTarget" class="com.boya.spring.dao.UserDao" />
- <bean id="userBeforeAdvice" class="com.boya.spring.aop.UserBeforeAdvice" />
- <bean id="userAfterAdvice" class="com.boya.spring.aop.UserAfterAdvice" />
- <bean id="userAroundAdvice" class="com.boya.spring.aop.UserAroundAdvice" />
- <bean id="userDao" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="interceptorNames">
- <list><value>userAroundAdvice</value></list>
- </property>
- <property name="target" ref="userDaoTarget"></property>
- </bean>
- </beans>
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
- UserDao userDao = context.getBean("userDao", UserDao.class);
- userDao.save(new User());
- }
调用方法:save()前拦截处理save user....调用方法:save()后拦截处理
3、使用Spring配置文件将业务逻辑和AOP切面逻辑进行组装
- public interface SignInterface {
- public Object sign(String nameList);
- }
- public class Teacher implements SignInterface {
- public Object sign(String nameList) {
- System. out .println( "Teacher sign..." );
- return new Object();
- }
- }
- public class Leader implements SignInterface {
- private Teacher teacher;
- public Object sign(String nameList) {
- if (teacher == null) {
- teacher = new Teacher();
- }
- Object o = teacher.sign(nameList);
- return o;
- }
- }
- public static void main(String[] args) {
- SignInterface s = new Leader();
- s.sign("names");
- }
- public class ProxyObject implements InvocationHandler {
- private Object proxy_obj;
- ProxyObject(Object obj) {
- this.proxy_obj = obj;
- }
- public static Object getProxy(Object obj) {
- Class cls = obj.getClass();
- // 通过Proxy类的newProxyInstance方法来返回代理对象
- return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new ProxyObject(obj));
- }
- /**
- * 实现InvocationHandler接口的invoke
- */
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- System.out.println("调用方法:" + method + "()之前拦截处理");
- if (args != null) {
- System.out.println("方法有" + args.length + "个参数");
- for (int i = 0; i < args.length; i++) {
- System.out.println(args[i]);
- }
- }
- // 利用反射机制动态调用真实对象的方法
- Object o = method.invoke(proxy_obj, args);
- System.out.println("调用方法:" + method + "()之后拦截处理");
- return o;
- }
- // 测试代码
- public static void main(String agr[]) {
- SignInterface si = (SignInterface) getProxy(new Teacher());
- si.sign("names");
- }
- }
- public class CGLibTeacher {
- public Object sign(String nameList) {
- System.out.println("Teacher sign...");
- return new Object();
- }
- }
- public class CGLibAop implements MethodInterceptor {
- public Object intercept(Object arg0, Method arg1, Object[] arg2,
- MethodProxy arg3) throws Throwable {
- System.out.println("before...");
- Object o = arg3.invokeSuper(arg0, arg2);
- System.out.println("after...");
- return o;
- }
- }
- public class CGLibProxy {
- public static CGLibTeacher create(CGLibAop aop){
- Enhancer en = new Enhancer();
- //进行代理
- en.setSuperclass(CGLibTeacher.class);
- en.setCallback(aop);
- //生成代理实例
- return (CGLibTeacher)en.create();
- }
- public static void main(String[] args) {
- CGLibTeacher t = CGLibProxy.create(new CGLibAop());
- t.sign("names");
- }
- }
Spring 教程(二)的更多相关文章
- Spring Security教程(二):自定义数据库查询
Spring Security教程(二):自定义数据库查询 Spring Security自带的默认数据库存储用户和权限的数据,但是Spring Security默认提供的表结构太过简单了,其实就 ...
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- Spring教程索引
Spring教程索引 2016-11-15 1 入门 1 概述.深入浅出Spring(一)Spring概述 2 体系结构 3 环境设置 4 Hello World 实例 5 IoC 容器 IoC容 ...
- 13、Spring教程之全部(包括所有章节)
Spring 教程 1.Spring概述 简介 Spring : 春天 --->给软件行业带来了春天 2002年,Rod Jahnson首次推出了Spring框架雏形interface21框架. ...
- CRL快速开发框架系列教程二(基于Lambda表达式查询)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- 手把手教从零开始在GitHub上使用Hexo搭建博客教程(二)-Hexo参数设置
前言 前文手把手教从零开始在GitHub上使用Hexo搭建博客教程(一)-附GitHub注册及配置介绍了github注册.git相关设置以及hexo基本操作. 本文主要介绍一下hexo的常用参数设置. ...
- C#微信公众号开发系列教程二(新手接入指南)
http://www.cnblogs.com/zskbll/p/4093954.html 此系列前面已经更新了两篇博文了,都是微信开发的前期准备工作,现在切入正题,本篇讲解新手接入的步骤与方法,大神可 ...
- 无废话ExtJs 入门教程二十一[继承:Extend]
无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...
- 无废话ExtJs 入门教程二十[数据交互:AJAX]
无废话ExtJs 入门教程二十[数据交互:AJAX] extjs技术交流,欢迎加群(521711109) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3C ...
- 无废话ExtJs 入门教程二[Hello World]
无废话ExtJs 入门教程二[Hello World] extjs技术交流,欢迎加群(201926085) 我们在学校里学习任何一门语言都是从"Hello World"开始,这里我 ...
随机推荐
- 由于本公司项目需要,现急需拥有微软MCSE证书的人才,一经录用,待遇从优!
志鸿科技于1988年在香港创办,从事资讯科技服务,为本地及跨国金融企业提供各种合适的企业应用软件及方案,并于2000年6月30日在香港联合交易所创业板成功上市 (股票代号8048),香港长江实业.新加 ...
- 通过物理模型生成Java代码
通过物理模型生成Java代码 软件开发过程中,我们一般是先针对数据库建模,物理建模完成后,生成数据库表,编码阶段的时候我们会针对数据库表生成大量的Javaeban或者是实体类 Powertdesign ...
- UINavigationBar导航栏相关设置
设置导航颜色 [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1] ...
- 3140:[HNOI2013]消毒 - BZOJ
题目描述 Description 最近在生物实验室工作的小 T 遇到了大麻烦. 由于实验室最近升级的缘故,他的分格实验皿是一个长方体,其尺寸为 a*b*c,a.b.c均为正整数.为了实验的方便,它被划 ...
- VB将PDF流写入ACCESS数据库,通过AcroPDF控件读出PDF流之实现
问题描述: 1.把pdf文件写入access2.读出时用AcroPDF控件 问题解答: 使用流对象保存和显示图片与文件打开vb6,新建工程. 添加两个按钮,一个image控件注意:Access中的ph ...
- ural 1123
找大于等于原数的最小回文数字 代码比较烂........... #include <iostream> #include <cstdio> #include <cstr ...
- Android Activity交互及App交互
Android交互--------->Intent Activity之间----->Explicit Intent App之间--------->Implicit Intent
- oracle中int类型和number类型区别
INT类型是NUMBER类型的子类型.下面简要说明:(1)NUMBER(P,S)该数据类型用于定义数字类型的数据,其中P表示数字的总位数(最大字节个数),而S则表示小数点后面的位数.假设定义SAL列为 ...
- secureCRT中文字符乱码
1.远程linux机器.修改环境变量LANG.例如在~/.bash_profile里面添加 export LANG=zh_CN.UTF8 2.本地windows机器.修改SecureCRT的设置.找到 ...
- c缺陷与陷阱笔记-第一章 词法陷阱
1.运算符的贪心性,匹配最长的运算符,例如 n-->0,从-开始,-是运算符,--是运算符,-->就不是,所以是 n -- > 0,--是 a---b,-是,--是,,---不是,所 ...