BAEL-5864 - Getting the Current ApplicationContext in Spring (#13433)

This commit is contained in:
apeterlic 2023-02-08 14:18:42 +01:00 committed by GitHub
parent 0adee1c8d7
commit 9b94733b9a
6 changed files with 119 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.applicationcontext;
import org.springframework.stereotype.Service;
@Service
public class ItemService {
public String getItem(){
return "New Item";
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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 {
}