BAEL-5864 - Getting the Current ApplicationContext in Spring (#13433)
This commit is contained in:
parent
0adee1c8d7
commit
9b94733b9a
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.applicationcontext;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class ApplicationContextProvider implements ApplicationContextAware {
|
||||||
|
private static ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
ApplicationContextProvider.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApplicationContext getApplicationContext() {
|
||||||
|
return applicationContext;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.applicationcontext;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ItemService {
|
||||||
|
|
||||||
|
public String getItem(){
|
||||||
|
return "New Item";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.applicationcontext;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class MyBean {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
public ApplicationContext getApplicationContext() {
|
||||||
|
return applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.applicationcontext;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
@ContextConfiguration(classes = TestContextConfig.class)
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
class ApplicationContextProviderUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGetApplicationContext_thenReturnApplicationContext() {
|
||||||
|
ApplicationContext context = ApplicationContextProvider.getApplicationContext();
|
||||||
|
assertNotNull(context);
|
||||||
|
System.out.printf("ApplicationContext has %d beans %n", context.getBeanDefinitionCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGetBean_thenReturnItemServiceReference() {
|
||||||
|
ApplicationContext context = ApplicationContextProvider.getApplicationContext();
|
||||||
|
assertNotNull(context);
|
||||||
|
|
||||||
|
ItemService itemService = context.getBean(ItemService.class);
|
||||||
|
assertNotNull(context);
|
||||||
|
|
||||||
|
System.out.println(itemService.getItem());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.baeldung.applicationcontext;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
@ContextConfiguration(classes = TestContextConfig.class)
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
class MyBeanUnitTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
MyBean myBean;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenGetApplicationContext_thenReturnApplicationContext() {
|
||||||
|
assertNotNull(myBean);
|
||||||
|
ApplicationContext context = myBean.getApplicationContext();
|
||||||
|
assertNotNull(context);
|
||||||
|
|
||||||
|
System.out.printf("ApplicationContext has %d beans %n", context.getBeanDefinitionCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.applicationcontext;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan("com.baeldung.applicationcontext")
|
||||||
|
public class TestContextConfig {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue