springboot实现bean手动注入

1.springboot启动类实现接口ApplicationListener,实现方法onApplicationEvent,初始化上下文

package test.projectTest;

import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.system.ApplicationPidFileWriter;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import test.projectTest.util.SpringContextUtil;

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
@SpringBootApplication(scanBasePackages={"test.projectTest"})
public class TestApplication implements ApplicationListener
{
    public static void main( String[] args )
    {
        SpringApplication application = new SpringApplication(TestApplication.class);
        application.addListeners(new ApplicationPidFileWriter());
        application.run(args);
        System.out.println( "启动成功" );
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        SpringContextUtil.setApplicationContext(event.getApplicationContext());
        
    }
}


2.SpringContextUtil工具类初始化ApplicationContext applicationContext

package test.projectTest.util;

import org.springframework.context.ApplicationContext;
/**
 *  获取spring容器,以访问容器中定义的其他bean
 */
public class SpringContextUtil{
    //spring上下文
    private static ApplicationContext applicationContext;
    
    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     * @param applicationContext
     */
    public static void setApplicationContext(ApplicationContext applicationContext){
        if(null==SpringContextUtil.applicationContext)
            SpringContextUtil.applicationContext=applicationContext;
    }
    
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
     /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);

    }

    /**
     * 通过name获取 Bean.
     *
     * @param clazz
     * @return
     */
    public static T getBean(Class clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @return
     */
    public static T getBean(String name, Class clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

}


3.获取bean

package test.projectTest.util;

import test.projectTest.mapper.slave.DailyDataMapper;

public class TestUtil{
    
    private static DailyDataMapper dailyDataMapper;
    
    static{//手动注入bean
        if(dailyDataMapper==null){
            dailyDataMapper = (DailyDataMapper)SpringContextUtil.getBean("dailyDataMapper");

        }
    }
    public static void test(){
        dailyDataMapper.selectByPrimaryKey(1);
    }
    
    
}

你可能感兴趣的:(springboot,java处理)