Spring、Hello AOP
AOP 概念:http://blog.csdn.net/moreevan/article/details/11977115
AOP 所使用到的jar 包:
aspectjrt.jar
common-annotations.jar
aspectjweaver.jar
cglib-nodep.jar
commons-logging.jar
spring.jar
1.在beans.xml中配置AOP的命名空间和Context的命名空间,同时打开AOP注解开关:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context/"
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.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <aop:aspectj-autoproxy />
</beans>
2.新建PersonIService和PersonServiceBean :
public interface PersonIService {
public void save();
public void update();
public String getObject(String name);
}
public class PersonServiceImpl implements PersonIService {
@Override
public void save() {
System.out.println("这是一个Save方法!");
}
@Override
public void update() {
System.out.println("这是一个Update方法!");
}
@Override
public String getObject(String name) {
System.out.println("get方法获取对象= "+name+" 的对象");
return "返回了"+name;
}
}
3.新建AOP拦截类:
// @Aspect 标注这个类是一个切面
@Aspect
public class MyInterceptor {
@Pointcut("execution (* cn.raffaello.service..*.*(..))")
private void anyMethod(){} // 声名一个切入点,声名方式比较奇特 @Before("anyMethod()") // 定义前置通知
public void doAccessCheck(){
System.out.println("前置通知");
}
}
4.在beans.xml中分别注入PersonService和MyInterceptor:
<bean id="myInterceptor" class="cn.raffaello.aop.MyInterceptor" />
<bean id="personIService" class="cn.raffaello.service.impl.PersonServiceImpl"/>
5.测试代码:
public class springTest {
private PersonIService personIService=null;
@Before
public void init(){
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
personIService=(PersonIService)context.getBean("personIService");
}
@Test
public void test() {
personIService.save();
}
}
Spring、Hello AOP的更多相关文章
- Spring、AOP详解
如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...
- JAVAEE——spring02:使用注解配置spring、sts插件、junit整合测试和aop演示
一.使用注解配置spring 1.步骤 1.1 导包4+2+spring-aop 1.2 为主配置文件引入新的命名空间(约束) 1.3 开启使用注解代替配置文件 1.4 在类中使用注解完成配置 2.将 ...
- 170511、Spring IOC和AOP 原理彻底搞懂
Spring提供了很多轻量级应用开发实践的工具集合,这些工具集以接口.抽象类.或工具类的形式存在于Spring中.通过使用这些工具集,可以实现应用程序与各种开源技术及框架间的友好整合.比如有关jdbc ...
- 16监听器、Filter、Nginx、Spring、AOP
16监听器.Filter.Nginx.Spring.AOP-2018/07/30 1.监听器 监听web对象创建与销毁的监听器 ServletContextListener HttpSessionLi ...
- Spring 3.0 AOP (一)AOP 术语
关于AOP.之前我已写过一个系列的随笔: <自己实现简单的AOP>,它的关注点在于实现.实现语言是C#,实现方式为 自定义实现 RealProxy 抽象类.重写Invoke方法,以便进行方 ...
- Spring系列之AOP实现的两种方式
AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...
- Spring系列之AOP
一.什么是AOPAOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引 ...
- SSM ( Spring 、 SpringMVC 和 Mybatis )配置详解
使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
随机推荐
- [置顶] viewPager控制滑动速度和时间
遇到如下问题:viewpager滑动时如果想跳过很多条直接到最后一条,中间会黑屏.黑屏是因为中间的view没有加载出来的缘故. stackOverflow上看到的解决方案,在这里记录一下, publi ...
- 依赖注入及AOP简述(一)——“依赖”的概念 .
一.入门:依赖注入 作为一种全新的设计模式理念,“依赖注入”这个词汇在软件设计开发中已经是越来越耳熟能详了,而各种流行于开源社区的“依赖注入框架”,也越来越多的被当作软件工程开发过程中使用的基础框架. ...
- 设置TextView水平居中显示
1.让TextView里的内容水平居中 android:gravity="center_horizontal" 2.让TextView控件在它的父布局里水平居中 android:l ...
- android开发Tost工具类管理(一)
Tost工具类管理: package com.gzcivil.utils; import android.content.Context; import android.widget.Toast; / ...
- Android之Socket通信(一)
一.服务器端,运行在PC机上 import java.io.*; import java.net.*; public class SimpleServer{ public static voi ...
- sql中的split方法
ALTER function [dbo].[f_split](@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp table( ...
- Oracle数据库按属性成绩查询
create or replace function bb return nvarchar2as-----------自定义游标类型type class_student is record( snam ...
- NHibernate各种数据库连接参数文件配置方法说明
//NHibernate各种数据库连接参数文件配置方法说明 //配置文件Config/Hibernate.cfg.xml内容如下所示: <?xml version="1.0" ...
- linux 系统分区方案建议
前言: 以前初识Linux时,对Linux系统安装时分区的选择,一点都不了解,导致几次没法进行下一步安装,因此就静下心来,专门拿出时间研究了研究这方面的知识: 以下内容就是以前通过研究Linux安装过 ...
- JavaScript计算加减乘除
//加法函数 function jiafa(a,b){ var a=parseInt(document.getElementById("number1").value); //pa ...