注解方式实现IOC和AOP

 3.1 IOC和DI的注解

      IOC:

  • @Component:实现Bean组件的定义
  • @Repository:用于标注DAO类,功能与@Component作用相当
  • @Service:用于标注业务类
  • @Controller:用于标注控制器

      DI:

  •  @Resource(name="id") 默认ByName方式,如果name确实默认按照ByType方式注入
  •  @Autowired默认ByType方式,如果出现同名类,则不能按照Type进行注入需要使用@Qualifier 指明ID

 AOP:

  • @Aspect 声明切面
  • @Ponitcut 声明公共的切点表达式
  • @Before 前置增强
  • @AfterReturning 后置增强
  • @Around 环绕增强
  • @AfterThrowing 异常抛出增强
  • @After 最终增强

使用注解方法实现AOP的案例

第一步:xml文件中配置包扫描器和开启Spring对AOP注解的支持

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启Spring IOC的注解支持 base-package 包扫描语句 com.yjc包下的注解-->
    <context:component-scan base-package="com.yjc"/>
    <!--开启Spring AOP注解的支持-->
    <aop:aspectj-autoproxy/>
</beans>

第二步:创建业务类

package com.yjc.annotate;

import org.springframework.stereotype.Service;

@Service("doSomeServiceImpl")//把此类放入到spring容器中,标识这是个Service
public class DoSomeServiceImpl  {

    public void doSome() {
        System.out.println("doSome===============================");
    }


    public void say() {
        System.out.println("say==========================================");
    }
}
package com.yjc.annotate;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
@Aspect   //表示这个类是一个切面类
@Component  //必须要把切面类也放入到Bean容器当中
public class AnnotateAdvice {
   //定义切点,方法不需要实现 @Pointcut(
"execution(* com.yjc.annotate.*.*(..))") public void poincut(){ }
  //参数为切点 @Before(
"poincut()") public void Before(){ System.out.println("俺是前置增强"); } /* @AfterReturning public void AfterReturning(){ System.out.println("俺是后置增强"); } @After("poincut()") public void After(){ System.out.println("额是最终增强"); } @Around("poincut()") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("前置"); proceedingJoinPoint.proceed(); System.out.println("后置"); }*/ /* @AfterThrowing(pointcut = "poincut()", throwing = "e") public void execption(JoinPoint joinPoint,RuntimeException e){ System.out.println(joinPoint.getSignature().getName()+"方法发生:"+e); }*/ }

第四步:测试类

package com.yjc.annotate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotateTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/yjc/annotate/application_annotate.xml");
      //获取Service层的Bean对象 DoSomeServiceImpl proxyFactory
= applicationContext.getBean("doSomeServiceImpl", DoSomeServiceImpl.class); proxyFactory.doSome(); proxyFactory.say(); } }

第五步:测试

上面的一个小例子就是用我们注解实现IOC声明和AOP的增强,五种增强的实现方式大致一样,这里就不再赘述

猜你喜欢

转载自www.cnblogs.com/yjc1605961523/p/11778216.html
今日推荐