Merge remote-tracking branch 'upstream/master'

This commit is contained in:
DOHA 2014-11-10 22:19:59 +02:00
commit b76aa27f3c
24 changed files with 473 additions and 114 deletions

View File

@ -19,11 +19,9 @@
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes> <attributes>
<attribute name="owner.project.facets" value="java"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>

View File

@ -31,17 +31,18 @@
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand> <buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name> <name>org.hibernate.eclipse.console.hibernateBuilder</name>
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand> <buildCommand>
<name>org.hibernate.eclipse.console.hibernateBuilder</name> <name>org.eclipse.m2e.core.maven2Builder</name>
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.springframework.ide.eclipse.core.springnature</nature> <nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature>

View File

@ -1,84 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId> <groupId>org.baeldung</groupId>
<artifactId>handling-spring-static-resources</artifactId> <artifactId>handling-spring-static-resources</artifactId>
<name>handling-spring-static-resources</name> <name>handling-spring-static-resources</name>
<version>0.1-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.1.0.RELEASE</org.springframework-version>
<org.springframework.security-version>3.2.0.RELEASE</org.springframework.security-version>
<org.aspectj-version>1.8.1</org.aspectj-version>
<org.slf4j-version>1.6.1</org.slf4j-version>
<!-- javax jsp -->
<javax.servlet.jsp-api.version>2.3.2-b01</javax.servlet.jsp-api.version>
</properties>
<dependencies> <dependencies>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${org.springframework.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${org.springframework.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${org.springframework.security.version}</version>
</dependency>
<!-- Spring --> <!-- Spring -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId> <artifactId>spring-core</artifactId>
<version>4.1.0.RELEASE</version> <version>${org.springframework.version}</version>
<exclusions> <exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion> <exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version> <version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.5.RELEASE</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId> <artifactId>spring-jdbc</artifactId>
<version>4.1.0.RELEASE</version> <version>${org.springframework.version}</version>
</dependency> </dependency>
<!-- AspectJ --> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- AOP -->
<dependency> <dependency>
<groupId>org.aspectj</groupId> <groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId> <artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version> <version>${org.aspectj-version}</version>
</dependency> </dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency> <dependency>
<groupId>javax.inject</groupId> <groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId> <artifactId>javax.inject</artifactId>
@ -101,31 +108,15 @@
<artifactId>jstl</artifactId> <artifactId>jstl</artifactId>
<version>1.2</version> <version>1.2</version>
</dependency> </dependency>
<!-- Taglibs -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<!-- Jackson JSON Processor -->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.4.1</version> <version>${jackson.version}</version>
</dependency>
<!-- Rome Atom+RSS -->
<!-- JSR 303 with Hibernate Validator -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId> <artifactId>hibernate-validator</artifactId>
<version>4.1.0.Final</version> <version>${hibernate-validator.version}</version>
</dependency> </dependency>
<!-- Joda Time Library --> <!-- Joda Time Library -->
@ -141,13 +132,31 @@
<artifactId>handlebars</artifactId> <artifactId>handlebars</artifactId>
<version>1.3.2</version> <version>1.3.2</version>
</dependency> </dependency>
<!-- Security (used for CSRF protection only) -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${org.springframework.security-version}</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
</dependency>
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
</dependencies> </dependencies>
@ -160,4 +169,54 @@
</resource> </resource>
</resources> </resources>
</build> </build>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.1.0.RELEASE</org.springframework-version>
<org.springframework.security-version>3.2.0.RELEASE</org.springframework.security-version>
<org.aspectj-version>1.8.1</org.aspectj-version>
<org.slf4j-version>1.6.1</org.slf4j-version>
<javax.servlet.jsp-api.version>2.3.2-b01</javax.servlet.jsp-api.version>
<!-- Spring -->
<org.springframework.version>4.1.1.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.7.Final</hibernate.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<spring-data-jpa.version>1.6.2.RELEASE</spring-data-jpa.version>
<!-- marshalling -->
<jackson.version>2.4.2</jackson.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
<logback.version>1.1.2</logback.version>
<!-- various -->
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
<!-- util -->
<guava.version>18.0</guava.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<mockito.version>1.10.8</mockito.version>
<httpcore.version>4.3.3</httpcore.version>
<httpclient.version>4.3.5</httpclient.version>
<rest-assured.version>2.3.4</rest-assured.version>
<!-- Maven plugins -->
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
<maven-war-plugin.version>2.5</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.10</cargo-maven2-plugin.version>
</properties>
</project> </project>

View File

@ -17,8 +17,7 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
handle(request, response, authentication); handle(request, response, authentication);
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
if (session != null) { if (session != null) {
@ -39,9 +38,9 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
} }
protected String determineTargetUrl(Authentication authentication) { protected String determineTargetUrl(Authentication authentication) {
return "/home.html"; return "/home.html";
} }
protected void clearAuthenticationAttributes(HttpServletRequest request) { protected void clearAuthenticationAttributes(HttpServletRequest request) {

View File

@ -1,9 +1,7 @@
package org.baeldung.spring; package org.baeldung.spring;
import java.util.Locale; import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -42,9 +40,9 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
@Override @Override
public void addViewControllers(final ViewControllerRegistry registry) { public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry); super.addViewControllers(registry);
registry.addViewController("/login.html"); registry.addViewController("/login.html");
registry.addViewController("/home.html"); registry.addViewController("/home.html");
} }
@Bean @Bean
@ -55,23 +53,21 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
bean.setSuffix(".jsp"); bean.setSuffix(".jsp");
return bean; return bean;
} }
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
//For examples using Spring 4.1.0 // For examples using Spring 4.1.0
if((env.getProperty("resource.handler.conf")).equals("4.1.0")){ if ((env.getProperty("resource.handler.conf")).equals("4.1.0")) {
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver()).addResolver(new PathResourceResolver()); registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver()).addResolver(new PathResourceResolver());
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/", "classpath:/other-resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver()); registry.addResourceHandler("/resources/**").addResourceLocations("/resources/", "classpath:/other-resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
registry.addResourceHandler("/files/**").addResourceLocations("file:/Users/Elena/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver()); registry.addResourceHandler("/files/**").addResourceLocations("file:/Users/Elena/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver());
registry.addResourceHandler("/other-files/**").addResourceLocations("file:/Users/Elena/"). registry.addResourceHandler("/other-files/**").addResourceLocations("file:/Users/Elena/").setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver());
setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver()); }
} // For examples using Spring 4.0.7
//For examples using Spring 4.0.7 else if ((env.getProperty("resource.handler.conf")).equals("4.0.7")) {
else if ((env.getProperty("resource.handler.conf")).equals("4.0.7")){
registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/", "classpath:/other-resources/"); registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/", "classpath:/other-resources/");
registry.addResourceHandler("/files/**") registry.addResourceHandler("/files/**").addResourceLocations("file:/Users/Elena/");
.addResourceLocations("file:/Users/Elena/");
} }
} }

View File

@ -7,7 +7,7 @@ import org.springframework.context.annotation.ImportResource;
@ImportResource({ "classpath:webSecurityConfig.xml" }) @ImportResource({ "classpath:webSecurityConfig.xml" })
public class SecSecurityConfig { public class SecSecurityConfig {
public SecSecurityConfig() { public SecSecurityConfig() {
super(); super();
} }

View File

@ -14,22 +14,19 @@ import org.springframework.web.context.request.WebRequest;
@Controller @Controller
public class HomeController { public class HomeController {
@Autowired @Autowired
Environment env; Environment env;
@RequestMapping(value = "/home", method = RequestMethod.GET) @RequestMapping(value = "/home", method = RequestMethod.GET)
public String showHome(WebRequest request, Model model, Locale locale) throws IOException { public String showHome(WebRequest request, Model model, Locale locale) throws IOException {
Date date = new Date(); Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date); String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate ); model.addAttribute("serverTime", formattedDate);
return "home"; return "home";
} }
} }

View File

@ -0,0 +1,5 @@
package org.baeldung.profiles;
public interface DatasourceConfig {
public void setup();
}

View File

@ -0,0 +1,15 @@
package org.baeldung.profiles;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("dev")
public class DevDatasourceConfig implements DatasourceConfig {
@Override
public void setup() {
System.out.println("Setting up datasource for DEV environment. ");
}
}

View File

@ -0,0 +1,15 @@
package org.baeldung.profiles;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("production")
public class ProductionDatasourceConfig implements DatasourceConfig {
@Override
public void setup() {
System.out.println("Setting up datasource for PRODUCTION environment. ");
}
}

View File

@ -0,0 +1,10 @@
package org.baeldung.profiles;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.profiles")
public class SpringProfilesConfig {
}

View File

@ -0,0 +1,21 @@
package org.baeldung.springevents.asynchronous;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
@Configuration
@ComponentScan("org.baeldung.springevents.synchronous")
public class AsynchronousSpringEventsConfig {
@Bean(name = "applicationEventMulticaster")
public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
final SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
simpleApplicationEventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
return simpleApplicationEventMulticaster;
}
}

View File

@ -0,0 +1,15 @@
package org.baeldung.springevents.synchronous;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(final ContextRefreshedEvent cse) {
System.out.println("Handling context re-freshed event. ");
}
}

View File

@ -0,0 +1,19 @@
package org.baeldung.springevents.synchronous;
import org.springframework.context.ApplicationEvent;
public class CustomSpringEvent extends ApplicationEvent {
private static final long serialVersionUID = -8053143381029977953L;
private String message;
public CustomSpringEvent(final Object source, final String message) {
super(source);
this.message = message;
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1,14 @@
package org.baeldung.springevents.synchronous;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class CustomSpringEventListener implements ApplicationListener<CustomSpringEvent> {
@Override
public void onApplicationEvent(final CustomSpringEvent event) {
System.out.println("Received spring custom event - " + event.getMessage());
}
}

View File

@ -0,0 +1,19 @@
package org.baeldung.springevents.synchronous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
@Component
public class CustomSpringEventPublisher {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void publishEvent(final String message) {
System.out.println("Publishing custom event. ");
final CustomSpringEvent customSpringEvent = new CustomSpringEvent(this, message);
applicationEventPublisher.publishEvent(customSpringEvent);
}
}

View File

@ -0,0 +1,10 @@
package org.baeldung.springevents.synchronous;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.springevents.synchronous")
public class SynchronousSpringEventsConfig {
}

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-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<beans profile="dev">
<bean id="devDatasourceConfig" class="org.baeldung.profiles.DevDatasourceConfig" />
</beans>
<beans profile="production">
<bean id="productionDatasourceConfig" class="org.baeldung.profiles.ProductionDatasourceConfig" />
</beans>
</beans>

View File

@ -0,0 +1,23 @@
package org.baeldung.profiles;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("dev")
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
public class DevProfileWithAnnotationTest {
@Autowired
DatasourceConfig datasourceConfig;
@Test
public void testSpringProfiles() {
Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig);
}
}

View File

@ -0,0 +1,32 @@
package org.baeldung.profiles;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("production")
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ProductionProfileWithAnnotationTest {
@Autowired
DatasourceConfig datasourceConfig;
@Autowired
Environment environment;
@Test
public void testSpringProfiles() {
for (final String profileName : environment.getActiveProfiles()) {
System.out.println("Currently active profile - " + profileName);
}
Assert.assertEquals("production", environment.getActiveProfiles()[0]);
Assert.assertTrue(datasourceConfig instanceof ProductionDatasourceConfig);
}
}

View File

@ -0,0 +1,33 @@
package org.baeldung.profiles;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
public class SpringProfilesWithXMLTest {
private ClassPathXmlApplicationContext classPathXmlApplicationContext;
@Test
public void testSpringProfilesForDevEnvironment() {
classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml");
final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment();
configurableEnvironment.setActiveProfiles("dev");
classPathXmlApplicationContext.refresh();
final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("devDatasourceConfig", DatasourceConfig.class);
Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig);
}
@Test
public void testSpringProfilesForProdEnvironment() {
classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml");
final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment();
configurableEnvironment.setActiveProfiles("production");
classPathXmlApplicationContext.refresh();
final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("productionDatasourceConfig", DatasourceConfig.class);
Assert.assertTrue(datasourceConfig instanceof ProductionDatasourceConfig);
}
}

View File

@ -0,0 +1,23 @@
package org.baeldung.springevents.asynchronous;
import org.baeldung.springevents.synchronous.CustomSpringEventPublisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AsynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
public class AsynchronousCustomSpringEventsTest {
@Autowired
private CustomSpringEventPublisher publisher;
@Test
public void testCustomSpringEvents() throws InterruptedException {
publisher.publishEvent("Hello world!!");
System.out.println("Done publishing asynchronous custom event. ");
}
}

View File

@ -0,0 +1,18 @@
package org.baeldung.springevents.synchronous;
import org.baeldung.springevents.synchronous.SynchronousSpringEventsConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ContextRefreshedListenerTest {
@Test
public void testContextRefreshedListener() throws InterruptedException {
System.out.println("Test context re-freshed listener.");
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.springevents.synchronous;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
public class SynchronousCustomSpringEventsTest {
@Autowired
private CustomSpringEventPublisher publisher;
@Test
public void testCustomSpringEvents() throws InterruptedException {
publisher.publishEvent("Hello world!!");
System.out.println("Done publishing synchronous custom event. ");
}
}