Monday, August 28, 2017

Spring boot conditional configuration

SpEL Ternary Operator

The @Value annotation allow setting SpEL expressions: 
@Value("#{httpMode.equals(\"secure\") ? 43 : 80}")
int portNumber;

Profiles

Profiles are used to alter the default bean selection and wiring and also de Spring environment properties source.

Activation

Any number of profiles can be activated.

Profiles can be activated by setting the spring.profiles.active Spring environment variable (in a property file or any other property source).
spring.profiles.active=profile1,profile2,...

The  --spring.profiles.active=profile1,profile2,... command line switch can also be used.

This methods allow to unconditionally set the full active profile set but you can also add (not replace) additional profiles to the existing ones with the spring.profiles.include property.

Profiles can also be activated programatically with SpringApplication.setAdditionalProfiles or the ConfigurableEnvironment interface.

Profiles can also be activated on tests with the @ActiveProfiles annotation.

Default profile

When no profile is activated there is still a "default" profile active.

The default profile can also be activated with other profiles by explicitly adding it ("default") to the active profile set.

@Profile annotation


@Profile({"profile1", "profile2", ...})
Loads the annotated bean when one of the profiles of the annotation is active.

It can be used on any @Component, complete @Configuration or any of its @Bean methods.

@Profile({"!profile1"})
Loads the bean when the profile is not present.

If the @Profile annotation is omitted, registration will occur regardless of which (if any) profiles are active.

@Profile("default", "profile1", ...)
Any bean annotated with @Profile will not be loaded when no profile is set unless the "default" profile is included in the annotation.

@IfProfileActive

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/annotation/IfProfileValue.html

Is an old Spring 2.0 annotation that enables or disables a test based on a property or set of Spring environment property value. It has no relation with @Profile.

@IfProfileValue(name = "java.vendor", value = "Oracle Corporation")
public void testSomething() {
   ...
}

@Primary annotation

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Primary.html

Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency.

It can be used on any @Component, complete @Configuration or any of its @Bean methods.

Can be combined with @Profile providing a way to replace any bean with the profile ones.

@Conditional annotation


The most powerfull annotation for conditional configuration. Spring Boot module makes heavy use of it.

It can be used on any @Component, complete @Configuration or any of its @Bean methods.


References

Saturday, August 19, 2017

Asynchronous and Parallel Programming in C#

History and current status

  • .NET Framework 1.1 - C# 1.1: Asynchronous Programming Model (APM).
  • .NET Framework 2 - C# 2: Event Asynchronous Pattern (EAP). 
  • .NET Framework 4 - C# 4: Task Parallel Library (TPL). Mostly multi-threading oriented.
  • .NET Framework 4.5 - C# 5: Asynchronous methods (async/await). Task Continuations are often not needed. Huge code noise reduction. Better support in the Basic Class Library (BCL). "Async" suffix in many methods. Mostly asynchronous programming oriented.
Today the preferred way to handle asynchronous programming is by using Tasks with async/await first and then TPL.

Caution! in the TPL world tasks are usually "Delegate tasks" (tied to threads) but async/await tasks are mostly "Promise tasks". See https://blog.stephencleary.com/2014/04/a-tour-of-task-part-0-overview.html

Extensions

Resources

Thursday, August 10, 2017

MobaXterm


Vagrant
Vagrant does not run when the env variable VAGRANT_DETECTED_OD=cygwin. It can be solved with:
export VAGRANT_DETECTED_OS=

Thursday, August 3, 2017