Java_Spring:10. 基于注解的 AOP 配置

目录

1 环境搭建

1.1 第一步:准备必要的代码和 jar 包

1.2 第二步:在配置文件中导入 context 的名称空间

1.3 第三步:把资源使用注解配置

1.4 第四步:在配置文件中指定 spring 要扫描的包

2 配置步骤

2.1 第一步:把通知类也使用注解配置

2.2 第二步:在通知类上使用@Aspect 注解声明为切面

2.3 第三步:在增强的方法上使用注解配置通知

2.3.1 @Before

2.3.2 @AfterReturning

2.3.3 @AfterThrowing

2.3.4 @After

2.4 第四步:在 spring 配置文件中开启 spring 对注解 AOP 的支持

3 环绕通知注解配置 @Around

4 切入点表达式注解 @Pointcut

5 不使用 XML 的配置方式


  • 1 环境搭建

    • 1.1 第一步:准备必要的代码和 jar 包

      • 拷贝上一小节的工程即可。
    • 1.2 第二步:在配置文件中导入 context 的名称空间

      • <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">
        
            <!-- 配置数据库操作对象 -->
            <bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
                <property name="dataSource" ref="dataSource"></property>
                
                <!-- 指定 connection 和线程绑定 -->
                <property name="useCurrentConnection" value="true"></property>
            </bean>
        
            <!-- 配置数据源 -->
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property>
                <property name="user" value="root"></property>
                <property name="password" value="1234"></property>
            </bean>
        </beans>
    • 1.3 第三步:把资源使用注解配置

      • 账户的业务层实现类
      • @Service("accountService")
        public class AccountServiceImpl implements IAccountService {
        
            @Autowired
            private IAccountDao accountDao;
        }

      • 账户的持久层实现类
      • @Repository("accountDao")
        public class AccountDaoImpl implements IAccountDao {
            @Autowired
            p
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值