Annotated component declaration


To be designated as a Spring component, a class may be annotated by anyone of the following
Annotation Description
@Component Plain old component
@Service There is no difference in behaviour of @Component and @Service annotations, but differences may gradually creep in in the future. @Service is intended for annotating service level components.
@Repository Triggers binding persistence layer exception handling.
@Controller Triggers servlet mapping and handling for the annotated class and its annotated methods.

Instead of declaring as a bean in the XML context-config file, Class Wally is annotated:
public class Wally {
  final private String name = "Wally";
  final private int value = 1000;

  public String getName() {
    return name;

  public int getValue() {
    return value;

Regardless which context bootstrap had discovered the annotation of Wally class, the Wally singleton is injectable in AppInfo
public void setWally(Wally wally){
  this.wName = wally.getName();
  this.wValue = wally.getValue();
  getLogger().info("wally={}", wally);    

as well as in DurianMVController
public class DurianMVController
extends AAngsta{

  private Wally wally;

  public void setWally(Wally wally){
    this.wally = wally;
    getLogger().info("mvc wally={}", wally);    

The log4j output indicates the success of injection:
INFO  AppInfo - message=Open
INFO  AppInfo - greeting=#### Hello Bellow ####
INFO  AppInfo - aProperties={maxUsers=8}
INFO  AppInfo -
INFO  DurianMVController - mvc

AppInfo and DurianMVController are themselves annotated, in order for Spring to instantiate their singletons.
public class AppInfo
extends AAngsta{
public class DurianMVController
extends AAngsta{

No comments:

Post a Comment