Spring Aop XML

来源:互联网 发布:ubuntu安装apache php 编辑:程序博客网 时间:2024/05/10 20:30



applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:component-scan base-package="com.mth" /><!--XML配置 拦截类--><bean id="logInterceptor" class="com.mth.aop.LogInterceptor"></bean><!--第一种写法--><!--<aop:config>--><!-- 在aspect外配置的point是全局切入点--><!--<aop:pointcut expression="" id="p2" />--><!--定义一个切面--><!--<aop:aspect id="a1" ref="logInterceptor">--><!--定义一个切入点 在aspect里面配置的只有自己可以使用--><!--<aop:pointcut expression="execution(public void com.mth.impl..*.*(..))"--><!--id="p1" />--><!--<aop:before pointcut-ref="p1" method="before" />--><!--<aop:after pointcut-ref="p1" method="afterMethod" />--><!--</aop:aspect>--><!--</aop:config>--><!--第二种写法 直接指定pointcut--><aop:config><aop:aspect id="a1" ref="logInterceptor"><aop:before  method="before" pointcut="execution(public void com.mth.impl..*.*(..))"/></aop:aspect></aop:config></beans>

LogInterceptor 类:

package com.mth.aop;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;/** *  * @ClassName: LogInterceptor * @Description: Aop 面像切面编程XML方式 此类也要交给Spring初始化就需要添加注解@Component * @author mth 75100313@qq.com * @date 2014-1-20 上午08:44:19 *  */// @Aspect// @Componentpublic class LogInterceptor {// 多个一样的植入点可以抽出来定义// @Pointcut("execution(public void com.mth.impl..*.*(..))")public void myMethod() {}// 在方法执行之前先要执行这个方法// 植入点语句// @Before("execution(public void com.mth.impl.StuDaoImpl.save*(com.mth.bean.Student))")public void before() {System.out.println("before save");}// 方法正常执行完成之后(参数就是方法名)// @AfterReturning("myMethod()")public void afterReturning() {System.out.println("after save");}// 方法抛异常之后执行(StuDaoImpl里面的保存学生方法)// @AfterThrowing("myMethod()")public void afterThrowing() {System.out.println("afterThrowing");}// 环绕方法// @Around("myMethod()")public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {System.out.println("around save start");pjp.proceed();System.out.println("arround save end");}// 最终通知 任意返回值类型 com.mth下的任意包中的任意类中的任意方法 参数任意// @After("execution(* com.mth..*.*(..))")public void afterMethod() {System.out.println("---------after----------");}}

StuDaoImpl:

package com.mth.impl;import org.aspectj.lang.annotation.AfterThrowing;import org.springframework.stereotype.Component;import com.mth.bean.Student;import com.mth.dao.IStuDao;@Component("stuDao")public class StuDaoImpl implements IStuDao {@Overridepublic void saveStu(Student stu) {System.out.println("保存学生");//配合注解@AfterThrowing测试// throw new RuntimeException();}}

Service:

package com.mth.service;import javax.annotation.Resource;import org.springframework.stereotype.Component;import com.mth.bean.Student;import com.mth.dao.IStuDao;@Component("ser")public class Service {private IStuDao dao;public IStuDao getDao() {return dao;}@Resource(name="stuDao")public void setDao(IStuDao dao) {this.dao = dao;}public void saveStu(Student student) {dao.saveStu(student);}}


测试代码:

package com.mth.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.mth.bean.Student;import com.mth.service.Service;public class Test {/** * @Title: main * @Description: 测试Spring注解 * @param @param args 设定文件 * @return void 返回类型 * @throws */public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Service service = (Service) context.getBean("ser");service.saveStu(new Student());}}

附第一种配置写法:

<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><context:component-scan base-package="com.mth" /><!--XML配置 拦截类--><bean id="logInterceptor" class="com.mth.aop.LogInterceptor"></bean><aop:config><aop:pointcut expression="" id="p2" /><aop:aspect id="a1" ref="logInterceptor"><aop:pointcut expression="execution(public void com.mth.impl..*.*(..))"id="p1" /><aop:before pointcut-ref="p1" method="before" /><aop:after pointcut-ref="p1" method="afterMethod" /></aop:aspect></aop:config></beans>


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 德牧不吃东西怎么办 摩托车头盔太重怎么办 摩托车头盔起雾怎么办 真皮沙发太软了怎么办 老师硬让换班怎么办 同事经常要换班怎么办 怀孕在家没钱花怎么办 摩托车车把歪了怎么办 摩旅 手机不防水怎么办 头盔镜片花了怎么办 踏板摩托车速度底怎么办 摩托车头盔小了怎么办 房东和租客纠纷怎么办 租客与房东纠纷怎么办 乙肝打了瘦脸针怎么办 去绣水搞到手上痛怎么办 脚破了皮很痛怎么办 脚被车撞了肿了怎么办 ps4光盘花了怎么办 耳后总是长孑子怎么办 孩孑高三总是玩手机怎么办 摩托车被收了怎么办 摩托车的手续都怎么办 摩托车罚单掉了怎么办 行人遇到黄灯该怎么办 长辈借钱不还怎么办 不绣刚电梯轿壁有凹槽怎么办 电梯下限位故障怎么办 卫生间夏天太热怎么办 07大檐帽变形了怎么办 税务局不批发票怎么办 进项发票太多了怎么办 发票报销联丢失怎么办 发票领用簿没有怎么办 发票购买本遗失怎么办 销售方遗失发票怎么办 增值税发票发票联丢失怎么办 苹果购买发票丢失怎么办 空白增值税发票发票丢失怎么办 网购发票 领购簿怎么办 购物发票丢了怎么办