DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Enterprise Spring Best Practices – Part 4 – Annotation Config

Enterprise Spring Best Practices – Part 4 – Annotation Config

Gordon Dickens user avatar by
Gordon Dickens
·
Nov. 24, 12 · Interview
Like (0)
Save
Tweet
Share
8.23K Views

Join the DZone community and get the full member experience.

Join For Free

 In this edition of Enterprise Spring Best Practices, let’s look at annotations. Several of the annotations became available in Spring 2.5. Spring 3.0 added convenient annotation discovery mechanisms which we’ll see here.

Annotations are available for Dependency Injection, Bean discovery and instantiation, post processing (required fields, initialization method, etc).

Annotations may or may NOT be discovered in java interfaces, refer to the documentation or JavaDoc for details on whether the annotation discovery mechanism will traverse upward in the class hierarchy.

Sections


  • Annotation Style Decision
  • Annotation Discovery from XML
  • Application Component Scanning
  • MVC Component Scanning
  • Component Scanning Best Practices
  • Further Reading
  • Social Me

Annotation Style Decision



Annotations help simplify configuration. All annotations in Java 5 and above, require some discovery mechanism.

Within Spring, we have 3 strategies for bean discovery and dependency injection

  1. XML based discovery
  2. JavaConfig
  3. Hybrid Configuration

Hybrid configuration is simply mixing standard XML <bean/> declarations with annotation methods discussed below. In the next blog, I will discuss JavaConfig, so for now lets focus on XML based discovery of Annotations.

Annotation Discovery from XML



All annotations REQUIRE a discovery directive. We MUST know which XML directives discover particular annotations, or the beans will not be processed.
For Spring annotation discovery detail, see Annotation Reference for Spring Projects

Option 1 – Register individual bean post processors

There are several individual Bean Post Processors that can be registered to discover specific annotation categories

  • RequiredAnnotationBeanPostProcessor – (@Required)
  • CommonAnnotationBeanPostProcessor – (@PostConstruct, @PreDestroy, @Resource, etc.)
  • AutowiredAnnotationBeanPostProcessor – (@Autowired, @Value, @Inject, etc.)
  • PersistenceAnnotationBeanPostProcessor – (@PersistenceUnit, @PersistenceContext, etc.)

Option 2 – Use the annotation-config directive

Automatically registers the Bean Post Processors above.

  <context:annotation-config/>

Option 3 – Use the component-scan directive

Automatically registers the Bean Post Processors above AND scans for component annotations @Component, @Repository, @Service, @Controller, and more…

  <context:context:component-scan 
      base-package="com.gordondickens.enterprisespring"/>

Application Component Scanning



Exclude configuration elements that are not part of the underlying application. Controllers are part of the application that deals with the user interface but are not pertinent to Web Services.
Within applicationContext-bootstrap.xml
<context:component-scan 
  base-package="com.gordondickens.enterprisespring">
    
  <context:exclude-filter
    expression="org.springframework.stereotype.Controller"
    type="annotation"/>
</context:component-scan>

MVC Component Scanning



Exclude components are not part of the entire application. If our application serves UI clients and web service clients, only the base components should be discovered in the main application configuration. We will discuss MVC configuration in a future blog.

Within applicationContext-webmvc.xml

<context:component-scan 
  base-package="com.gordondickens.enterprisespring"
  use-default-filters="false"/>

    <context:include-filter 
      expression="org.springframework.stereotype.Controller" 
      type="annotation"/>
</context:component-scan>

Component Scanning Best Practices



When Spring creates the application context, it aggregates all of the configuration into a single context, no matter how many configuration files or annotations are used.
  • Component scanning should ONLY be configured in the bootstrap config file, not in every XML config file
  • Do NOT also include the <context:annotation-config/> directive, it is automatically included by component scan
  • Do NOT start scanning from “com” and/or “org”, as this will scan ALL sub packages in all of the project and jars for candidates!
  • Be as specific as possible with the packages
  • Do NOT cross application boundaries with component-scan
    • Create an applicationContext-services.xml for scanning services
    • Create an applicationContext-persistence.xml for persistence and entity beans
    • Create an applicationContext-webmvc.xml for persistence and entity beans
    • Create an applicationContext-webservice.xml for web service beans
    • Import these references into the applicationContext-bootstrap.xml to these elements

Why separate the discovery files into layer specific configuration?

  • Unit testing is easier as discovery of beans is more specific
  • Allows the project to be separated into multiple Jar/Wars
  • Lowers risk of widely scoped discovery issues, overscanning and beans being replaced by multiple scanners

For Spring annotation discovery detail, see Annotation Reference for Spring Projects

Annotation Spring Framework Discovery (law) application Web Service

Published at DZone with permission of Gordon Dickens, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Apache NiFi Overview
  • Real-Time Stream Processing With Hazelcast and StreamNative
  • Building Angular Library and Publishing in npmjs Registry
  • How to Use MQTT in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: