From b5cc62b50e5bdf720dffdb6ee01501582ca264f2 Mon Sep 17 00:00:00 2001 From: mgooty Date: Wed, 1 Oct 2014 14:30:11 +0530 Subject: [PATCH 1/4] Added sample code for Spring events and Spring profiles. --- spring-all/pom.xml | 3 +- .../baeldung/profiles/DatasourceConfig.java | 5 ++ .../profiles/DevDatasourceConfig.java | 15 ++++++ .../profiles/ProductionDatasourceConfig.java | 15 ++++++ .../profiles/SpringProfilesConfig.java | 10 ++++ .../ScheduledAnnotationExample.java | 52 +++++++++++++++++++ .../scheduling/SchedulingWithXmlConfig.java | 16 ++++++ .../scheduling/SpringSchedulingConfig.java | 20 +++++++ .../AsynchronousSpringEventsConfig.java | 20 +++++++ .../synchronous/ContextRefreshedListener.java | 15 ++++++ .../synchronous/CustomSpringEvent.java | 19 +++++++ .../CustomSpringEventListener.java | 14 +++++ .../CustomSpringEventPublisher.java | 18 +++++++ .../SynchronousSpringEventsConfig.java | 10 ++++ .../main/resources/springProfiles-config.xml | 15 ++++++ .../main/resources/springScheduled-config.xml | 26 ++++++++++ .../main/resources/springScheduled.properties | 3 ++ .../DevProfileWithAnnotationTest.java | 23 ++++++++ .../ProductionProfileWithAnnotationTest.java | 32 ++++++++++++ .../profiles/SpringProfilesWithXMLTest.java | 33 ++++++++++++ .../ScheduledAnnotationExampleTest.java | 17 ++++++ .../SchedulingWithXmlConfigTest.java | 16 ++++++ .../AsynchronousCustomSpringEventsTest.java | 23 ++++++++ .../ContextRefreshedListenerTest.java | 18 +++++++ .../SynchronousCustomSpringEventsTest.java | 22 ++++++++ 25 files changed, 458 insertions(+), 2 deletions(-) create mode 100644 spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java create mode 100644 spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java create mode 100644 spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java create mode 100644 spring-all/src/main/resources/springProfiles-config.xml create mode 100644 spring-all/src/main/resources/springScheduled-config.xml create mode 100644 spring-all/src/main/resources/springScheduled.properties create mode 100644 spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java create mode 100644 spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java create mode 100644 spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java create mode 100644 spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java create mode 100644 spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java create mode 100644 spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java create mode 100644 spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java create mode 100644 spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java diff --git a/spring-all/pom.xml b/spring-all/pom.xml index e7350e4612..feb25dc987 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -33,7 +33,7 @@ - + org.hibernate hibernate-core @@ -118,7 +118,6 @@ ${mockito.version} test - diff --git a/spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java b/spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java new file mode 100644 index 0000000000..80cb060c7e --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/DatasourceConfig.java @@ -0,0 +1,5 @@ +package org.baeldung.profiles; + +public interface DatasourceConfig { + public void setup(); +} diff --git a/spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java b/spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java new file mode 100644 index 0000000000..e357280c43 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/DevDatasourceConfig.java @@ -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. "); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java b/spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java new file mode 100644 index 0000000000..f1adff9c96 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/ProductionDatasourceConfig.java @@ -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. "); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java b/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java new file mode 100644 index 0000000000..eb5543e3db --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/profiles/SpringProfilesConfig.java @@ -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 { + +} diff --git a/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java b/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java new file mode 100644 index 0000000000..284bcf5e6a --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java @@ -0,0 +1,52 @@ +package org.baeldung.scheduling; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component("scheduledAnnotationExample") +public class ScheduledAnnotationExample { + + @Scheduled(fixedDelay = 1000) + public void scheduleFixedDelayTask() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}") + public void scheduleFixedDelayTaskUsingExpression() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelay = 1000, initialDelay = 2000) + public void scheduleFixedDelayWithInitialDelayTask() { + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedRate = 1000) + public void scheduleFixedRateTask() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedRateString = "${fixedRate.in.milliseconds}") + public void scheduleFixedRateTaskUsingExpression() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelay = 1000, initialDelay = 100) + public void scheduleFixedRateWithInitialDelayTask() { + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); + } + + /** + * Scheduled task is executed at 10:15 AM on the 15th day of every month + */ + @Scheduled(cron = "0 15 10 15 * ?") + public void scheduleTaskUsingCronExpression() { + System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(cron = "${cron.expression}") + public void scheduleTaskUsingExternalizedCronExpression() { + System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java b/spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java new file mode 100644 index 0000000000..d7da9ac19c --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/SchedulingWithXmlConfig.java @@ -0,0 +1,16 @@ +package org.baeldung.scheduling; + +public class SchedulingWithXmlConfig { + + public void scheduleFixedDelayTask() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + public void scheduleFixedRateTask() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + public void scheduleTaskUsingCronExpression() { + System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000); + } +} diff --git a/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java b/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java new file mode 100644 index 0000000000..1d81fa0aa8 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java @@ -0,0 +1,20 @@ +package org.baeldung.scheduling; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +@ComponentScan("org.baeldung.scheduling") +@PropertySource("classpath:springScheduled.properties") +public class SpringSchedulingConfig { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java b/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java new file mode 100644 index 0000000000..93f61bf517 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java @@ -0,0 +1,20 @@ +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 static ApplicationEventMulticaster simpleApplicationEventMulticaster() { + final SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster(); + simpleApplicationEventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor()); + return simpleApplicationEventMulticaster; + } +} diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java new file mode 100644 index 0000000000..052437e555 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/ContextRefreshedListener.java @@ -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 { + + @Override + public void onApplicationEvent(final ContextRefreshedEvent cse) { + System.out.println("Handling context re-freshed event. "); + } + +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java new file mode 100644 index 0000000000..1631801ffe --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEvent.java @@ -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; + } + +} diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java new file mode 100644 index 0000000000..69f96966ec --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventListener.java @@ -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 { + + @Override + public void onApplicationEvent(final CustomSpringEvent event) { + System.out.println("Received spring custom event - " + event.getMessage()); + } + +} \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java new file mode 100644 index 0000000000..decbcf446a --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java @@ -0,0 +1,18 @@ +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); + } +} diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java new file mode 100644 index 0000000000..fff8a7e4b5 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/SynchronousSpringEventsConfig.java @@ -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 { + +} diff --git a/spring-all/src/main/resources/springProfiles-config.xml b/spring-all/src/main/resources/springProfiles-config.xml new file mode 100644 index 0000000000..a290f83b26 --- /dev/null +++ b/spring-all/src/main/resources/springProfiles-config.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/springScheduled-config.xml b/spring-all/src/main/resources/springScheduled-config.xml new file mode 100644 index 0000000000..68d42fdd57 --- /dev/null +++ b/spring-all/src/main/resources/springScheduled-config.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/springScheduled.properties b/spring-all/src/main/resources/springScheduled.properties new file mode 100644 index 0000000000..336fc83937 --- /dev/null +++ b/spring-all/src/main/resources/springScheduled.properties @@ -0,0 +1,3 @@ +cron.expression=0 15 10 15 * ? +fixedRate.in.milliseconds=1000 +fixedDelay.in.milliseconds=1000 \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java b/spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java new file mode 100644 index 0000000000..2b65928da8 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/profiles/DevProfileWithAnnotationTest.java @@ -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); + } +} \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java b/spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java new file mode 100644 index 0000000000..551636bd31 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/profiles/ProductionProfileWithAnnotationTest.java @@ -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); + } +} \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java b/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java new file mode 100644 index 0000000000..15c9265a13 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/profiles/SpringProfilesWithXMLTest.java @@ -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); + } +} diff --git a/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java b/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java new file mode 100644 index 0000000000..9317c7bb7f --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java @@ -0,0 +1,17 @@ +package org.baeldung.scheduling; + +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 = { SpringSchedulingConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ScheduledAnnotationExampleTest { + + @Test + public void testScheduledAnnotation() throws InterruptedException { + Thread.sleep(5000); + } +} diff --git a/spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java b/spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java new file mode 100644 index 0000000000..0fca4d21c8 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/scheduling/SchedulingWithXmlConfigTest.java @@ -0,0 +1,16 @@ +package org.baeldung.scheduling; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:springScheduled-config.xml") +public class SchedulingWithXmlConfigTest { + + @Test + public void testXmlBasedScheduling() throws InterruptedException { + Thread.sleep(5000); + } +} diff --git a/spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java b/spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java new file mode 100644 index 0000000000..2b45ae4e68 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/springevents/asynchronous/AsynchronousCustomSpringEventsTest.java @@ -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. "); + } +} diff --git a/spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java b/spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java new file mode 100644 index 0000000000..d971698e3f --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/springevents/synchronous/ContextRefreshedListenerTest.java @@ -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."); + } +} \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java b/spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java new file mode 100644 index 0000000000..b559ca9fc9 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/springevents/synchronous/SynchronousCustomSpringEventsTest.java @@ -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. "); + } +} From 2499509ada6e437823c0b5af0171b43e01446ed4 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 9 Nov 2014 20:11:22 +0200 Subject: [PATCH 2/4] cleanup work --- handling-spring-static-resources/pom.xml | 215 +++++++++++------- .../AsynchronousSpringEventsConfig.java | 3 +- .../CustomSpringEventPublisher.java | 1 + 3 files changed, 140 insertions(+), 79 deletions(-) diff --git a/handling-spring-static-resources/pom.xml b/handling-spring-static-resources/pom.xml index fb29bf1a3d..2a78e4cc98 100644 --- a/handling-spring-static-resources/pom.xml +++ b/handling-spring-static-resources/pom.xml @@ -1,84 +1,91 @@ - + 4.0.0 org.baeldung handling-spring-static-resources handling-spring-static-resources + 0.1-SNAPSHOT war - 1.0.0-BUILD-SNAPSHOT - - 1.7 - 4.1.0.RELEASE - 3.2.0.RELEASE - 1.8.1 - 1.6.1 - - 2.3.2-b01 - + + + + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + org.springframework.security + spring-security-taglibs + ${org.springframework.security.version} + + org.springframework - spring-context - 4.1.0.RELEASE + spring-core + ${org.springframework.version} - - commons-logging commons-logging + commons-logging org.springframework - spring-webmvc - ${org.springframework-version} - - - org.springframework.security - spring-security-config - 3.2.5.RELEASE - runtime + spring-context + ${org.springframework.version} org.springframework - spring-context-support - 4.1.0.RELEASE + spring-jdbc + ${org.springframework.version} - + + org.springframework + spring-beans + ${org.springframework.version} + + + org.springframework + spring-aop + ${org.springframework.version} + + + org.springframework + spring-tx + ${org.springframework.version} + + + org.springframework + spring-expression + ${org.springframework.version} + + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + org.aspectj aspectjrt ${org.aspectj-version} - - - - org.slf4j - slf4j-api - ${org.slf4j-version} - - - org.slf4j - jcl-over-slf4j - ${org.slf4j-version} - runtime - - - org.slf4j - slf4j-log4j12 - ${org.slf4j-version} - runtime - - - log4j - log4j - 1.2.16 - runtime - - - javax.inject javax.inject @@ -101,31 +108,15 @@ jstl 1.2 - - - org.springframework.security - spring-security-taglibs - 3.2.5.RELEASE - - com.fasterxml.jackson.core jackson-databind - 2.4.1 - - - - - - - javax.validation - validation-api - 1.0.0.GA + ${jackson.version} org.hibernate hibernate-validator - 4.1.0.Final + ${hibernate-validator.version} @@ -141,13 +132,31 @@ handlebars 1.3.2 - - - org.springframework.security - spring-security-web - ${org.springframework.security-version} - + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + @@ -160,4 +169,54 @@ + + + 1.7 + 4.1.0.RELEASE + 3.2.0.RELEASE + 1.8.1 + 1.6.1 + 2.3.2-b01 + + + 4.1.1.RELEASE + 3.2.5.RELEASE + + + 4.3.7.Final + 5.1.32 + 1.6.2.RELEASE + + + + 2.4.2 + + + 1.7.7 + 1.1.2 + + + 5.1.3.Final + + + 18.0 + 3.3.2 + + + 1.3 + 4.11 + 1.10.8 + + 4.3.3 + 4.3.5 + + 2.3.4 + + + 3.2 + 2.5 + 2.17 + 1.4.10 + + \ No newline at end of file diff --git a/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java b/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java index 93f61bf517..082a986974 100644 --- a/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java +++ b/spring-all/src/main/java/org/baeldung/springevents/asynchronous/AsynchronousSpringEventsConfig.java @@ -12,9 +12,10 @@ import org.springframework.core.task.SimpleAsyncTaskExecutor; public class AsynchronousSpringEventsConfig { @Bean(name = "applicationEventMulticaster") - public static ApplicationEventMulticaster simpleApplicationEventMulticaster() { + public ApplicationEventMulticaster simpleApplicationEventMulticaster() { final SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster(); simpleApplicationEventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor()); return simpleApplicationEventMulticaster; } + } diff --git a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java index decbcf446a..4569de1d5e 100644 --- a/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java +++ b/spring-all/src/main/java/org/baeldung/springevents/synchronous/CustomSpringEventPublisher.java @@ -15,4 +15,5 @@ public class CustomSpringEventPublisher { final CustomSpringEvent customSpringEvent = new CustomSpringEvent(this, message); applicationEventPublisher.publishEvent(customSpringEvent); } + } From 2c9fa6a01010e34e2b0e3c89b4e470ab4f2b87f6 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 9 Nov 2014 20:11:47 +0200 Subject: [PATCH 3/4] formatting cleanup --- ...SimpleUrlAuthenticationSuccessHandler.java | 8 +++--- .../java/org/baeldung/spring/MvcConfig.java | 28 ++++++++----------- .../baeldung/spring/SecSecurityConfig.java | 2 +- .../web/controller/HomeController.java | 13 ++++----- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java b/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java index 45ce91adc8..730def5adf 100644 --- a/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java +++ b/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java @@ -18,7 +18,7 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu 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); HttpSession session = request.getSession(false); if (session != null) { @@ -39,9 +39,9 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu } protected String determineTargetUrl(Authentication authentication) { - - return "/home.html"; - + + return "/home.html"; + } protected void clearAuthenticationAttributes(HttpServletRequest request) { diff --git a/handling-spring-static-resources/src/main/java/org/baeldung/spring/MvcConfig.java b/handling-spring-static-resources/src/main/java/org/baeldung/spring/MvcConfig.java index bd874a6e67..4a198cf195 100644 --- a/handling-spring-static-resources/src/main/java/org/baeldung/spring/MvcConfig.java +++ b/handling-spring-static-resources/src/main/java/org/baeldung/spring/MvcConfig.java @@ -1,9 +1,7 @@ package org.baeldung.spring; - import java.util.Locale; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; @@ -42,9 +40,9 @@ public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(final ViewControllerRegistry registry) { super.addViewControllers(registry); - registry.addViewController("/login.html"); + registry.addViewController("/login.html"); registry.addViewController("/home.html"); - + } @Bean @@ -55,23 +53,21 @@ public class MvcConfig extends WebMvcConfigurerAdapter { bean.setSuffix(".jsp"); return bean; } - + @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - //For examples using Spring 4.1.0 - if((env.getProperty("resource.handler.conf")).equals("4.1.0")){ + public void addResourceHandlers(ResourceHandlerRegistry registry) { + // For examples using Spring 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("/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("/other-files/**").addResourceLocations("file:/Users/Elena/"). - setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver()); - } - //For examples using Spring 4.0.7 - else if ((env.getProperty("resource.handler.conf")).equals("4.0.7")){ + registry.addResourceHandler("/other-files/**").addResourceLocations("file:/Users/Elena/").setCachePeriod(3600).resourceChain(true).addResolver(new GzipResourceResolver()); + } + // For examples using Spring 4.0.7 + else if ((env.getProperty("resource.handler.conf")).equals("4.0.7")) { registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/", "classpath:/other-resources/"); - registry.addResourceHandler("/files/**") - .addResourceLocations("file:/Users/Elena/"); - + registry.addResourceHandler("/files/**").addResourceLocations("file:/Users/Elena/"); + } } diff --git a/handling-spring-static-resources/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/handling-spring-static-resources/src/main/java/org/baeldung/spring/SecSecurityConfig.java index d51d0e9a50..4da114c78b 100644 --- a/handling-spring-static-resources/src/main/java/org/baeldung/spring/SecSecurityConfig.java +++ b/handling-spring-static-resources/src/main/java/org/baeldung/spring/SecSecurityConfig.java @@ -7,7 +7,7 @@ import org.springframework.context.annotation.ImportResource; @ImportResource({ "classpath:webSecurityConfig.xml" }) public class SecSecurityConfig { - public SecSecurityConfig() { + public SecSecurityConfig() { super(); } diff --git a/handling-spring-static-resources/src/main/java/org/baeldung/web/controller/HomeController.java b/handling-spring-static-resources/src/main/java/org/baeldung/web/controller/HomeController.java index aa2b9f63fc..44645a1471 100644 --- a/handling-spring-static-resources/src/main/java/org/baeldung/web/controller/HomeController.java +++ b/handling-spring-static-resources/src/main/java/org/baeldung/web/controller/HomeController.java @@ -14,22 +14,19 @@ import org.springframework.web.context.request.WebRequest; @Controller public class HomeController { - - - + @Autowired Environment env; - @RequestMapping(value = "/home", method = RequestMethod.GET) public String showHome(WebRequest request, Model model, Locale locale) throws IOException { - + Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); - + String formattedDate = dateFormat.format(date); - model.addAttribute("serverTime", formattedDate ); + model.addAttribute("serverTime", formattedDate); return "home"; } - + } From 5ba5027e5085db584b72bdb21dec4924aad41cd0 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 9 Nov 2014 20:13:33 +0200 Subject: [PATCH 4/4] static resources project now eclipse web project --- handling-spring-static-resources/.classpath | 6 ++---- handling-spring-static-resources/.project | 5 +++-- .../security/MySimpleUrlAuthenticationSuccessHandler.java | 1 - 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/handling-spring-static-resources/.classpath b/handling-spring-static-resources/.classpath index ceb436dc82..71a1c1e0b5 100644 --- a/handling-spring-static-resources/.classpath +++ b/handling-spring-static-resources/.classpath @@ -19,11 +19,9 @@ - - - + - + diff --git a/handling-spring-static-resources/.project b/handling-spring-static-resources/.project index 3d1ebc3a95..a16def8042 100644 --- a/handling-spring-static-resources/.project +++ b/handling-spring-static-resources/.project @@ -31,17 +31,18 @@ - org.eclipse.m2e.core.maven2Builder + org.hibernate.eclipse.console.hibernateBuilder - org.hibernate.eclipse.console.hibernateBuilder + org.eclipse.m2e.core.maven2Builder + org.eclipse.jem.workbench.JavaEMFNature org.springframework.ide.eclipse.core.springnature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature diff --git a/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java b/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java index 730def5adf..316642ab6f 100644 --- a/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java +++ b/handling-spring-static-resources/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java @@ -17,7 +17,6 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); - @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { handle(request, response, authentication); HttpSession session = request.getSession(false);