A new pointcut in Spring 2.5 : bean() pointcut

Spring framework 2.5 introducing a new pointcut method to its users. Its called the bean pointcut. This will help us to weave Aspects in to the beans. According to the bean name(s) which we had specified or according to the pattern which we mentioned,  we can weave the aspects into that points.

Besides selecting a specific bean, this pointcut designator offers two interesting ways to select beans if you follow an appropriate naming convention:

  1. Selecting a vertical slice of beans: If you follow a convention where bean names include a string indicating their role from the business perspective, a bean() pointcut can select beans based on their business role. For example, you may use the bean(account*) pointcut to select all accounting-related beans such as accountRepository, accountService, and accountController if bean names start with a string representing their business functionality.
  2. Selecting a horizontal slice of beans: If you follow a convention where bean names include a string indicating their role from the architectural perspective, a bean() pointcut can select beans based on their architectural role. For example, you can use bean(*Repository) to select all repository beans if bean names end with a string representing their architectural role. Without the bean() pointcut, you had to rely on the package structure or type-based pointcuts, which can be sometimes a bit too restrictive.

These are the different ways or patterns of weaving this pointcut…

Pointcut

Join points selected in

bean(accountRepository)

The bean named “accountRepository”

!bean(accountRepository)

Any bean except the “accountRepository” bean

bean(*)

Any bean

bean(account*)

Any bean with name starting in “account”

bean(*Repository)

Any bean with name ending in “Repository”

bean(accounting/showaccount)

The bean named accounting/showaccount (designating, say, a controller handling that URL)

bean(accounting/*)

Any bean whose name starts with “accounting/” (designating, say, any controller handling accounting-related URLs)

bean(accounting/*/edit)

Any bean whose name starts with “accounting/” and ends with “/edit” (designating, say, any controller handling the edit operation functionality related to accounting)

bean(*dataSource) || bean(*DataSource)

Any bean whose name ends with either “dataSource” or “DataSource”

bean(service:name=monitoring)

The bean named “service:name=monitoring”

For more read it from Spring Team blog.

Technorati Tags: ,
Advertisements

One Reply to “A new pointcut in Spring 2.5 : bean() pointcut”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s