有时我们需要在项目启动时就要加载一些数据到缓存中(spring缓存或第三方缓存),spring在启动时加载数据有两种方式*(我所知道的,如果还有别的方式欢迎告知,嘿嘿):实现InitializingBean和实现BeanPostProcessor
第一种:实现InitializingBean
这种方式比较简单,写一个service实现InitializingBean并在spring配置文件中配置一下,启动时就会加载数据
public class InternationalServiceImpl implements InitializingBean{ @Override public void afterPropertiesSet() throws Exception { System.err.println("重启开始加载数据。。。"); } }
配置文件:
<bean id = "internationalService" class="com.taotao.manage.service.InternationalServiceImpl"></bean>
测试例子:
public class LoadTest { private PBSTrackManagerPostProcessor pbsTrackManagerPostProcessor; @Before public void setUp() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/applicationContext.xml"}); this.pbsTrackManagerPostProcessor = context.getBean(PBSTrackManagerPostProcessor.class); } @Test public void testQueryOrderByOrderNumber() { System.out.println("开始加载"); } }
结果:
重启开始加载数据。。。
第二种:实现BeanPostProcessor
数据层(这里写的比较简单):
public class Country implements Serializable{ private static final long serialVersionUID = 4014709713611499131L; private String Province; private String city; public String getProvince() { return Province; } public void setProvince(String province) { Province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public String toString() { return "Country [Province=" + Province + ", city=" + city + "]"; } }
service层:
@Service public class CountryServiceImpl implements CountryService{ @Override public List<Country> getAllCountry() { List<Country> countries = new ArrayList<Country>(); for(int i = 0;i <10;i++){ Country country = new Country(); country.setCity("北京"+i); country.setProvince("上海"+i); countries.add(country); } return countries; } }
再写一个service来调用这个service
public class StartOnLoadService { public static Map<String, List<Country>> map = new HashMap<String, List<Country>>(); @Autowired private CountryService countryService; public void loadAllCountries(){ List<Country> list = countryService.getAllCountry(); map.put("countryMap", list); System.out.println(map); } }
然后写一个processor来加载这个service
public class PBSTrackManagerPostProcessor implements BeanPostProcessor{ @Override public Object postProcessAfterInitialization(Object obj, String arg1) throws BeansException { if(obj instanceof StartOnLoadService){ ((StartOnLoadService)obj).loadAllCountries(); } return obj; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub return arg0; } }
最后写配置文件:
<bean id="startOnLoadService" class="com.taotao.manage.controller.StartOnLoadService"></bean> <bean id="pbsTrackManagerPostProcessor" class="com.taotao.manage.controller.PBSTrackManagerPostProcessor"> </bean>
测试例子:
public class LoadTest { private PBSTrackManagerPostProcessor pbsTrackManagerPostProcessor; @Before public void setUp() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/applicationContext.xml"}); this.pbsTrackManagerPostProcessor = context.getBean(PBSTrackManagerPostProcessor.class); } @Test public void testQueryOrderByOrderNumber() { System.out.println("开始加载"); } }
结果:
{countryMap=[Country [Province=上海0, city=北京0], Country [Province=上海1, city=北京1], Country [Province=上海2, city=北京2],。。。}
我觉得那个countryService可以不用写,当时就那么写了。大家可以尝试下
新手,不足指出请指教!