Académique Documents
Professionnel Documents
Culture Documents
CONTENTS INCLUDE:
Spring Annotations
n
Core Spring Annotations
n
Spring MVC Annotations
n
AspectJ Annotations
n
JSR-250 Annotations
Testing Annotations
By Craig Walls
n
n
Hot Tips and more...
At the class level, @Transactional is declaring that all methods @Pointcut Method Declares an empty method as a pointcut
placeholder method.
should support transactions and be read-only. But, at the
method-level, @Transactional declares that the storeTreasure()
What’s important to note, however, is that while you can use
method requires a transaction and is not read-only.
AspectJ annotations to define Spring aspects, those aspects will
Note that for transactions to be applied to @Transactional- be defined in the context of Spring AOP and will not be handled
annotated classes, those classes must be wired as beans in Spring. by the AspectJ runtime. This is significant because Spring AOP
JMX Annotations is limited to proxying method invocations and does not provide
for the more exotic pointcuts (constructor interception, field
These annotations, used with the <context:mbean-export> interception, etc.) offered by AspectJ.
element, declare bean methods and properties as MBean
operations and attributes. Annotating Aspects
Annotations Use Description To use AspectJ annotations to create Spring aspects, you’ll first
@ManagedAttribute Method Used on a setter or getter method need to provide a bit of Spring XML plumbing:
to indicate that the bean’s property
should be exposed as a MBean <beans xmlns="http://www.springframework.org/schema/
attribute. beans"
@ManagedNotification Type Indicates a JMX notification emitted xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
by a bean. xmlns:aop="http://www.springframework.org/schema/aop"
@ManagedNotifications Type Indicates the JMX notifications xsi:schemaLocation="http://www.springframework.org/
emitted by a bean. schema/beans
@ManagedOperation Method Specifies that a method should be http://www.springframework.org/schema/beans/
exposed as a MBean operation. spring-beans-2.5.xsd
@ManagedOperationParameter Method Used to provide a description for an http://www.springframework.org/schema/aop
operation parameter. http://www.springframework.org/schema/aop/spring-
@ManagedOperationParameters Method Provides descriptions for one or aop-2.5.xsd">
more operation parameters. …
@ManagedResource Type Specifies that all instances of a class <aop:aspectj-autoproxy/>
should be exposed a MBeans. …
</beans>
Exposing a Spring Bean as a MBean
To get started with Spring-annotated MBeans, you’ll need to The <aop:aspectj-autoproxy> element tells Spring to watch for
include <context:mbean-export> in the Spring XML configuration: beans annotated with AspectJ annotations and, if it finds any, to
<context:mbean-export/> use them to create aspects. Then you can annotate bean classes
to be aspects:
Then, you can annotate any of your Spring-managed beans to be
@Aspect
exported as MBeans:
public class ChantySinger {
@ManagedResource(objectName="pirates:name=PirateService")
@Pointcut(“execution(* Pirate.plunder(..))”)
public interface PirateService {
public void plunderPC() {}
@ManagedOperation(
description="Get the pirate list")
@Before(“plunderPC()”)
public List<Pirate> getPirateList();
public void singYoHo() {
}
…
Here, the PirateService has been annotated to be exported as a }
MBean and its getPirateList() method is a managed operation.
@AfterReturning(“plunderPC()”)
public void singAPiratesLifeForMe() {
ASPECTJ ANNOTATIONS …
}
}
For defining aspects, Spring leverages the set of annotations
provided by AspectJ. This simple annotation-based aspect has a pointcut that is
Annotation Use Description triggered by the execution of a plunder() method on the Pirate
@Aspect Type Declares a class to be an aspect. class. Before the Pirate.plunder() method is executed, the
@After Method Declares a method to be called after a singYoHo() method is called. Then, after the Pirate.plunder()
pointcut completes. method returns successfully, the singAPiratesLifeForMe()
@PreDestroy @RunWith(SpringJUnit4ClassRunner.class)
public void goAway() { @ContextConfiguration(locations = { "pirates.xml" })
System.out.println(“Yar!”); public class PirateTest {
} …
} }
As annotated, the wakeUp() method will be invoked just after With test configured to load a Spring application context, you
Spring instantiates the bean and goAway() will be invoked just now may request that Spring autowire properties of the test class
before the bean is removed from the Spring container. with beans from the Spring context:
DZone, Inc.
1251 NW Maynard
ISBN-13: 978-1-934238-29-5
Cary, NC 27513
ISBN-10: 1-934238-29-5
50795
888.678.0399
DZone communities deliver over 4 million pages each month to 919.678.0300
more than 1.7 million software developers, architects and decision
Refcardz Feedback Welcome
makers. DZone offers something for everyone, including news, refcardz@dzone.com
$7.95
tutorials, cheatsheets, blogs, feature articles, source code and more. Sponsorship Opportunities 9 781934 238295
“DZone is a developer’s dream,” says PC Magazine. sales@dzone.com
Copyright © 2008 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, Version 1.0
photocopying, or otherwise, without prior written permission of the publisher. Reference: Spring in Action, 2nd Edition, Craig Walls, Manning Publications, 2007.