Skip to content

Commit b5cc62b

Browse files
author
mgooty
committed
Added sample code for Spring events and Spring profiles.
1 parent fa42403 commit b5cc62b

25 files changed

+458
-2
lines changed

spring-all/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</dependency>
3434

3535
<!-- persistence -->
36-
36+
3737
<dependency>
3838
<groupId>org.hibernate</groupId>
3939
<artifactId>hibernate-core</artifactId>
@@ -118,7 +118,6 @@
118118
<version>${mockito.version}</version>
119119
<scope>test</scope>
120120
</dependency>
121-
122121
</dependencies>
123122

124123
<build>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.baeldung.profiles;
2+
3+
public interface DatasourceConfig {
4+
public void setup();
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.profiles;
2+
3+
import org.springframework.context.annotation.Profile;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Profile("dev")
8+
public class DevDatasourceConfig implements DatasourceConfig {
9+
10+
@Override
11+
public void setup() {
12+
System.out.println("Setting up datasource for DEV environment. ");
13+
}
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.profiles;
2+
3+
import org.springframework.context.annotation.Profile;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@Profile("production")
8+
public class ProductionDatasourceConfig implements DatasourceConfig {
9+
10+
@Override
11+
public void setup() {
12+
System.out.println("Setting up datasource for PRODUCTION environment. ");
13+
}
14+
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.profiles;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("org.baeldung.profiles")
8+
public class SpringProfilesConfig {
9+
10+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.baeldung.scheduling;
2+
3+
import org.springframework.scheduling.annotation.Scheduled;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("scheduledAnnotationExample")
7+
public class ScheduledAnnotationExample {
8+
9+
@Scheduled(fixedDelay = 1000)
10+
public void scheduleFixedDelayTask() {
11+
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
12+
}
13+
14+
@Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}")
15+
public void scheduleFixedDelayTaskUsingExpression() {
16+
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
17+
}
18+
19+
@Scheduled(fixedDelay = 1000, initialDelay = 2000)
20+
public void scheduleFixedDelayWithInitialDelayTask() {
21+
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
22+
}
23+
24+
@Scheduled(fixedRate = 1000)
25+
public void scheduleFixedRateTask() {
26+
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
27+
}
28+
29+
@Scheduled(fixedRateString = "${fixedRate.in.milliseconds}")
30+
public void scheduleFixedRateTaskUsingExpression() {
31+
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
32+
}
33+
34+
@Scheduled(fixedDelay = 1000, initialDelay = 100)
35+
public void scheduleFixedRateWithInitialDelayTask() {
36+
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
37+
}
38+
39+
/**
40+
* Scheduled task is executed at 10:15 AM on the 15th day of every month
41+
*/
42+
@Scheduled(cron = "0 15 10 15 * ?")
43+
public void scheduleTaskUsingCronExpression() {
44+
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
45+
}
46+
47+
@Scheduled(cron = "${cron.expression}")
48+
public void scheduleTaskUsingExternalizedCronExpression() {
49+
System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000);
50+
}
51+
52+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.scheduling;
2+
3+
public class SchedulingWithXmlConfig {
4+
5+
public void scheduleFixedDelayTask() {
6+
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
7+
}
8+
9+
public void scheduleFixedRateTask() {
10+
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
11+
}
12+
13+
public void scheduleTaskUsingCronExpression() {
14+
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.scheduling;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.PropertySource;
7+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
8+
import org.springframework.scheduling.annotation.EnableScheduling;
9+
10+
@Configuration
11+
@EnableScheduling
12+
@ComponentScan("org.baeldung.scheduling")
13+
@PropertySource("classpath:springScheduled.properties")
14+
public class SpringSchedulingConfig {
15+
16+
@Bean
17+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
18+
return new PropertySourcesPlaceholderConfigurer();
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.springevents.asynchronous;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.event.ApplicationEventMulticaster;
7+
import org.springframework.context.event.SimpleApplicationEventMulticaster;
8+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
9+
10+
@Configuration
11+
@ComponentScan("org.baeldung.springevents.synchronous")
12+
public class AsynchronousSpringEventsConfig {
13+
14+
@Bean(name = "applicationEventMulticaster")
15+
public static ApplicationEventMulticaster simpleApplicationEventMulticaster() {
16+
final SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
17+
simpleApplicationEventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
18+
return simpleApplicationEventMulticaster;
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.springevents.synchronous;
2+
3+
import org.springframework.context.ApplicationListener;
4+
import org.springframework.context.event.ContextRefreshedEvent;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
9+
10+
@Override
11+
public void onApplicationEvent(final ContextRefreshedEvent cse) {
12+
System.out.println("Handling context re-freshed event. ");
13+
}
14+
15+
}

0 commit comments

Comments
 (0)