ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot学习-0225

2022-02-25 19:00:25  阅读:108  来源: 互联网

标签:10 15 springboot 触发 0225 springframework 学习 import public


1.自定义注解

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {

     String value() default "";
}

2.SpringBoot使用AOP统一处理请求日志

 /**
     *切入点定义:拦截所有@Log注解的
     */
    @Pointcut("@annotation(com.entor.annotation.Log)")
    public void log(){}

    @Around("log()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long begin = System.currentTimeMillis();

        Signature signature = joinPoint.getSignature();

        if (signature instanceof MethodSignature){
            //如果注解作用在方法上,是一个MethodSignature对象
            ProceedingJoinPoint pjp;
            MethodSignature methodSignature = (MethodSignature)signature;
            //获取当前方法对象
            Method method = methodSignature.getMethod();
            Log logs = method.getAnnotation(com.entor.annotation.Log.class);
            log.info("注解的内容 :" + logs.value());
        }
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        log.info("请求地址URL : " + request.getRequestURL().toString());
        log.info("请求方法HTTP_METHOD : " + request.getMethod());
        log.info("请求IP地址 : " + request.getRemoteAddr());
        Enumeration<String> enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            String name = (String) enu.nextElement();
            log.info("参数名称name:{},参数值value:{}", name, request.getParameter(name));
        }
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        log.info("方法执行时间毫秒数 :" + (end-begin));
        //处理完请求,返回内容
        log.info("返回结果 :" + result);
        return result;
    }



}

3.SpringBoot定时任务

@Scheduled

/**
*   一、结构
*   corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份
* 注意事项:
*
*   每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:
*
*   (1)*:表示匹配该域的任意值。假如在Minutes域使用*, 即表示每分钟都会触发事件。
*
*   (2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
*
*   (3)-:表示范围。例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
*
*   (4)/:表示起始时间开始触发,然后每隔固定时间触发一次。例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
*
*   (5),:表示列出枚举值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
*
*   (6)L:表示最后,只能出现在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
*
*   (7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 。
*
*   (8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
*
*   (9)#:用于确定每个月第几个星期几,只能出现在DayofWeek域。例如在4#2,表示某月的第二个星期三。
*   三、常用表达式例子
*   (0)0/20 * * * * ? 表示每20秒 调整任务
*   (1)0 0 2 1 * ? 表示在每月的1日的凌晨2点调整任务
*   (2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
*   (3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
*   (4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
*   (5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
*   (6)0 0 12 ? * WED 表示每个星期三中午12点
*   (7)0 0 12 * * ? 每天中午12点触发
*   (8)0 15 10 ? * * 每天上午10:15触发
*   (9)0 15 10 * * ? 每天上午10:15触发
*   (10)0 15 10 * * ? * 每天上午10:15触发
*   (11)0 15 10 * * ? 2005 2005年的每天上午10:15触发
*   (12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
*   (13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
*   (14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
*   (15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
*   (16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
*   (17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
*   (18)0 15 10 15 * ? 每月15日上午10:15触发
*   (19)0 15 10 L * ? 每月最后一日的上午10:15触发
*   (20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
*   (21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
*   (22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
*/
package com.entor.scheduledTest;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author 556
 * @version 1.0.0
 * @Description
 * @date 2022年02月25日周五
 */
@Service
public class ScheduledService {


    @Scheduled(cron = "0 0/10 8 ? * 2-6" )//设定闹钟定时任务,周一到周五早上8点,每隔10分钟再响
    public void clock() {
        System.out.println("该起床了,上班了");
    }

}

4.SpringBoot异步调用Async

这个和定时器差不多,启动加上@EnableAsync ,只需要在我们需要异步的方法上面加上@Async注解

package com.entor.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author 556
 * @version 1.0.0
 * @Description
 * @date 2022年02月25日周五
 */
@Service
public class AsyncService {

    @Async//异步执行的注解
    public void test1() throws InterruptedException {
        System.out.println("AsyncService test1");
        Thread.sleep(1000);
    }
    @Async
    public void test2() throws InterruptedException {
        System.out.println("AsyncService test2");
        Thread.sleep(2000);
    }
    @Async
    public void test3() throws InterruptedException {
        System.out.println("AsyncService test3");
        Thread.sleep(3000);
    }
}

5.SpringBoot自定义参数获取

server.port=8090

student.id=1
student.name=张三
student.sex=男
student.age=20
#map对象
student.address.home=南宁市大学路2号
student.address.work=南宁市滨湖路3号
#集合/数值对象
student.courses=语文,数学,英语
package com.entor.controller;

import com.entor.config.StudentConfig;
import com.entor.config.TeacherConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 556
 * @version 1.0.0
 * @Description
 * @date 2022年02月25日周五
 */
@RestController
public class PropertiesController {

    /**
     * 获取配置文件application.properties文件中自定义的参数配置
     */
    @Value("${student.id}")
    private Integer id;
    @Value("${student.name}")
    private String name;
    @Value("${student.sex}")
    private String sex;
    @Value("${student.age}")
    private Integer age;

    @Autowired
    private StudentConfig studentConfig;
    @Autowired
    private TeacherConfig teacherConfig;
    @GetMapping(value = "/get")
    public String get(){
        return "编号:" + id + "姓名:" + name + "性别:" + sex + "年龄:" + age;
    }

    @GetMapping(value = "/getStudentConfig")
    public String getStudentConfig(){
        return ",编号:" + studentConfig.getId() +
                ",姓名:" + studentConfig.getName() +
                ",性别:" + studentConfig.getSex() +
                ",年龄:" + studentConfig.getAge() +
                ",地址:" + studentConfig.getAddress() +
                ",课程" + studentConfig.getCourses();
    }
}

6.SpringBoot配置文件yml

book:
  id: 2
  name: 三国演义
  price: 90
  author: 罗贯中
  publisher: 清华大学出版社,人民出版社
  sale-type:
    online: 网购
    offline:
      - id: 1
        name: 新华书店大学路分店
        phone: 187646654
        address: 南宁市大学路10号
      - id: 2
        name: 新华书店鲁班路分店
        phone: 1876465244
        address: 南宁市鲁班路10号
package com.entor.config;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

/**
 * @author 556
 * @version 1.0.0
 * @Description
 * @date 2022年02月25日周五
 */
@Configuration
public class YamlConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        // class引入
        yaml.setResources(new ClassPathResource("customer.yml"));
        configurer.setProperties(yaml.getObject());
        return configurer;
    }
}

 




标签:10,15,springboot,触发,0225,springframework,学习,import,public
来源: https://www.cnblogs.com/fanglongsheng/p/15937423.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有