第一个完全注解使用aop例子(不使用配置)

1.spring 3.0 增加了---->AnnotationConfigApplicationContext初始化容器方法(之前我们常用Applicationcontext 这个摆脱不了xml配置)

2.有了AnnotationConfigApplicationContext---------->增加了@Configuration和@Bean(共同使用)

3.@Configuration等于@Component

4.@Bean等于bean配置方法,同时@Bean方法必须公开,有返回值

5.@Bean的属性有四:1.name;2.initMethod;3.destoryMethod;4.autowire (作用与xml差不多,仅4没有了constructor寻找方法)

6.看以下代码:

组件1:

package test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Configuration
public class person {
    @Bean
    public person demo() {
        System.out.println("人造1号初始化完毕!");
        return new person();
    }
}

组件2:
package test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Configuration
public class person2 {
    
    @Bean
    public person2 demo1() {
        System.out.println("人造2号初始化完毕!");
        return new person2();
    }
}

运行类:
package test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class aac {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext aac=new AnnotationConfigApplicationContext("test");
    
    }    
}


运行结果:

需要注意的是AnnotationConfigApplicationContext  aac = new AnnotationConfigApplicationContext("test");  //参数部分是写进包名的,而不像ApplicationContext的classpathxmlapplication("配置文件名"); 


总结:第一次无需配置进行aop测试,感觉非常好!!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Spring注解实现AOP例子: 首先,在你的Spring配置文件中,需要开启AOP支持: ``` <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> ``` 然后,创建一个切面类,使用@Aspect注解标注: ``` @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Before executing " + joinPoint.getSignature().getName()); } @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void logAfterReturning(JoinPoint joinPoint, Object result) { System.out.println("After executing " + joinPoint.getSignature().getName() + ", result is " + result); } @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "exception") public void logAfterThrowing(JoinPoint joinPoint, Exception exception) { System.out.println("Exception thrown from " + joinPoint.getSignature().getName() + ", exception is " + exception); } } ``` 在上面的例子中,我们定义了三个通知,分别在方法执行前、执行后和抛出异常时执行。我们使用@Pointcut注解定义了一个切点,它匹配com.example.service包中的所有方法。然后,在通知方法上使用@Before、@AfterReturning和@AfterThrowing注解,分别表示在方法执行前、执行后和抛出异常时执行。 最后,在你的服务类中,使用@Loggable注解标注需要记录日志的方法: ``` @Service public class MyService { @Loggable public void doSomething() { // ... } } ``` 这样,在执行doSomething方法时,LoggingAspect中定义的通知方法就会被自动调用,从而实现AOP的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值