SpringBoot常用功能(容器启动执行,定时任务,配置转成java对象)

一.容器启动执行两种实现。

第一种: applicationrunner

 package com.hangzhou.runner;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//容器启动之后,执行一些操作
@Order(value=2)
@Component
public class runner02 implements ApplicationRunner {

	@Override
	public void run(ApplicationArguments args) throws Exception {
		
		System.out.println("Running runner02");
		
	}

}

第二种:CommandLineRunner

/**
 * 
 */
/**
 * @author Administrator
 *
 */
package com.hangzhou.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(value=1)
@Component
public class runner01 implements CommandLineRunner{

	@Override
	public void run(String... args) throws Exception {
		System.out.println("running eunner01");
	}

}

注:默认第一先执行。但可以@order设置执行顺序,小的执行。还有他们的方法传参不同,待续。。。。

二.读取配置文件生成Bean。 @ConfigurationProperties(prefix="adconf.mysql")

package com.hangzhou.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 读配置文件
 * @author Administrator
 *
 */
@Component
@ConfigurationProperties(prefix="adconf.mysql")
public class MySQLConfig {

	private String host;
	private Integer port;
	private String username;
	private String password;
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public Integer getPort() {
		return port;
	}
	public void setPort(Integer port) {
		this.port = port;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

配置文件

spring:
 profiles:
  active: prod

adconf:
 mysql:
  host:127.0.0.1
  port:3306
  username:root
  password:Djangobbs
  

spring:
 profiles:dev
 
server:
 port:8080

spring:
 profiles:test
 
server:
 port:8081

还可以设置配置环境切换spring.profiles.active=test

三.定时任务。


package com.hangzhou.schedule;

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

/**
 * 定时任务
 * @author Administrator
 *
 */
@Component
public class ScheduledTask{
	
	/**@Scheduled(fixedRate=1000)  上次开始执行时间点之后5秒再执行
	 * @Scheduled(fixedDelay=1000) 上次执行完毕时间点之后5秒再执行
	 
	 */
	//@Scheduled(cron="*/5 * * * *") 通过crontab 表达式定义规则
	
	
	@Scheduled(fixedRate=1000)
	public void helloSpring() {
		System.out.println("hello springBoot");
	}
	
}

要开启定时功能

package com.hangzhou.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class SpringBootStudy{
	
	public static void main(String[] args) {
		
		
		SpringApplication.run(SpringBootStudy.class, args);
		
	}
	
	
}

待续。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring Boot 中执行定时任务可以使用 Spring Framework 提供的 `@Scheduled` 注解来实现。以下是一些步骤: 1. 在你的 Spring Boot 应用程序中,创建一个带有 `@EnableScheduling` 注解的配置类。这个注解将启用 Spring 的定时任务功能。 ```java import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @EnableScheduling public class AppConfig { } ``` 2. 创建一个带有定时任务方法的类,并使用 `@Scheduled` 注解来标记该方法。你可以设置定时任务执行时间表达式,决定任务在什么时候执行。 ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyTask { @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行 public void executeTask() { // 执行定时任务的逻辑 } } ``` 在上面的例子中,定时任务 `executeTask` 方法使用了 `cron` 表达式来定义每天凌晨执行的时间表达式。 3. 确保你的定时任务类被正确扫描并注入到 Spring 容器中。你可以在应用程序的主类上添加 `@ComponentScan` 注解,以确保扫描到你的定时任务类。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages = "com.example") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在上面的例子中,`@ComponentScan` 注解将扫描 `com.example` 包及其子包中的组件。 现在,你的定时任务应该在指定的时间执行了。确保你的应用程序已经启动,并检查日志输出以确认定时任务执行情况。 这是一个简单的示例,你可以根据自己的需求调整定时任务执行时间和逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值