有这么一个需求 每个部门,需要操作的数据库不同,A部门要将数据放test数据库,B 部门数据 要放在test1数据库 同一个项目 需要整合 多个数据源

上传个demo 方便自己以后回看!!!!!!!!!

https://github.com/SCchengbo/springboot-mybatis-demo.git

  1. springboot使用AOP 过滤请求:
    在pom文件中导入 aop所需要的依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
  2. 定义切面
    package com.springboot.aop;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component; @Aspect
    @Component
    public class testAspect {
    private Logger logger = LoggerFactory.getLogger(getClass()); @Pointcut("execution(public * com.springboot.controller..*.*(..))")
    public void testPoint() {
    } @Before(value = "testPoint()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    logger.info("前置通知");
    } @AfterReturning(returning = "ret", pointcut = "testPoint()")
    public void doAfterReturning(Object ret) throws Throwable {
    // 处理完请求,返回内容
    logger.info("RESPONSE : " + ret);
    }
  3.  @Pointcut("execution(public * com.springboot.controller..*.*(..))") 所有反问com.springboot.controller包下的请求 都 会被切面拦截 执行路径 http://localhost:8080/select?name=zhangsan
    2019-02-11 16:09:22.362 [http-nio-8080-exec-1] INFO  com.springboot.aop.testAspect:33 - 前置通知
    2019-02-11 16:09:22.366 [http-nio-8080-exec-1] INFO com.springboot.controller.Controller:15 - 接受到请求
    2019-02-11 16:09:22.366 [http-nio-8080-exec-1] INFO com.springboot.aop.testAspect:39 - RESPONSE : zhangsan

关于 springboot 使用@Scheduled 做定时任务:

  1. package com.springboot.scheduledtasks;
    
    import java.text.SimpleDateFormat;
    import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component; @Component
    public class ScheduledTasks {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
    System.out.println("任务1:" + dateFormat.format(new Date()));
    } }
  2. 在启动类上面 必须添加@EnableScheduling 注解 不然 定时任务 不起作用
    任务1:17:10:22
    任务1:17:10:27
  3. ScheduledTasks原理:

    简要介绍:spring在初始化bean后,通过“postProcessAfterInitialization”拦截到所有的用到“@Scheduled”注解的方法,并解析相应的的注解参数,放入“定时任务列表”等待后续处理;之后再“定时任务列表”中统一执行相应的定时任务(任务为顺序执行,先执行cron,之后再执行fixedRate)。

    重要代码如下:

    第一步:依次加载所有的实现Scheduled注解的类方法。

    第二步:将对应类型的定时器放入相应的“定时任务列表”中。

    第三步:执行相应的定时任务。

    参考资料:https://blog.csdn.net/gaodebao1/article/details/51789225
  4. ScheduledTasks是单线程执行任务,平常 使用定时任务 用的xxl  如果是想要实现多线程 定时任务 则要重写
    configureTasks方法
    package com.springboot.scheduledtasks;
    
    import java.util.concurrent.Executors;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar; @Configuration
    public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    // 设定一个长度10的定时任务线程池
    taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    } }
  5. 测试多线程执行:
    package com.springboot.scheduledtasks;
    
    import java.text.SimpleDateFormat;
    import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component; @Component
    public class ScheduledTasks {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
    System.out.println("任务1:" + dateFormat.format(new Date()));
    } @Scheduled(fixedRate = 5000)
    public void reportCurrentTime1() {
    System.out.println("任务2:" + dateFormat.format(new Date()));
    for (;;) { }
    } }
  6. 结果:
    任务1:17:18:32
    任务1:17:18:37
    任务1:17:18:42
    任务1:17:18:47
    任务1:17:18:52
ScheduledTasks平常用的比较少。都是公司内部自己封装的,也有类似xxl此类的定时任务

关于springboot 中 异步方法调用,原来 项目中 希望调用一个 不阻塞的方法的时候,是新创建一个线程 来 执行 需要的逻辑。。springboot 中直接可以使用@Async 标记需要异步执行的方法就可以了
package com.springboot.util;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; @Component
public class TestAsync {
@Async
public void testAsyncMethod() {
for (;;) { }
}
}
    @RequestMapping("/testAsync")
public void testAsync() {
testAsync.testAsyncMethod();
logger.info("测试异步调用");
}

一定需要在启动类中 添加@EnableAsync  注解  不然  异步方法调用 不起作用

springboot如何获取 properties文件中自定义的参数值

package com.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.springboot.util.TestAsync; @org.springframework.stereotype.Controller
public class Controller {
private static Logger logger = LoggerFactory.getLogger(Controller.class);
@Autowired
public TestAsync testAsync;
@Value("${time}")
public int time; @RequestMapping("/getTime")
@ResponseBody
public Integer getTime() {
return time; }

使用@Value注解

 @Value("${time}") 可以获取自定义的数据信息

springboot(整合多数据源demo,aop,定时任务,异步方法调用,以及获取properties中自定义的变量值)的更多相关文章

  1. springboot整合多数据源解决分布式事务

    一.前言        springboot整合多数据源解决分布式事务.             1.多数据源采用分包策略              2.全局分布式事务管理:jta-atomikos. ...

  2. springBoot整合多数据源

    springBoot整合相关 1:springBoot整合多数据源: 应用场景:     项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...

  3. 第九章 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  4. SpringBoot整合多数据源实现

    项目架构 1.导入相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  5. springboot整合多数据源及事物

    有两种方式:一种是分包的方式.一种是加注解的方式(@DataSource(ref="")). 分包方式:项目结构图如下: 分为com.itmayiedu.test01.com.it ...

  6. SpringBoot整合Druid数据源

    关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...

  7. springboot整合Quartz实现动态配置定时任务

    前言 在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功能. 一.新建一个springboot工程,并添加依赖 & ...

  8. 【第九章】 springboot + mybatis + 多数据源 (AOP实现)

    在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...

  9. spring-boot整合dubbo启动demo

    参考资料: https://docs.spring.io/spring-boot/docs/2.1.7.RELEASE/reference/html/ https://github.com/apach ...

随机推荐

  1. python性能分析——insert()

    我们在list中插入数据时,经常使用这两个函数: append():在列表的末尾增加一个数据 insert():在某个特定位置前加一个数据 Python内的list实现是通过数组实现的,而不是链表的形 ...

  2. https://blog.csdn.net/qq_35447305/article/details/78587691

    来源:https://blog.csdn.net/qq_35447305/article/details/78587691 需要去查看设置.C:\Users\用户名 目录下找到 .npmrc文件,删除 ...

  3. LG1861 星之器

    题意 题目背景 Magic Land 上的时间又过了若干世纪„„ 现在, 人们谈论着一个传说:从前,他们的祖先来到了一个位于东方的岛屿, 那里简直就是另外一个世界.善于分析与构造的 Magic Lan ...

  4. shell-url-decode

    查询每个小时的clk ,然后获取对应的字段 #!/bin/bash urldecode(){ echo -e "$(sed 's/+/ /g;s/%\(..\)/\\x\1/g;')&quo ...

  5. centos7安装部署mysql5.7服务器

    因为自带源没有最新版的mysql,所以我们需要自己下载rpm包,先下载下面的rpm包源 https://repo.mysql.com//mysql57-community-release-el7-11 ...

  6. 数学 它的内容,方法和意义 第一卷 (A. D. 亚历山大洛夫 著)

    第一章 数学概观 (已看) 1. 数学的特点 2. 算术 3. 几何 4. 算术和几何 5. 初等数学时代 6. 变量的数学 7. 现代数学 8. 数学的本质 9. 数学发展的规律性 第二章 数学分析 ...

  7. 用Python免费发短信,实现程序实时报警

    进入正文 今天跟大家分享的主题是利用python库twilio来免费发送短信. 先放一张成品图: 代码放在了本文最后的地址中 正文 眼尖的小伙伴已经发现了上面的短信的前缀显示这个短信来自于一个叫Twi ...

  8. Package has no installation candidate解决方法

    今天在安装软件的时候出现了Package has no installation candidate的问题,如:# apt-get install <packagename>Reading ...

  9. NET设计模式 第二部分 创建型模式(4):工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...

  10. MS Batch AI

    微软的Batch AI服务是一项新服务,它可以帮助你在GPU pool上训练和测试机器学习模型,包括深度学习模型.它简化了在当前许多流行的深度学习框架(如TensorFlow.Microsoft认知工 ...