BAEL-4094: Code example for Spring ApplicationContext article (#9656)

This commit is contained in:
Kamlesh Kumar
2020-07-19 20:13:14 +05:30
committed by GitHub
parent ca8eee9359
commit cbdebb76ad
11 changed files with 239 additions and 0 deletions
@@ -0,0 +1,71 @@
package com.baeldung.applicationcontext;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class ApplicationContextUnitTest {
@Test
public void givenAnnotationConfigAppContext_whenSpringConfig_thenMappingSuccess() {
ApplicationContext context = new AnnotationConfigApplicationContext(AccountConfig.class);
AccountService accountService = context.getBean(AccountService.class);
assertNotNull(accountService);
assertNotNull(accountService.getAccountRepository());
((AnnotationConfigApplicationContext) context).close();
}
@Test
public void givenClasspathXmlAppContext_whenAnnotationConfig_thenMappingSuccess() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/user-bean-config.xml");
UserService userService = context.getBean(UserService.class);
assertNotNull(userService);
((ClassPathXmlApplicationContext) context).close();
}
@Test
@Ignore
public void givenFileXmlAppContext_whenXMLConfig_thenMappingSuccess() {
String path = "D:/workspaces/Baeldung/tutorials/spring-core-4/src/test/resources/applicationcontext/account-bean-config.xml";
ApplicationContext context = new FileSystemXmlApplicationContext(path);
AccountService accountService = context.getBean("accountService", AccountService.class);
assertNotNull(accountService);
assertNotNull(accountService.getAccountRepository());
((FileSystemXmlApplicationContext) context).close();
}
@Test
public void givenClasspathXmlAppContext_whenXMLConfig_thenMappingSuccess() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/account-bean-config.xml");
AccountService accountService = context.getBean("accountService", AccountService.class);
assertNotNull(accountService);
assertNotNull(accountService.getAccountRepository());
((ClassPathXmlApplicationContext) context).close();
}
@Test
public void givenMessagesInFile_whenMessageResourceUsed_thenReadMessage() {
ApplicationContext context = new AnnotationConfigApplicationContext(AccountConfig.class);
AccountService accountService = context.getBean(AccountService.class);
assertEquals("TestAccount", accountService.getAccountName());
((AnnotationConfigApplicationContext) context).close();
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountService" class="com.baeldung.applicationcontext.AccountService">
<constructor-arg name="accountRepository" ref="accountRepository" />
</bean>
<bean id="accountRepository" class="com.baeldung.applicationcontext.AccountRepository" />
</beans>
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.baeldung.applicationcontext" />
</beans>
@@ -0,0 +1 @@
account.name=TestAccount