concept service layer in category aspectj

This is an excerpt from Manning's book AspectJ in Action, Second Edition.
This limitation dictates the architecture utilized. For example, you can’t apply transaction management and security at the domain entities level, because domain entities typically aren’t Spring beans. In practice, this means you need to employ a service layer, expose the service objects as Spring beans, and apply crosscutting at that level. In chapter 16, we’ll examine the use of AspectJ-based crosscutting to overcome this limitation.
Listing 11.1. Enforcement of layered architecture
package ajia.architecture; public aspect LayeringViolation { declare error : SystemArchitecture.dataLayerCall() && !SystemArchitecture.inRepositoryLayer() : "Only the repository layer is allowed to access the data layer"; declare error : SystemArchitecture.repositoryLayerCall() && !SystemArchitecture.inRepositoryLayer() && !SystemArchitecture.inServiceLayer() : "Only the service layer is allowed to access the repository layer"; declare error : SystemArchitecture.serviceLayerCall() && !SystemArchitecture.inServiceLayer() && !SystemArchitecture.inWebLayer() : "Only the UI layer is allowed to access the service layer"; declare error : SystemArchitecture.webLayerCall() && !SystemArchitecture.inWebLayer() : "Only the UI layer is allowed to access itself"; }
As shown in the figure, the authentication system stores an Authentication object inside a SecurityContext. The SecurityContextHolder holds on to one SecurityContext object per scope, which may be a thread-local, an inheritable thread-local, or global. The thread-local scope is the default and most useful in web applications. The authenticated user may then be accessed by other parts of the system. For example, a service layer may access the principal authenticated by the web layer so that it can determine whether the user has appropriate privileges for the invoked services. You’ll use this fact to access the principal from security-related aspects.
Figure 15.3. Securing the service layer using proxy-based AOP and Spring Security. An aspect consists of a pointcut and an advice. The advice delegates to a support class, which then performs the same task as in figure 15.2.
![]()