AOP——Annotation配置

本文介绍如何使用Spring AOP及AspectJ进行注解配置,实现方法调用前的日志记录功能。通过具体示例代码,展示了配置过程、关键概念如JoinPoint、Pointcut等,并介绍了基于接口和非接口类的代理实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AOP——Annotation配置 

1.实现AOPAnnotation配置,需要在配置文件中,配置如下:<aop:aspectj-autoproxy />,但是在配置文件中还需要加入aop的命名空间。
xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

2.Aspectj是专门用于产生代理的框架

3.小实验如下:

a) 定义一个日志逻辑LogInterceptor

package com.zgy.aop;

 

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

 

@Aspect

@Component

public class LogInterceptor {

//织入点语法,指定从何处织入

@Before("execution(public void com.zgy.impl.UserDAOImpl.save(com.zgy.model.User))")

public void before(){

System.out.println("method start ...");

}

}

b) 定义UserDAO

package com.zgy.dao;

import com.zgy.model.User;

public interface UserDAO {

public void save(User u);

}

c) 定义UserDAOImpl

package com.zgy.impl;

import org.springframework.stereotype.Component;

import com.zgy.dao.UserDAO;

import com.zgy.model.User;

@Component("u")

public class UserDAOImpl implements UserDAO {

public void save(User u) {

System.out.println("user saved");

}

}

d) 定义UserService

package com.zgy.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.zgy.dao.UserDAO;

import com.zgy.model.User;

@Component("userService")

public class UserService {

private UserDAO userDAO;

public void init(){

System.out.println("init");

}

public void add(User u){

this.userDAO.save(u);

}

 

public UserDAO getUserDAO() {

return userDAO;

}

@Resource(name="u")

public void setUserDAO(UserDAO userDAO) {

this.userDAO = userDAO;

}

public void destory(){

System.out.println("destroy");

}

}

 

e) 定义配置文件bean.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: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/context

           http://www.springframework.org/schema/context/spring-context-2.5.xsd

           http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<context:annotation-config />

<context:component-scan base-package="com.zgy"/>

  <aop:aspectj-autoproxy />

</beans>

f) 测试:

package com.zgy.service;

import org.junit.Test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zgy.model.User;

public class UserServiceTest {

@Test 

public void testAdd() throws Exception {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

UserService service = (UserService)ctx.getBean("userService");

service.add(new User());

}

}

g) 运行结果:

method start ...

user saved

4)相关概念:

AJoinPoint:连接点。指定在哪些连接点上织入切面逻辑

B、Pointcut:joinpoint的集合,带有一个名字

CAspect:切面,加入的切面逻辑就是切面,即此处的LogInterceptor

DAdvice:简单的理解为方法或所加入的逻辑出的简易,即此处的@Before

ETarget:被代理对象

FWeaver:织入。

5)如果一个类实现了接口,那么将由JDK产生代理。而Spring中的类可以不实现接口,所以也此时如果要产生代理,就要用到CGLIB这个jar包,通过直接操纵二进制码的形式产生代理。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值