Merge pull request #70 from mgooty/master
Added sample code for Spring events and Spring profiles.
This commit is contained in:
commit
bd697c383e
|
@ -0,0 +1,5 @@
|
|||
package org.baeldung.profiles;
|
||||
|
||||
public interface DatasourceConfig {
|
||||
public void setup();
|
||||
}
|
|
@ -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. ");
|
||||
}
|
||||
|
||||
}
|
|
@ -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. ");
|
||||
}
|
||||
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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. ");
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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. ");
|
||||
}
|
||||
}
|
|
@ -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.");
|
||||
}
|
||||
}
|
|
@ -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. ");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue