package com.init;
import com.zhiwee.gree.model.goods.GoodsInfo;
import com.zhiwee.gree.service.goods.GoodsInfoService;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
/**
-
@author sun on 2018/12/21
*/
public class SpringInit implements InitializingBean,BeanNameAware,ApplicationContextAware,BeanFactoryAware,BeanFactoryPostProcessor {
@Autowired
private GoodsInfoService goodsInfoService;
@Autowired
private RedisTemplate redisTemplate;private BeanFactory beanFactory;
@Override
// InitializingBean这个接口要实现方法,在初始化bean时候会调用这个方法
public void afterPropertiesSet() throws Exception {
// redisTemplate.opsForValue().set(“onlineConsumerAmount”,0);
// List list= goodsInfoService.list();
// for(GoodsInfo goodsInfo:list){
// if(goodsInfo.getStockNum()!=null){
// redisTemplate.opsForValue().set(goodsInfo.getId(),goodsInfo.getStockNum());
// }
// }
}
//ApplicationContextAware接口实现额方法,可以将上下文注入在程序中
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
String name = applicationContext.getBean(“dealerGoodsInfoController”).getClass().getName();
System.out.print(name);
}
//BeanFactoryAware接口实现的方法
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
//BeanFactoryPostProcessor实现的方法,可以改变bean的一些属性,例如单例多例
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(“dealerGoodsInfoController”);
MutablePropertyValues beanProperty = beanDefinition.getPropertyValues();
System.out.println(beanDefinition.getScope());
//beanDefinition.setScope(“singleton”);
System.out.println(beanDefinition.getScope());
System.out.println(beanProperty);
}
//BeanNameAware实现的方法,可以获取到初始化bean是的名字
@Override
public void setBeanName(String name) {
System.out.println(name);
}
}