Spring 基于注解的 IOC 与 AOP

时间:2022-07-26
本文章向大家介绍Spring 基于注解的 IOC 与 AOP,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本篇博客为基于注解的简单示例,IOC 详细介绍请移步 ☞ Spring 基于 XML 的 IOC,AOP 详细介绍请移步 ☞ Spring 基于 XML 的 AOP,对于注解的解释请移步 ☞ Spring 基本注解

1.1 基于注解的 IOC

1.1.1 开启注解扫描

<?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"
       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">

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

</beans>

1.1.2 控制反转

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description dao 接口
 */
public interface DemoDao {
    String get();
}
/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description dao 实现类
 */
@Repository
public class DemoDaoImpl implements DemoDao {
    public String get() {
        return "哟哟, 切克闹!";
    }
}
/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description 测试类
 */
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:application.xml")
public class DemoController {

    @Autowired
    private DemoDao demoDao;

    @Test
    public void get() {
        System.out.println(demoDao.get());
    }
}

1.2 基于注解的 AOP

1.2.1 开启注解扫描及自动代理

<?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">

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.softrware.spring"></context:component-scan>

    <!-- 开启自动代理 -->
    <aop:aspectj-autoproxy />

</beans>

1.2.2 目标类

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description 目标接口
 */
public interface MyTarget {
    void run();
}
/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description 目标实现类
 */
@Component
public class MyTargetImpl implements MyTarget {
    public void run() {
        System.out.println("run...");
    }
}

1.2.3 切面类

名称

注解

说明

前置通知

@Before

用于配置前置通知。指定增强的方法在切入点方法之前执行

后置通知

@AfterReturning

用于配置后置通知。指定增强的方法在切入点方法之后执行

环绕通知

@Around

用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行

异常抛出通知

@AfterThrowing

用于配置异常抛出通知。指定增强的方法在出现异常时执行

最终通知

@After

用于配置最终通知。无论增强方式执行是否有异常都会执行

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description 切面类
 */
@Component
@Aspect
public class MyAspect {

    @Before("execution(* com.softrware.spring.aop.domain..*.*(..))")
    public void before() {
        System.out.println("前置方法");
    }

}

1.2.4 抽取切点表达式

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description 切面类抽取切点表达式
 */
@Component
@Aspect		// 标明该类为切面类
public class MyAspect {

	// 抽取切点表达式
    @Pointcut("execution(* com.softrware.spring.aop.domain..*.*(..))")
    public void pointCut() {}

    @Before("MyAspect.pointCut()")
    public void before() {
        System.out.println("前置方法");
    }
}

1.2.5 测试类

/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description 测试类
 */
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:application.xml")
public class RunDemo {

    @Autowired
    private MyTarget myTarget;

    @Test
    public void run() {
        myTarget.run();
    }
}