Created
June 5, 2013 10:08
-
-
Save jprudent/5712884 to your computer and use it in GitHub Desktop.
self injecting spring test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fr.pmu.commons.spring.SpringBuilder; | |
import org.junit.*; | |
import org.junit.Test; | |
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.scheduling.annotation.Async; | |
import org.springframework.scheduling.annotation.EnableAsync; | |
import org.springframework.scheduling.annotation.EnableScheduling; | |
import org.springframework.stereotype.Service; | |
import javax.inject.Inject; | |
public class LearAsyncTest { | |
@Service | |
public static class AsyncService { | |
@Async | |
public void foo(int i) { | |
System.out.println(i + ". Zombie"); | |
} | |
} | |
@org.springframework.context.annotation.Configuration | |
@EnableAsync | |
@EnableScheduling | |
public static class Configuration { | |
} | |
@Inject | |
private AsyncService asyncService; | |
@Before | |
public void setUp() throws Exception { | |
AnnotationConfigApplicationContext applicationContext = new SpringBuilder()// | |
.scanningClasses(Configuration.class, AsyncService.class) // | |
.build(); | |
AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory(); | |
autowireCapableBeanFactory.autowireBean(this); | |
} | |
@Test | |
public void testAsync() throws Exception { | |
for (int i = 0; i < 100_009; i++) { | |
asyncService.foo(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment