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

Spring @configuration - So Long Spring XMLs

Jul 13, 2015

Download

Software

Avi Etzioni
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Spring @configuration - So Long Spring XMLs

Make your life easier with

Spring @Configuration@By Avi Etzioni

Page 2: Spring @configuration - So Long Spring XMLs

Some history…

Page 3: Spring @configuration - So Long Spring XMLs

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

Page 4: Spring @configuration - So Long Spring XMLs

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

Page 5: Spring @configuration - So Long Spring XMLs

September 2004

Java 5 is released

Introduces Metadata

Which is better known as annotations

Page 6: Spring @configuration - So Long Spring XMLs

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

Page 7: Spring @configuration - So Long Spring XMLs

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

Page 8: Spring @configuration - So Long Spring XMLs

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)

Page 9: Spring @configuration - So Long Spring XMLs

How does it work?

Page 10: Spring @configuration - So Long Spring XMLs

Create a @Configuration class

Instead of <beans> use @Configuration:

Page 11: Spring @configuration - So Long Spring XMLs

Add methods for generating beans

What about the dependencies?

Instead of <bean> use @Bean:

Page 12: Spring @configuration - So Long Spring XMLs

Dependencies – Option 1: Class

Members

restTemplate

needs to be

injected

Name of bean to inject

Page 13: Spring @configuration - So Long Spring XMLs

Dependencies – Option 2: Method

Variables

Page 14: Spring @configuration - So Long Spring XMLs

What about properties??

Page 15: Spring @configuration - So Long Spring XMLs

Use @Value for injecting propeties

Page 16: Spring @configuration - So Long Spring XMLs

Feature Flags

Page 17: Spring @configuration - So Long Spring XMLs

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

Page 18: Spring @configuration - So Long Spring XMLs

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

Page 19: Spring @configuration - So Long Spring XMLs

Importing Other @Configuration classes

We use @Import to import another @Configuration class:

Page 20: Spring @configuration - So Long Spring XMLs

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

Page 21: Spring @configuration - So Long Spring XMLs

Import directly from the servlet

web.xml Taken from ImageProvider

Xml Style

@Configuration Style

Page 22: Spring @configuration - So Long Spring XMLs

Don’t abuse

Code is much easier to abuse than XMLs

Keep the configuration – configuration

Avoid logic inside @Configuration (aside from feature

flags)

Page 23: Spring @configuration - So Long Spring XMLs

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

Page 24: Spring @configuration - So Long Spring XMLs

ImageProvider:

Look mommy, no XMLs