Reactive (webflux)

pom.xml

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.7.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<scope>provided</scope>
		</dependency>	
    	<dependency>
        	<groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-starter-webflux</artifactId>
        	<exclusions>
        		<exclusion>
        			<groupId>org.springframework.boot</groupId>
        			<artifactId>spring-boot-starter-logging</artifactId>
        		</exclusion>
        	</exclusions>
    	</dependency>  	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		
		<dependency>
			<groupId>io.projectreactor.ipc</groupId>
			<artifactId>reactor-netty</artifactId>
		</dependency>
		
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>	

Application Events and Listeners

兩種方式註冊

  • META-INF/spring.factories
org.springframework.context.ApplicationListener=com.example.project.MyListener
  • System.setProperty("context.listener.classes","zzz.zzz.zzz,xxx.xxx.xxx");

ApplicationEnvironmentPreparedEvent

用於初始ENVIRONMENT
public class Aaa implements pplicationListener<ApplicationEnvironmentPreparedEvent{
	
	@Override
	public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
		propertySources=event.getEnvironment().getPropertySources();				
	}
}

ApplicationPreparedEvent

public class Bbb implements ApplicationListener<ApplicationPreparedEvent> {

	@Override
	public void onApplicationEvent(ApplicationPreparedEvent event) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) event.getApplicationContext();

		BeanDefinition def = BeanDefinitionBuilder
				.genericBeanDefinition(SomeClass.class, () -> getSomeInstance())
                .setDestroyMethodName("close")
				.getBeanDefinition();
		registry.registerBeanDefinition("SomeBeanName", def);		
	}

}

Micrometter

  • 初始AOP時就可建立
	public MonitorAspect(@Autowired MeterRegistry registry ,@Autowired SomeService service){
		Counter someCounter=Counter
			    .builder("app.xxx.xxx.TotalSizes")
			    .description("Total Size")
			    .register(registry);
		Gauge.builder("app.xxxx.xxxx.CurrentSize", service, t->t.findCount())
	    .description("The current size .") 			    
	    .register(registry);
	}
  • 如果只有jersey-starter,沒有web-starter,要注意加入spring-webmvc,不然會以jersey path為主,並且無法透過properties變更

Notes

  • 假如 config.xml有用schemaLocation,網路上的sample https開頭會透過網路下載,http才會map到jar檔內,參考spring-beans.jar內的spring.schemas