Thread can not use annotations direct way to get spring beans, but often need to use bean thread to achieve business processes; There are two ways
Method 1: initialize the thread through the implementation class manner set by private property, assigned to the bean thread implementation class;
Method 2: acquired directly through applicationcontext bean thread;
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 voidRUN () { // embodiment a / * String = mockService.call Call (); System.out.println (Call + name); * / // Second way System.out.println ( "Get springbeans Start ---" ); MockService mock = (MockService) ApplicationContextHandle.getBean ( "mockSeviceImpl" ); System.out.println ( "get --- end 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必须要使用此注解,否则不生效 public class ApplicationContextHandle implements ApplicationContextAware { private static ApplicationContext applicationContext; private final static Logger logger = Logger.getLogger(ApplicationContextHandle.class); @Override public void setApplicationContext (the ApplicationContext applicationContext) throws BeansException { ApplicationContextHandle.applicationContext = applicationContext; } / ** * Get Object * This rewrite method bean, play a major role * @param name * @return Object register to a given name examples of the bean * @throws BeansException * / public static Object the getBean (String name) throws BeansException { return to applicationContext.getBean (name); } }
The spring is conventionally need to manually add <bean id = "ApplicationContextHandle" class = "com.polymer.app.utils.ApplicationContextHandle" />