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();
    }
}



  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值