前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 基于注解的 IOC 与 AOP

Spring 基于注解的 IOC 与 AOP

作者头像
Demo_Null
发布2020-09-28 14:29:59
4020
发布2020-09-28 14:29:59
举报
文章被收录于专栏:Java 学习Java 学习

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

1.1 基于注解的 IOC

1.1.1 开启注解扫描
代码语言:javascript
复制
<?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 控制反转
代码语言:javascript
复制
/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description dao 接口
 */
public interface DemoDao {
    String get();
}
代码语言:javascript
复制
/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description dao 实现类
 */
@Repository
public class DemoDaoImpl implements DemoDao {
    public String get() {
        return "哟哟, 切克闹!";
    }
}
代码语言:javascript
复制
/**
 * 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 开启注解扫描及自动代理
代码语言:javascript
复制
<?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 目标类
代码语言:javascript
复制
/**
 * Created with IntelliJ IDEA.
 *
 * @author Demo_Null
 * @date 2020/8/21
 * @description 目标接口
 */
public interface MyTarget {
    void run();
}
代码语言:javascript
复制
/**
 * 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

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

代码语言:javascript
复制
/**
 * 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 抽取切点表达式
代码语言:javascript
复制
/**
 * 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 测试类
代码语言:javascript
复制
/**
 * 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();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/08/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.1 基于注解的 IOC
    • 1.1.1 开启注解扫描
      • 1.1.2 控制反转
      • 1.2 基于注解的 AOP
        • 1.2.1 开启注解扫描及自动代理
          • 1.2.2 目标类
            • 1.2.3 切面类
              • 1.2.4 抽取切点表达式
                • 1.2.5 测试类
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档