SpringAOP
AOP一一面向切面编程,编程的关注点不同
- Aspect(切面) : Aspect声明类似于java中的类声明 , 在Aspect中会包含着一些 Pointcut以及对应的Advice
- JoingPoint (连接点) : 表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,他自身还可以嵌套其他jointpoint.
- Pointcut (切点) : 表示一组jointpoint,这些jiontpoint或者通过逻辑关系组合起来,或者是通过配置 , 正则表达式等方式集中起来,他定义了相应的Advice将要发生的地方.
- Advice (增强) : Advice定义了再pointcut里面定义的程序点具体要做的操作 , 他通过before,after和around来区别实在每个jointpoint之前,之后还是代替执行的代码.
- Target (目标对象) : 织入advice的目标对象.
- Waecing (织入) : 将asoect和其他对象连接起来,并创建Advice d object的过程.
Spring常用注解
- component (意思为组件)
- controller (springmvc controlle层)
- service (业务层)
- repositoty (dao层)
AOP.XML版配置步骤
-
xml配置激活
-
注册一个切面要是用的类
-
配置切入点等等信息
-
配置测试类(service层)
<!-- 1.aop是基于代理完成的,所以我们要激活我们的自动代理--> <aop:aspectj-autoproxy/> <!-- 2.注册一个切面要使用的类 --> <bean class="com.leike.advice.BeforeAdice" id="beforeAdice"> </bean> <!-- 3.配置切入点等等信息--> <aop:config> <aop:aspect id="beforeAspect" ref="beforeAdice"> <!-- aop:before 表明他确实是前置通知 method:指明他使用哪个方法来切。 pointcut:切入点 你要什么包下面的什么类下面的什么方法 pointcut="execution(* com.leike.service.*.*(..))" --> <aop:before method="methodBefore" pointcut="execution(void com.leike.service.ProviderService.add())"></aop:before> </aop:aspect> </aop:config> <bean class="com.leike.service.ProviderService" id="providerService"> </bean>
AOP-@注解版配置步骤
配置步骤
-
激活代理(和配置版相同)
-
配置基础扫描包(扫描路径下含有@Component注解的类)
-
配置service层的需要被切的类(非必须 , 和aop无直接关联,只是测试需要)
@Aspect //标记其为一个切面 @Component //标记这个类为Spring的一个组件,相当于在xml中注册了一个bean一样 public class AbanAdvice { @Before("execution(* com.leike.aban_service.AbanService.show(String))") public void before() { System.out.println("注解版的before,方法执行前"); } @Order(1) @Before("execution(* com.leike.aban_service.AbanService.show(String))") public void before2() { System.out.println(""); } @After("execution(* com.leike.aban_service.AbanService.show(String))") public void after(JoinPoint joinPoint) { System.out.println(joinPoint.getSignature().getName()); System.out.println(Arrays.toString(joinPoint.getArgs())); System.out.println("注解版的after,方法执行结束"); } // @AfterThrowing(value = "execution(* com.leike.aban_service.AbanService.throwmethon())",throwing = "throwMsg") // @AfterThrowing(pointcut="execution(* com.leike.aban_service.*(..))", throwing= "error") @AfterThrowing(throwing="ex" , pointcut="execution(* com.leike.aban_service.AbanService.throwmethon())") public void afterThrowing(JoinPoint joinPoint,Throwable ex){ System.out.println("异常之后"+joinPoint.getSignature().getName()); System.out.println("异常信息"+ex); } }
常用注解及解释
-
@Aspect
-
@Component
Spring 中提供@Component 的三个衍生注解:(功能目前来讲是一致的) @Controller:WEB层 @Service:业务层 @Repository:持久层 这三个注解是为了让标注类本身的用途清晰,Spring后续会增强其功能 -
@Before
-
@After
-
@Order
-
@AfterThrowing
-
@around
execution表达式
先写访问修饰符 包名的限定 类目 方法名 参数列表 + 组合条件符合 , 同时符合两个条件,多个条件都可以.
publice com.leike..*.*(java.lang.String)
访问修饰符为public的并且是leike这个包下面的任意的类的任意的方法的参数为一个,并且类型为String的方法,就可以切到
public com.leike.*.*()
protected com.leike.*.*()
protected com.leike.*.*.*(java.lang.Integer,...)
Spring 注解配置类
-
Configuration : 标记一个类要给配置类 , 然后程序启动的时候只要扫描这个类 , 就可以清楚所有的哦诶之规则
-
Comonent: 表明一个类为Spring的一个组件,可以被Spring的一个组件 , 可以被spring容器所管理 , 他是要给普通组件的语义
-
Service: 同上,语义上属于dao层.
-
Repository: 同上,语义上属于DAO层
-
Controller : 同上 , 语义上属于控制层
-
ComponentScan : 组件扫描 , 可以绝对去扫描那些包.
@Configuration @ComponentScan(value = "com.leike.zhujiekaifa") public class SpringConfig { @Bean("dog") public Dog createDog() { Dog dog = new Dog(); dog.setName("大黄狗"); return dog; } -
Bean : 用于在Spring容器当中注册一个Bean.
@Component(value = "girl") public class Girl {} -
Autowired : 用于自动注入组件
@Service("billService") public class BillService { @Autowired //自动注入 private BillDao billDao; public void pay(double money){ System.out.println("Bill Service pay..."); billDao.pay(money); }
测试
-
方式1 通过注解在实体类上 然后自动注入来完成
@Test public void t3(){ ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); BillService billService = context.getBean("billService", BillService.class); billService.pay(4234.13); } -
方式二 通过配置资源类 来实例化声明(不同于xml 不需要用反射)
@Test
public void t2(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
BillService billService = context.getBean("billService", BillService.class);
BillDao billDao = context.getBean("billDao", BillDao.class);
billService.setBillDao(billDao);
billService.pay(4234.13);
}
注意 XML和注解方式的区别
-
读取配置文件的时候
-
注解方式
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); -
XML方式
ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
-
-
激活代理 和 配置基础扫描包 的时候
-
注解方式
@Configuration @ComponentScan( value = "com.leike") @EnableAspectJAutoProxy public class SpringConfig { } -
xml方式
<!--第一步,激活代理--> <aop:aspectj-autoproxy/> <!--第二步,配置基础扫描包--> <context:component-scan base-package="com.leike"/>
-
本文详细介绍了Spring AOP(面向切面编程)的核心概念,包括切面、连接点、切点、增强等,并展示了如何使用XML和注解两种方式进行AOP配置。同时,列举了Spring框架中常用的注解及其功能,帮助读者理解AOP在实际项目中的应用。
1172

被折叠的 条评论
为什么被折叠?



