项目启动初始化数据的方法

在开发过程中,可能有一些固定的数据比如字典表数据,可以在项目初始化时候获取并保存到常量类中,可以避免多次调用数据库。在项目启动时初始化数据有以下几种方法:

1、@PostConstruct注解:通过给方法名加上@PostConstruct注解,会在项目初始化时执行:

@Compant
public class Demo{
 
    @PostConstruct
    public void init(){
        //项目启动执行方法
        
    }
}

2、在配置文件注入bean的地方添加init-method方法:这种方法会在注入bean到容器时,主动去调用init-method指定的方法,一般用在spring项目中:


3、实现InitializingBean接口,复写afterPropertiesSet方法,如:

package com.mydemo.config;

import com.mydemo.service.AreaService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class InitDemo implements InitializingBean {
    @Autowired
    private AreaService areaService;

    @Override
    public void afterPropertiesSet() throws Exception {
        List list = areaService.getProvinceList();
        System.out.println(list.size());
    }
}

4、在ApplicationContextAware实现类中初始化,如:

package com.mydemo.config;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DefaultBeanFactory {
	private static ApplicationContext context = null;
	private static DefaultBeanFactory instance = null;
	private static Object lock = new Object();

	private DefaultBeanFactory(String filepath){
		try {
			context = new ClassPathXmlApplicationContext(filepath);
		} catch (Exception e) {
		}
	}
	@SuppressWarnings("static-access")
	private DefaultBeanFactory(ApplicationContext context){
		try {
			this.context = context;
		} catch (Exception e) {
		}
	}

	public static void setSpringApplicationContext(ApplicationContext context){
		synchronized (lock) {
			instance = new DefaultBeanFactory(context);
		}
	}
	public static DefaultBeanFactory getInstance() {
		if(instance == null || context == null){
			throw new RuntimeException("Spring context is null!");
		}
		return instance;
	}

	public static DefaultBeanFactory getInstance(String filepath) {
		synchronized (lock) {
			instance = new DefaultBeanFactory(filepath);
		}
		return instance;
	}
	public Object getBean(String name) {
		return context.getBean(name);
	}
}
package com.mydemo.config;


import com.mydemo.service.AreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class ApplicationContextRegister implements ApplicationContextAware {
    private static Logger logger = LoggerFactory.getLogger(ApplicationContextRegister.class);
    private static ApplicationContext APPLICATION_CONTEXT;

    @Autowired
    private AreaService areaService;
    /**
     * 设置spring上下文
     * @param applicationContext spring上下文
     * @throws BeansException
     * */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.debug("ApplicationContext registed-->{}", applicationContext);
        DefaultBeanFactory.setSpringApplicationContext(applicationContext);

        List list = areaService.getProvinceList();
        System.out.println("长度"+list.size());

        APPLICATION_CONTEXT = applicationContext;
    }

    /**
     * 获取容器
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return APPLICATION_CONTEXT;
    }

    /**
     * 获取容器对象
     * @param type
     * @param 
     * @return
     */
    public static  T getBean(Class type) {
        return APPLICATION_CONTEXT.getBean(type);
    }
}

项目启动初始化数据的方法_第1张图片

你可能感兴趣的:(SpringMVC,SpringBoot+线程池)