Spring @configuration - So Long Spring XMLs

24
Make your life easier with Spring @Configuration @By Avi Etzioni

Transcript of Spring @configuration - So Long Spring XMLs

Make your life easier with

Spring @Configuration@By Avi Etzioni

Some history…

October 2002

Rod Johnson releases Spring, a new framework for easy

implementation of IoC

Configuration is done in XML files

Allows complete separation between code and wiring

Not everyone liked it

XMLs are not readable and very verbose

They lack things like:

Type safety

auto-completion

Smart navigation

Debugging

Xdoclet – a tool that (amongst other things) generated

XMLs from java doc comments

September 2004

Java 5 is released

Introduces Metadata

Which is better known as annotations

November 2007

Spring 2.5 is released

Introduces annotation-based configuration:

Full-featured annotation-driven DI

Auto-detection of application components (in the classpath) and auto-configuring them as Spring managed objects.

Simplified

1. Annotate your beans with suitable annotation

2. Annotate the injection points with @Autowired or @Resource

3. Let the magic happen

But annotations had their

problems too

No central point of wiring

Harder (though possible) to replace entire

configuration

With XMLs you could just load another main XML context

file

Obscures the injected class

December 2009

Spring 3.0 is released

Introduces @Configuration classes:

Like XMLs – one point for declaring configuration

But better– Have all the capabilities of code

You can now:

Debug the context startup

Perform conditionals (Did someone say feature-flags??)

And use IDE’s capabilities (even without the Ultimate

version)

How does it work?

Create a @Configuration class

Instead of <beans> use @Configuration:

Add methods for generating beans

What about the dependencies?

Instead of <bean> use @Bean:

Dependencies – Option 1: Class

Members

restTemplate

needs to be

injected

Name of bean to inject

Dependencies – Option 2: Method

Variables

What about properties??

Use @Value for injecting propeties

Feature Flags

It’s important to understand

Spring doesn’t care about the method of the beans

declaration:

XML bean == Annotation bean == @Configuration bean

You can mix the whole 3 ways of declaring beans

THIS MAKES IT VERY EASY TO MIGRATE FROM XML TO

@Configuration!!!!

Just migrate one XML at a time

Importing XMLs from

@Configuration

In XML we use <import resource=“…”/>

In @Configuration classes we use @ImportResource

Just copy the classpath from the original <import> tag

Importing Other @Configuration classes

We use @Import to import another @Configuration class:

Importing @Configuration from XML

Declare <context:annotation-config/> tag in any of your

XMLs

Declare a bean with the class type of the

@Configuration class

Import directly from the servlet

web.xml Taken from ImageProvider

Xml Style

@Configuration Style

Don’t abuse

Code is much easier to abuse than XMLs

Keep the configuration – configuration

Avoid logic inside @Configuration (aside from feature

flags)

Conventions

Always put under a package “context”

Use suffix *ApplicationContext.java (to ease the search)

Don’t overload the @Configuration with code – split

them as if they were XMLs

ImageProvider:

Look mommy, no XMLs