/spring-javaconfig-demos

Spring JavaConfig for Annotation based Spring Configuration

Primary LanguageJava

spring-javaconfig-demos

Spring JavaConfig for Annotation-based Spring Configuration

General Features

  • Demonstration of JavaConfig Annotation based Spring Configuration
  • Beans created in src/main/java/com/gordondickens/javaconfig/AppContext.java
  • Tests are in src/test/java/com/gordondickens/javaconfig/AppContextText.java
  • Logback with SLF4J for logging, see src/test/resources/logback-test.xml for logging configuration

JavaConfig Notes

  • @Configuration classes must be non-final
  • @Configuration classes must be non-local (may not be declared within a method)
  • @Configuration classes must have a default/no-arg constructor
  • Cannot use @Autowired constructor parameters
  • Nested configuration classes must be static

01-Component-Scan

  • Demonstrates using @ComponentScan in place of XML based <context:component-scan/>

02-Enable-TX

  • Demonstrates using _@EnableTransactionManagement in place of XML based <tx:annotation-config/>

03-Import-Resource

  • Demonstrates using @ImportResource to import XML configuration similar to XML based <import/>

04-Import

  • Demonstrates using @Import in place to import another JavaConfig class similar to XML based <import/>

05-Property-Source

  • Demonstrates using @PropertySource to import properties similar to XML based <context:property-placeholder/>
  • Demonstrates using SpEL (Spring Expression Language) and @Value to inject a dependency into an @Bean via an argument

06-Nested-Configuration

  • Demonstrates Nested @Configuration within an existing @Configuration class
  • NOTE: Nested @Configuration classes MUST be static

07-Application-Initializer

  • Demonstrates using an Application Initializer to evaluate an environment parameter at runtime and choosing beans based on that descriminator
  • Demonstrates using the @Profile annotation for configuration classes
  • Demonstrates using the @ActiveProfiles test annotation for JUnit tests
  • Demonstrates how to set the profile and reload the context in the class com.gordondickens.javaconfig.Main.java class
  • The Main class can be executed from the command line with mvn exec:java

Lifecycle

  • TODO - To be implemented