springboot線程中獲取spring beans

線程中沒法直接使用註解的方式獲取spring beans,可是線程常常須要用到bean來實現業務流程;這裏有兩種方式
方法1:是經過初始化線程實現類的方式經過set私有屬性,把bean賦值到線程實現類中;java

方法2:經過applicationcontext線程中直接獲取bean;

spring

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.polymer.app.StartApplication;
import com.polymer.app.service.MockService;
import com.polymer.app.utils.ApplicationContextHandle;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartApplication.class)
public class TestThreadAOP {

    private static ExecutorService excutor = Executors.newFixedThreadPool(1);

    @Autowired
    private MockService mockService;

    @Test
    public void testTAOP() throws InterruptedException, ExecutionException {
        Task t = new Task();
        t.setName("zhangyf");
        t.setMockService(mockService);
        excutor.execute(t);
    }

    class Task implements Runnable {
        private String name;

        private MockService mockService;

        public MockService getMockService() {
            return mockService;
        }

        public void setMockService(MockService mockService) {
            this.mockService = mockService;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            //方式一
            /*String call = mockService.call();
            System.out.println(call + name);*/
            //方式二
            System.out.println("獲取springbeans開始---");
            MockService mock = (MockService) ApplicationContextHandle.getBean("mockSeviceImpl");
            System.out.println("獲取springbeans結束---");
            String call = mock.call();
            System.out.println(call + name);
        }

    }

}

 

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Configuration//springboot的使用applicationcontext須要使用此註解或者使用@Component初始化加載bean,不然不生效
public class ApplicationContextHandle implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    private final static Logger logger = Logger.getLogger(ApplicationContextHandle.class);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
               ApplicationContextHandle.applicationContext = applicationContext;
    }

    /** 
     * 獲取對象 
     * 這裏重寫了bean方法,起主要做用 
     * @param name 
     * @return Object 一個以所給名字註冊的bean的實例 
     * @throws BeansException 
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
}

 

以往的spring則是須要手動添加<bean id="ApplicationContextHandle" class="com.polymer.app.utils.ApplicationContextHandle"/>apache

相關文章
相關標籤/搜索