Spring Annotations: Become A Spring Guru
Spring Annotations: Become A Spring Guru
1 of 8
https://dzone.com/refcardz/spring-annotations
Refcard #026
Craig Walls
Principal Consultant,
Improving Enterprises
SECTION 1
Spring Annotations
From its beginning, Spring's most common means of configuration has been XML-based. But as
developers grow weary of navigating through a seemingly endless maze of angle-brackets, some have
started looking for other ways to wire the beans in their Spring-enabled applications. Spring has
responded with several annotation-driven configuration options. In this reference card, you'll find a
guide to all of the annotations supported in Spring 2.5.
12/23/2016 11:51 PM
2 of 8
https://dzone.com/refcardz/spring-annotations
SECTION 2
USE
DESCRIPTION
@Autowired
Constructor, Field,
Method
@Configurable
Type
@Order
Type, Method,
Field
@Qualifier
Field, Parameter,
Type, Annotation
Type
@Required
Method (setters)
@Scope
Type
But it's also possible to have Spring automatically inject a bean's properties from other beans in the
context. For example, if the Pirate class were annotated with @Autowired like this...
public class Pirate { private String name; private TreasureMap treasureMap; public Pirate(String n
ame) { this.name = name; } @Autowired public void setTreasureMap(TreasureMap treasureMap) { this.t
reasureMap = treasureMap; } }
...and if you were to configure annotation configuration in Spring using the <context:annotationconfiguration> element like this...
<beans ... > <bean id="pirate" class="Pirate"> <constructor-arg value="Long John Silver" /> </bean
> <bean id="treasureMap" class="TreasureMap" /> <context:annotation-config /> </beans>
...then the "treasureMap" property will be automatically injected with a reference to a bean whose type
is assignable to TreasureMap (in this case, the bean whose ID is "treasureMap").
3 of 8
https://dzone.com/refcardz/spring-annotations
SECTION 3
Aspect Annotations
For defining aspects, Spring leverages the set of annotations provided by AspectJ.
ANNOTATION
USE
DESCRIPTION
@Aspect
Type
@After
Method
@AfterReturning
Method
@AfterThrowing
Method
@Around
Method
@Before
Method
@DeclareParents
Static
Field
Declares that matching types should be given new parents,that is, it introduces
new functionality into matching types.
@Pointcut
Method
What's important to note, however, is that while you can use AspectJ annotations to define Spring
aspects, those aspects will be defined in the context of Spring AOP and will not be handled by the
AspectJ runtime. This is significant because Spring AOP is limited to proxying method invocations and
does not provide for the more exotic pointcuts (constructor interception, field interception, etc.)
offered by AspectJ.
Annotating Aspects
To use AspectJ annotations to create Spring aspects, you'll first need to provide a bit of Spring XML
plumbing:
<beans xmlns="http://www.springframework.org/schema/ beans" xmlns:xsi="http://www.w3.org/2001/XMLS
chema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://w
ww.springframework.org/ schema/beans http://www.springframework.org/schema/beans/ spring-beans-2.5
.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/springaop
- 2.5.xsd"> ... <aop:aspectj-autoproxy/> ... </beans>
The <aop:aspectj-autoproxy> element tells Spring to watch for beans annotated with AspectJ
annotations and, if it finds any, to use them to create aspects. Then you can annotate bean classes to
be aspects:
@Aspect public class ChantySinger { @Pointcut("execution(* Pirate.plunder(..))") public void plund
erPC() {} @Before("plunderPC()") public void singYoHo() { ... } @AfterReturning("plunderPC()") pub
lic void singAPiratesLifeForMe() { ... } }
This simple annotation-based aspect has a pointcut that is triggered by the execution of a plunder()
method on the Pirate class. Before the Pirate.plunder() method is executed, the singYoHo() method is
called. Then, after the Pirate.plunder() method returns successfully, the singAPiratesLifeForMe()
method is invoked. (For more advanced examples of AspectJ annotations, see the AspectJ
documentation at http://www.eclipse.org/aspectj/docs.php.)
Note the rather odd looking plunderPC() method. It is annotated with @Pointcut to indicate that this
method is a pointcut placeholder. The key thing here is that the most interesting stuff happens in the
annotation itself and not in the method. In fact, pointcut placeholder methods must be empty methods
12/23/2016 11:51 PM
4 of 8
https://dzone.com/refcardz/spring-annotations
SECTION 4
JSR-250 Annotations
In addition to Spring's own set of annotations, Spring also supports a few of the annotations defined
by JSR-250, which is the basis for the annotations used in EJB 3.
ANNOTATION
USE
DESCRIPTION
@PostConstruct
Method
@PreDestroy
Method
@Resource
Method,
Field
Indicates that a method or field should be injected with a named resource (by
default, another bean).
In this case, Spring will attempt to wire the "treasureMap" property with a reference to a bean whose
ID is "treasureMap". If you'd rather explicitly choose another bean to wire into the property, specify it
to the name attribute:
public class Pirate { @Resource(name="mapToSkullIsland") private TreasureMap treasureMap; }
As annotated, the wakeUp() method will be invoked just after Spring instantiates the bean and
goAway() will be invoked just before the bean is removed from the Spring container.
12/23/2016 11:51 PM
5 of 8
https://dzone.com/refcardz/spring-annotations
SECTION 5
Testing Annotations
These annotations are useful for creating unit tests in the JUnit 4 style that depend on Spring beans
and/or require a transactional context.
ANNOTATION
USE
DESCRIPTION
@AfterTransaction
Method
@BeforeTransaction
Method
@ContextConfiguration
Type
@DirtiesContext
Method
@ExpectedException
Method
@IfProfileValue
Type,
Method
@NotTransactional
Method
@ProfileValueSourceConfiguration
Type
@Repeat
Method
@Rollback
Method
@TestExecutionListeners
Type
@Timed
Method
Specifies a time limit for the test method. If the test does not
complete before the time has expired, the test will fail.
Type
@TransactionConfiguration
In this case, the Spring test runner will try to load a Spring application context from a file named
PirateTest-context.xml. If you'd rather specify one or more XML files to load the application context
from, you can do that with @ContextConfiguration:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "pirates.xml" }) publi
c class PirateTest { ... }
12/23/2016 11:51 PM
6 of 8
https://dzone.com/refcardz/spring-annotations
Publications
Building
Maintainable and
Scalable Software
Learn design patterns
quickly with Jason
McDonald's outstanding
tutorial on the original 23
Gang of Four design
patterns, including class
diagrams, explanations,
usage info, and real world
examples.
Jason
McDonald
Featured
A Power-User's
Guide to Java
Gives you an overview of
key aspects of the Java
language and references
on the core library,
commonly used tools, and
new Java 8 features.
Cay
Horstmann
Latest
Popular
Unlocking
Unobtrusive
JavaScript
The De Facto
Standard For
Version Control
Dave
Crane
123.9k
100.9k
332.9k
207.6k
201.3k
559.8k
Matthew
McCullough
112.1k
259.0k
Dependency
Injection in a
Nutshell
Catalogs the XML
elements available as of
Spring 2.5 and highlights
those most commonly
used: a handy resource
for Spring context
configuration.
Craig
Walls
102.4k
257.8k
REST Practices
The Representational
State Transfer (REST)
architectural style is a
worldview that elevates
information into a
first-class element of
architectures. REST
allows us to achieve the
architectural properties of
performance, scalability,
generality, simplicity,
modifiability, and
extensibility. This newly
updated Refcard explains
main HTTP verbs,
describes response
codes, and lists libraries
and frameworks. It also
gives additional resources
to further explore each
12/23/2016 11:51 PM
7 of 8
https://dzone.com/refcardz/spring-annotations
topic.
Brian
Sletten
96.63k
Understanding the
Core Concurrency
Concepts
Helps Java developers
working with multithreaded programs
understand the core
concurrency concepts and
how to apply them.
Alex
Miller
86.13k
89.00k
229.7k
184.4k
145.1k
Understanding
Selectors
Covers Core principles of
CSS that will expand and
strengthen your
professional ability to
work with CSS. Part two
of three.
Molly
Holzschlag
72.87k
139.3k
Mickael
Istria
77.28k
201.1k
ABOUT US
ADVERTISE
Stay Updated
About DZone
Send feedback
Careers
Media Kit
[email protected]
+1 (919) 443-1644
CONTRIBUTE ON
DZONE
MVB Program
Zone Leader Program
Become a Contributor
CONTACT US
150 Preston Executive Drive
Cary, NC 27513
[email protected]
+1 (919) 678-0300
SEE AN EXAMPLE
Enter Email
Let's be friends:
12/23/2016 11:51 PM
8 of 8
https://dzone.com/refcardz/spring-annotations
LEGAL
Terms of Service
Privacy Policy
DZone.com is powered by
12/23/2016 11:51 PM