AOP-Demo例子

一、添加依赖
AOP的实现基于Aspect框架的实现

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
        <version>1.8.9</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>  
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

1.1基于xml实现的方法

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 

    <!-- 核心业务对象 -->
    <bean id="helloService"
          class="spring.beans.HelloServiceImpl"/>
<!-- 配置非核心业务对象(日志处理对象):切面 -->
<bean id="log" 
          class="spring.aop.LoggingAspect"/>
    <!-- AOP配置(切入点,切面) -->  
    <aop:config>
       <!-- 配置切入点(bean,@annotation,within) -->
       <aop:pointcut 
            expression="within(spring.beans.HelloServiceImpl)" 
            id="logPointCut"/>
       <!-- 配置日志处理 -->
       <aop:aspect ref="log" >
           <aop:before method="beforeMethod" 
                       pointcut-ref="logPointCut"/>
           <aop:after  method="afterMethod"
                       pointcut-ref="logPointCut"/>
       </aop:aspect>
    </aop:config>
</beans>

基于xml方式注解配置的测试实现

public class TestAOP01 {

    public static void main(String[] args) {
        //1.初始化容器
        ClassPathXmlApplicationContext ctx=
        new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        //2.获取bean对象
        OrderService os=(OrderService)
        ctx.getBean("orderService", 
                OrderService.class);
        //3.执行业务方法
        os.saveOrder();
        os.deleteOrder();
        //4.释放资源
        ctx.close();
    }
}

1.2在类中基于注解方式的配置

@ComponentScan("com.spring.beans")
@EnableAspectJAutoProxy
public class AppConfig {

}

基于注解方式配置的测试实现

public class TestAOP02 {
    public static void main(String[] args) {
        //1.初始化容器对象
        AnnotationConfigApplicationContext ctx=
        new AnnotationConfigApplicationContext(
                AppConfig.class);
        //2.获取Bean对象
        OrderService orderService=
        ctx.getBean("orderService", OrderService.class);
        //3.执行业务
        orderService.saveOrder();
        //orderService.deleteOrder();
        //4.释放资源
        ctx.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring AOP (Aspect Oriented Programming) 是一种编程范式,它通过将应用的业务逻辑和系统关注点(如日志、事务管理等)分离,提高了代码的可维护性和复用性。下面是一个简单的Spring AOP切面(Aspect)的Demo示例: 首先,你需要在Spring配置文件中启用AOP支持,并定义一个切面(Aspect): ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aspectj="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 启用AOP --> <aop:aspectj-autoproxy/> <!-- 定义切面 --> <bean id="myAspect" class="com.example.MyAspect"> <!-- 配置通知(advice) --> <property name="beforeAdvice" ref="beforeAdvice"/> </bean> <!-- 定义通知 --> <bean id="beforeAdvice" class="com.example.BeforeAdvice"/> </beans> ``` 然后,创建一个`MyAspect`切面类,通常包含通知(advice),例如前置通知(BeforeAdvice): ```java import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class MyAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { // 在方法执行前添加的操作,如日志记录 System.out.println("Method " + joinPoint.getSignature() + " is about to execute."); } } ``` 在这个例子中,`@Before`注解定义了一个前置通知,它将在`com.example.service`包下的所有方法执行前执行。 接下来,创建`BeforeAdvice`类,这是一个具体的通知实现: ```java public class BeforeAdvice { // 可能包含一些自定义逻辑,比如参数检查或资源获取 } ``` 相关问题--: 1. Spring AOP中的通知有哪些类型? 2. `@Aspect`注解在Spring AOP中的作用是什么? 3. 如何在Spring中配置切点(execution表达式)?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值