Annotated component declaration



GitHub: https://github.com/SylkeWay/synthful


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:
@Component
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
@Inject
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;

  @Inject
  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 - wally=com.synthful.angst.app.durian.Wally@2b44a3c4
.....
INFO  DurianMVController - mvc wally=com.synthful.angst.app.durian.Wally@2b44a3c4

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


No comments:

Post a Comment