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

View File

@ -24,6 +24,16 @@
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
@ -69,6 +79,12 @@
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,28 @@
package com.baeldung.applicationcontext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
@Configuration
public class AccountConfig {
@Bean
public AccountService accountService() {
return new AccountService(accountRepository());
}
@Bean
public AccountRepository accountRepository() {
return new AccountRepository();
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("config/messages");
return messageSource;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.applicationcontext;
public class AccountRepository {
}

View File

@ -0,0 +1,32 @@
package com.baeldung.applicationcontext;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
public class AccountService {
@Autowired
private AccountRepository accountRepository;
@Autowired
private MessageSource messageSource;
public void setAccountRepository(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
public AccountRepository getAccountRepository() {
return accountRepository;
}
public AccountService(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
public String getAccountName() {
return messageSource.getMessage("account.name", null, Locale.ENGLISH);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.applicationcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MyWebApplicationInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AccountConfig.class);
context.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(context));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.applicationcontext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MyXmlWebApplicationInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocation("/WEB-INF/spring/applicationContext.xml");
context.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(context));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.applicationcontext;
import org.springframework.stereotype.Component;
@Component
public class UserService {
// user service code
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1 @@
account.name=TestAccount