admin
2024-10-17 b30fb8afd3cd6228bda9b182dc412bb3c8daf69c
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
package com.yeshi.buwan.util;
 
import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
 
/**
 * 系统初始化
 *
 * @author Administrator
 */
@Component
public class SpringContext implements ApplicationListener<ContextRefreshedEvent> {
 
 
    private static boolean isInited = false;
 
    private final static Logger logger = LoggerFactory.getLogger(SpringContext.class);
 
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        if (arg0.getApplicationContext().getParent() != null) {
            System.out.println(System.currentTimeMillis());
            onApplication(arg0);
        }
    }
 
    @Bean
    public DataSourceProxy dataSourceProxy(DruidDataSource druidDataSource) {
        return new DataSourceProxy(druidDataSource);
    }
 
 
    private synchronized void onApplication(ContextRefreshedEvent context) {
        if (!isInited) {
            isInited = true;
            System.out.println("系统初始化成功");
            init();
        }
    }
 
    private void init() {
        logger.error("初始化");
    }
 
 
}