spring中进阶编程常用的几个类

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);
}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值