spring07

Aop的作用:在程序运行期间,不修改源码对已有方法进行增强

Aspect 切面 :切入点(要增强的方法)和通知(怎么增强)之间的关系
连接点: 可以通过jdk代理的方法
切入点: 需要被增强的方法
Advice: 增强
Target:目标对象
Proxy:代理

aop: config 用于声明开始aop的配置
aop:aspect 配置切面 (配置切入点和终端之间的关系)
aop:pointcut 配置切入点表达式
aop:before 用于配置前置通知
aop:after-returning 后置通知
aop: after-throwing 异常通知
aop: after 最终通知
aop: around 环绕通知

AccountDaoImpl .java

package com.bky.dao.impl;

import com.bky.dao.AccountDao;

public class AccountDaoImpl implements AccountDao {
    @Override
    public void add() {
        int  a=1/0;
        System.out.println("执行添加功能");
    }

    @Override
    public void delete() {
        System.out.println("执行删除功能");

    }

    @Override
    public void update() {
        System.out.println("执行修改功能");

    }

    @Override
    public void select() {
        System.out.println("执行查询功能");

    }
}

AccountDao .java

package com.bky.dao;

public interface AccountDao {
    void  add();
    void delete();
    void update();;
    void select();
}

AccountLog .java

package com.bky.log;

/**
 * 日志配置类
 */
public class AccountLog {

    //前置通知
    public void Qianzhi(){
        System.out.println("前置通知开始进入日志");
    }
    //后置通知
    public void Houzhi(){
        System.out.println("后置通知开始记录日志");
    }
    //异常通知
    public void Yichang(){
        System.out.println("捕获到异常日志");
    }
    //最终通知
    public void Zuizhong(){
        System.out.println("最终通知执行了");
    }
}

AccountService .java

package com.bky.service;

public interface AccountService {
    void  add();
    void delete();
    void update();;
    void select();
}

AccountServiceImpl .java

package com.bky.service.impl;

import com.bky.dao.AccountDao;
import com.bky.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void add() {
        accountDao.add();
    }

    @Override
    public void delete() {
        accountDao.delete();
    }

    @Override
    public void update() {
        accountDao.update();
    }

    @Override
    public void select() {
        accountDao.select();
    }
}

Test .java

package com.bky.test;

import com.bky.dao.AccountDao;
import com.bky.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public  void  testo1(){
        ApplicationContext applicationContext
                =new ClassPathXmlApplicationContext("xml/beans.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        AccountDao accountDao = applicationContext.getBean("accountDao", AccountDao.class);
        System.out.println(accountDao);
        System.out.println(accountService);
    }
    @org.junit.Test
    public  void  test02(){
        ApplicationContext applicationContext
                =new ClassPathXmlApplicationContext("xml/beans.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        accountService.add();
        accountService.delete();
        accountService.select();
        accountService.update();;
    }
}

beans.xml

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置service实例-->
    <bean id="accountService" class="com.bky.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <bean id="accountDao" class="com.bky.dao.impl.AccountDaoImpl">
    </bean>

<!--
<aop:config> 用于声明aop配置
<aop:aspect> 配置切面 (想要增强的通知)
aop:pointcut  配置切入点表达式
aop:before 用于配置前置通知
-->
    <!--切入点表达式-->
    <bean id="accountlog" class="com.bky.log.AccountLog">
    </bean>

    <aop:config >
        <!--配置切入点表达式 -->
        <aop:pointcut id="pt01" expression="execution(public void com.bky.service.impl.AccountServiceImpl.*(..))"/>
        <!--配置切面-->
        <aop:aspect id="logaspet " ref="accountlog">
            <!--配置前置通知-->
            <aop:before method="Qianzhi" pointcut-ref="pt01"></aop:before>
            <!--配置后置通知-->
            <aop:after-returning method="Houzhi" pointcut-ref="pt01"></aop:after-returning>
            <!--异常通知-->
            <aop:after-throwing method="Yichang" pointcut-ref="pt01"></aop:after-throwing>
            <!--最终通知-->
            <aop:after method="Zuizhong" pointcut-ref="pt01"></aop:after>
        </aop:aspect>
    </aop:config>

</beans>

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中,有多种方式可以实现定时任务调度。其中一种方式是使用Spring Task提供的定时任务功能。另外还有其他的定时任务框架,如Quartz和xxl-job。Spring Task是一种功能简单的定时任务框架,可以通过在配置文件中添加约束文件来启用它。在spring-mvc.xml配置文件中,可以添加以下约束文件来启用Spring Task的定时任务功能: ```xml xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ``` 接下来,需要配置定时任务的具体执行方法。可以通过在配置文件中添加以下代码来配置定时任务的执行方法: ```xml <bean id="adminJob" class="com.xxx.xxx.AdminJob"/> <!--此处id值为需要执行的定时任务方法名--> <bean id="job1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="adminJob"/> <property name="targetMethod" value="job1"/> </bean> ``` 以上代码中,`adminJob`是定时任务的执行类,`job1`是需要执行的具体方法。 最后,需要配置定时任务的触发器。可以通过在配置文件中添加以下代码来配置定时任务的触发器: ```xml <bean id="job1Trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="job1"/> </property> <property name="cronExpression"> <value>0 15 0 16 * ?</value> </property> </bean> ``` 以上代码中,`job1Trigger`是定时任务的触发器,`cronExpression`指定了定时任务的执行时间表达式。 这样,就完成了Spring定时任务的调试配置。可以根据具体的需求和业务逻辑来配置定时任务的执行方法和触发器。 #### 引用[.reference_title] - *1* [22-09-29 西安 谷粒商城(07)分布式定时任务xxl-job、Cron表达式、springTask定时任务、订单业务](https://blog.csdn.net/m0_56799642/article/details/127109242)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [spring配置定时任务的几种方式](https://blog.csdn.net/kaicen/article/details/114886381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值