springboot-中工具类,普通方法,和quartz的Job中获取bean的方法

springboot-中工具类,普通方法,和quartz的Job中获取bean的方法

ApplicationContextAware



    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>


/**
 * 主要讲applicationcontext对象注入进该类,从而我们可以在普通项目中获取bean
 */
public final class BeanDefinedLocator implements ApplicationContextAware {

    private static final BeanDefinedLocator beanLocator = new BeanDefinedLocator();

    private ApplicationContext context = null;

    private BeanDefinedLocator() {}

    public static BeanDefinedLocator getInstance() {
    	return beanLocator;
    }

    public ApplicationContext getApplicationContext(){
    	return context;
	}

    
    public void setApplicationContext(ApplicationContext applicationcontext) throws BeansException {
    	beanLocator.context = applicationcontext;
	}

    public Object getBean(String beanName) {
    	Assert.hasText( beanName );
    	return this.context.getBean(beanName);
    }
    
    /**
     * 根据指定的bean名及类型来获取bean
     * @param <T>
     * @param beanName
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
	public <T> T getBean(String beanName, Class<T> clazz) {
		Assert.hasText( beanName );
		contextNotNull();
		return (T) this.context.getBean(beanName, clazz);
	}
    
    /**
     * 根据指定的类型来获取bean
     * @param <T>
     * @param clazz
     * @return
     */
	@SuppressWarnings("unchecked")
	public <T> T getBean(Class<T> clazz) {
		Assert.notNull( clazz );
		contextNotNull();
		
		return (T) BeanFactoryUtils.beansOfTypeIncludingAncestors(this.context, clazz, true, true).values().iterator().next();
	}
	
	 /**
     * 根据指定的类型来获取bean集合
     * @param <T>
     * @param clazz
     * @return 
     */
	@SuppressWarnings("unchecked")
	public <T> List<T> getBeans(Class<T> clazz) {
		Assert.notNull( clazz );
		contextNotNull();
		
		return new ArrayList<T>(BeanFactoryUtils.beansOfTypeIncludingAncestors(this.context, clazz, true, true).values());
	}
    
	private void contextNotNull() {
		if (this.context == null)
		    throw new ApplicationContextException("application context is null");
	}

SpringBeanAutowiringSupport(项目必须是web项目)

public class InitServlet extends HttpServlet {  
         
    @Autowired  
    private ServiceA serviceA;  
      
    public void init(ServletConfig config) throws ServletException {  
        super.init(config);  
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);  
        assertNotNull("Service should be injected.", serviceA);  
    }  
      
        // Omitted doGet(req, res), doPost(req, res);  
}  

public class DumpJob implements Job {  
      
    @Autowired  
    private ServiceA serviceA;  
  
    public void execute(JobExecutionContext context) throws JobExecutionException {  
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);  
        assertNotNull("Service should be injected.", serviceA);  
    }  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值