concept annotation in category spring

appears as: nnotations, notation, The notation, nnotation, n annotation, notations, Annotations, The notations
Spring Security in Action

This is an excerpt from Manning's book Spring Security in Action.

Technically applying security with Spring Security in Spring applications is very simple. You already implement Spring applications, so you know that the framework’s philosophy starts with the management of the Spring context. You define beans in the Spring context to allow the framework to manage them based on configurations you specify. And let me refer only to using annotations to make these configurations and leave behind the old-fashioned XML configuration style!

So you use annotations to instruct Spring what to do: expose endpoints, wrap methods in transactions, intercept methods in aspects, and so on. The same is true with Spring Security configurations. This is where Spring Security comes into action. So what you want is to use, as comfortable already, annotations, beans, and in general Spring-fashioned configuration style to define your application-level security. If you think of a Spring application, the behavior that you need to protect is defined by methods.

Similar to the @PreAuthorize and @PostAuthorize annotations we discussed in chapter 16, you set the authorization rule as the value of the @PreFilter annotation. In these rules, which you provide as a SpEL expression, you use filterObject to refer to any element inside the collection or array you provide as a parameter to the method.

Figure 20.5 The difference between using annotations and a RequestPostProcessor to create the test security environment: When using annotations, first, the framework sets up the test security environment. When using a RequestPostProcessor, the test request is created and then changed to define other constraints, like the test security environment. In the figure, I shaded differently the point where the framework applies the test security environment.
Spring in Practice

This is an excerpt from Manning's book Spring in Practice.

The Contact class shows how JPA annotations work. There are many such annotations in the javax.persistence package (many more than we’re using here), and you use them to perform the mapping. You import the annotations you’re using at .

This recipe extends the infrastructure you’ve already created to add support for annotation-based configuration. Annotations are popular in both Java in general and Spring in particular, and many framework users have come to expect support for annotation-based configuration.

Design-wise, there are different approaches you might take. Spring’s transaction support, for instance, uses annotations to specify transaction attributes such as isolation and rollback behavior. For the circuit breaker, the annotation will target methods (not types), and it will reference the associated breaker by name. The intent is that each separate resource has its own circuit breaker, and each method accessing that resource will use that breaker.

Creating the annotation

You create an annotation called @GuardedBy as follows.

Listing 14.27. GuardedBy.java for annotation-based configuration
package com.springinpractice.ch14.kite; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface GuardedBy { String[] value() default ""; }

The annotation takes a single value, which is the name of the guard list guarding the annotated method.

Next you’ll create a new GuardListSource implementation to source guards from annotations, along with a corresponding pointcut and advisor.

Spring Batch in Action

This is an excerpt from Manning's book Spring Batch in Action.

Listing 3.16. Listening to job execution with annotations
public class AnnotatedImportProductsJobListener { @BeforeJob public void executeBeforeJob(JobExecution jobExecution) { //Notifying when job starts } @AfterJob public void executeAfterJob(JobExecution jobExecution) { if (jobExecution.getStatus()==BatchStatus.COMPLETED) { //Notifying when job successfully ends } else if (jobExecution.getStatus()==BatchStatus.FAILED) { //Notifying when job ends with failure } } }

An XML configuration has many advantages: it doesn’t affect your Java code—making it easier to reuse—and it’s flexible because you can externalize part of your configuration in a property file, using a Spring property placeholder. This allows switching the scheduling configuration between the development and production environments, for example. The XML configuration is external to your code: when you look at your Java code, you have no idea a scheduler launches it periodically. If you change the name of the Java method to launch periodically, you need to reflect this change in the XML configuration. If you want the scheduling configuration to be closer to your code than in a separate XML file, then annotations are the way to go.