<bean id="classDao" class="cn.itheima03.spring.aop.xml.ClassesDaoImpl"></bean> <bean id="myTransaction" class="cn.itheima03.spring.aop.xml.MyTransaction"></bean> |
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<!-- 1、目标类 2、切面 3、进行AOP的配置 -->
<bean id="classDao" class="cn.itheima03.spring.aop.xml.ClassesDaoImpl" ></bean>
<bean id="myTransaction" class="cn.itheima03.spring.aop.xml.MyTransaction" ></bean>
<aop:config >
<!-- 切入点表达式 expression切入点表达式
id 唯一标示 -->
<aop:pointcut
expression="execution(*
cn.itheima03.spring.aop.xml.ClassesDaoImpl.*(..))"
id= "perform" />
<!-- ref 引向切面
切面里包含各种各样的通知,这些通知都是我们自己想要额外实现的东西,比如开启事务等。。-->
<aop:aspect ref= "myTransaction">
<!-- 方法执行之前执行
-->
<aop:before method= "beginTransaction" pointcut-ref="perform" />
<!-- 后置通知
returning 返回值 要与方法中的参数的名字相对应 -->
<aop:after-returning method= "commit" pointcut-ref="perform" returning="val" />
<!-- 最终通知
无论目标方法是否有异常,都执行 -->
<aop:after method= "finnalyMethod" pointcut-ref="perform" />
<!-- 异常通知
throwing 获取目标方法抛出的异常信息 -->
<aop:after-throwing method= "throwingMethod" pointcut-ref="perform" throwing="ex" />
<!-- 相当于
代理中invoke 方法,可以控制切入点的执行 -->
<aop:around method= "aroundMethod" pointcut-ref="perform" />
</aop:aspect>
</aop:config >
</beans>
|
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)
throws-pattern?)
|
public interface ClassesDao
{
public void saveClasses(Classes
classes);
public void updateClasses(Classes
classes);
public List<Classes>
getClasses();
}
===========================================
public class ClassesDaoImpl extends HibernateUtils{
public String
saveClasses(Classes classes) {
int a =
1/0;
sessionFactory.getCurrentSession().save(classes);
return "aaaa" ;
}
public List<Classes>
getClasses() {
return sessionFactory .getCurrentSession().createQuery("from
Classes").list();
}
public void updateClasses(Classes
classes) {
sessionFactory.getCurrentSession().update(classes);
}
}
===========================================
public class HibernateUtils
{
public static SessionFactory sessionFactory;
static{
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory =
configuration.buildSessionFactory();
}
}
===========================================
public class MyTransaction extends HibernateUtils{
private Transaction transaction;
/**
* 前置通知
* JoinPoint 能够调用该API得到连接点的一些信息
*/
public void beginTransaction(JoinPoint
joinPoint){
System. out.println(joinPoint.getSignature().getName());
this.transaction = sessionFactory.getCurrentSession().beginTransaction();
}
/**
* 后置通知
* 1、获取目标方法的返回值
*/
public void commit(Object
val){
System. out.println(val);
this.transaction .commit();
}
/**
* 最终通知
*/
public void finnalyMethod(){
System. out.println("finally
method" );
}
/**
* 异常通知
*/
public void throwingMethod(Throwable
ex){
/**
* 输出目标方法的异常信息
*/
System. out.println(ex.getMessage());
}
/**
* 环绕通知
* 1、如果不执行joinPoint.proceed();,目标方法是不执行的
* 2、在目标方法执行的上下文添加内容
*/
public void aroundMethod(ProceedingJoinPoint
joinPoint){
try {
System. out.println("aaaa" );
joinPoint. proceed();//执行目标方法
System. out.println("bbbb" );
} catch (Throwable
e) {
e.printStackTrace();
}
}
}
===========================================
/**
* 注意的点
* 1、代理对象的方法体的内容就是拦截器 中invoke方法体的内容
* 2、在客户端,用代理对象调用方法的时候进去了invoke方法
*/
public class ClassesDaoTest
{
@Test
public void testSaveClasses(){
ApplicationContext context = new ClassPathXmlApplicationContext("cn/itheima03/spring/aop/xml/applicationContext.xml" );
ClassesDaoImpl classesDao = (ClassesDaoImpl)context.getBean("classDao" );
Classes classes = new Classes();
classes.setCname( "afds");
classesDao.saveClasses(classes);
}
}
|
/**
* 切面
* 日志管理
*/
public class Logger
{
public void interceptor()
{
System. out.println("logging" );
}
}
===========================================
/**
* 安全管理
*/
public class Security
{
public void interceptor()
{
System. out.println("security" );
}
}
===========================================
/**
* 权限管理
*/
public class Privilege{
private String access;
public String
getAccess() {
return access ;
}
public void setAccess(String
access) {
this.access =
access;
}
public void interceptor(ProceedingJoinPoint
joinPoint) {
if("admin" .equals(this.access)){
try {
joinPoint.proceed();
} catch (Throwable
e) {
e.printStackTrace();
}
} else{
System. out.println("对不起,没有权限查看...." );
}
}
}
===========================================
/**
* 目标类
*/
public class SalaryManagerImpl implements SalaryManager{
@Override
public void showSalary()
{
System. out.println("正在查看工资" );
}
}
===========================================
public class SalaryTest
{
@Test
public void test(){
ApplicationContext context= new ClassPathXmlApplicationContext("cn/itheima03/spring/aop/multiaspect/applicationContext.xml" );
SalaryManager salarmManager=(SalaryManager) context.getBean("salaryManager" );
salarmManager.showSalary();
}
}
===========================================
|
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" >
<!--目标类, 切面类,aop
-->
<bean id="salaryManager" class="cn.itheima03.spring.aop.multiaspect.SalaryManagerImpl" ></bean>
<bean id="logger" class="cn.itheima03.spring.aop.multiaspect.Logger" ></bean>
<bean id="security" class="cn.itheima03.spring.aop.multiaspect.Security" ></bean>
<bean id="privilege" class="cn.itheima03.spring.aop.multiaspect.Privilege" >
<property name= "access" value="admin" ></property>
</bean >
<aop:config >
<!--切入点 -->
<aop:pointcut expression="execution(*
cn.itheima03.spring.aop.multiaspect.SalaryManagerImpl.*(..))" id ="sm"/>
<!-- 切面 -->
<aop:aspect ref= "logger">
<aop:before method= "interceptor" pointcut-ref="sm" />
</aop:aspect>
<aop:aspect ref= "security">
<aop:before method= "interceptor" pointcut-ref="sm" />
</aop:aspect>
<aop:aspect ref= "privilege">
<!--环绕切入点
-->
<aop:around method= "interceptor" pointcut-ref="sm" />
</aop:aspect>
</aop:config >
</beans>
|
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/damogu_arthur/article/details/46988379