mitao
2024-07-09 07c83c163675e24252de05d029cef2eab046e583
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.finance;
 
import com.finance.system.service.AsyncService;
import java.net.InetAddress;
import java.net.UnknownHostException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
 
/**
 * 启动程序
 *
 * @author ruoyi
 */
@Slf4j
@EnableScheduling// 开启定时任务
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class FinanceAdminDeptApplication {
 
    @Autowired
    private AsyncService asyncService;
 
    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(
                FinanceAdminDeptApplication.class, args);
        Environment env = application.getEnvironment();
        log.info("\n----------------------------------------------------------\n\t" +
                        "应用 '{}' 运行成功! 访问连接:\n\t" +
                        "Swagger文档: \t\thttp://{}:{}/doc.html\n" +
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name", "后台"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port", "8081"));
    }
 
    /**
     * 当不存在此 wxRestTemplate 使用此方法的bean注入
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "restTemplate")
    public RestTemplate wxRestTemplate() {
        // 复杂构造函数的使用
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        // 设置超时
        requestFactory.setConnectTimeout(6000);
        requestFactory.setReadTimeout(6000);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(requestFactory);
        return restTemplate;
    }
 
    @Bean
    public ApplicationRunner applicationRunner() {
        return applicationArguments -> {
            long startTime = System.currentTimeMillis();
            System.out.println(Thread.currentThread().getName() + ":开始调用异步业务");
            asyncService.asyncTask();
            long endTime = System.currentTimeMillis();
            System.out.println(
                    Thread.currentThread().getName() + ":调用异步业务结束,耗时:" + (endTime
                            - startTime));
        };
    }
 
}