MockCertMockCert
Menu

Test yourself in exam conditions

A full Spring Certified Professional mock exam: 60 questions in 130 minutes, scored against the real exam's pass mark. Your first one is free.

Start a free mock exam
Domain 1 · 6 topics · about 39 minTopics in this domainTopics

Domain 1 · Overview

Overview

Up next: Introduction to Spring Framework · 11 steps + check · 6 min

Domain 1 · Topic 1 of 6

Introduction to Spring Framework

Step 1 of 11

Think first What does the Spring IoC container do for your application, and what do AOP and data access support add to it?

Why it matters: Questions ask what the container, beans, ApplicationContext and Spring's core services each do in an application.

Spring's IoC container creates, configures and assembles your objects, called beans. With dependency injection, objects declare what they need and the container supplies it. Optional AOP adds crosscutting services, and data access support translates exceptions.

Step 2 of 11

Term Meaning
Bean An object that a Spring IoC container instantiates, assembles and manages.
Dependency injection Objects declare dependencies as constructor, factory method or property arguments, and the container injects them.
ApplicationContext The interface that represents the Spring IoC container and builds the beans.
Aspect The AOP unit of modularity; it packages a concern that cuts across many types.
DataAccessException The root of Spring's exception hierarchy for translated data access errors.

Step 3 of 11

ApplicationContext is the container that builds your beans. It instantiates, configures and assembles beans. Its foundation is two packages: org.springframework.beans and org.springframework.context. ApplicationContext is a full superset of BeanFactory, adding enterprise-specific features to its basic configuration framework.

Step 4 of 11

Objects declare dependencies; the container injects them. A dependency arrives through constructor arguments, factory method arguments or properties set after construction. The container injects them when it creates the bean. Your code should ideally never call getBean(), so it stays free of Spring APIs.

Exam trap Looking beans up with getBean() throughout your code is not the goal. Ideal application code has no getBean() calls and no dependency on Spring APIs.

Step 5 of 11

You describe beans with annotations, Java classes or XML. Metadata can take three forms: annotated component classes, configuration classes whose factory methods create beans, or external XML files and Groovy scripts. Java configuration uses @Bean methods inside a @Configuration class, one per bean definition. Fine-grained domain objects are usually left out, because repositories and business logic create them.

Step 6 of 11

Stand-alone apps create a context; Spring Boot does it for you. Stand-alone applications typically create an AnnotationConfigApplicationContext or a ClassPathXmlApplicationContext. In Spring Boot, the context is bootstrapped implicitly from common setup conventions. When you must, retrieve a bean by calling getBean() with its name and required type. The context also provides internationalization (i18n) support.

Step 7 of 11

By default, listeners receive events synchronously. publishEvent() blocks until every listener has finished with the event. You can still register as many listeners as you like.

Exam trap Publishing an event does not return at once by default. publishEvent() waits until all listeners have processed it.

Step 8 of 11

In AOP, the aspect is the unit of modularity, not the class. Aspects modularize concerns such as transaction management that cut across many types and objects. Declarative transaction management is the most important enterprise service built this way. You write aspects in a schema-based style or the @AspectJ style. The IoC container does not depend on AOP, so it is optional.

Step 9 of 11

Spring translates technology-specific exceptions for you. An SQLException becomes part of a hierarchy rooted at DataAccessException. JPA- and Hibernate-specific exceptions turn into focused runtime exceptions too. The translated exceptions wrap the original, so no error detail is lost.

Step 10 of 11

@Repository guarantees exception translation for DAOs. It is the best way to make sure DAOs and repositories translate exceptions. It also lets component scanning find and configure them without XML entries. Spring's DAO support lets you switch between JDBC, Hibernate and JPA fairly easily.

Exam trap @Repository is more than a marker for component scanning. It is also the best way to guarantee that your DAOs and repositories translate exceptions.

Step 11 of 11

  • A bean is any object that the Spring IoC container instantiates, assembles and manages.
  • ApplicationContext is the IoC container and a full superset of BeanFactory.
  • By default, event listeners run synchronously and publishEvent() blocks until they finish.
  • Use @Repository so DAOs get exception translation into DataAccessException.

Sources: Container Overview :: Spring Framework (opens in a new tab) · Introduction to the Spring IoC Container and Beans :: Spring Framework (opens in a new tab) · Aspect Oriented Programming with Spring :: Spring Framework (opens in a new tab) · DAO Support :: Spring Framework (opens in a new tab) · Additional Capabilities of the ApplicationContext :: Spring Framework (opens in a new tab)

Check

Check: Introduction to Spring Framework

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Up next: Java Configuration · 10 steps + check · 6 min

Domain 1 · Topic 2 of 6

Java Configuration

Step 1 of 10

Think first How do you define beans in Java code, wire them together and control how many instances Spring creates?

Why it matters: Questions show configuration classes and ask which beans exist, what they are named, how they connect and how long they live.

A @Configuration class declares beans with @Bean methods, and Spring registers each one in the application context. A CGLIB subclass makes calls between those methods return the container's beans. Beans are singletons by default, and @Scope changes that.

Step 2 of 10

Term Meaning
@Bean A method-level annotation that declares a bean; the direct Java analog of the XML bean element.
Lite mode @Bean methods outside a full @Configuration class; direct calls to them are not intercepted.
Bean scope Set on a @Bean method with @Scope; singleton is the default, and a singleton is per container and per bean.
CGLIB subclass The startup-time subclass Spring creates for each @Configuration class to intercept @Bean calls.
Prototype scope A non-singleton scope chosen with @Scope; Spring builds such a bean on request, not when the container starts.

Step 3 of 10

Each @Bean method declares one bean. By default the bean takes the method's name. You can put @Bean methods in a @Configuration class or a @Component class. Declare the most specific return type when the bean implements several interfaces.

Exam trap The bean is not named after its return type. By default it gets the name of the @Bean method.

Step 4 of 10

Pass configuration classes to AnnotationConfigApplicationContext. Spring registers the class itself and each of its @Bean methods as bean definitions. You can also use the no-arg constructor and then call register(). Stand-alone apps commonly use this context or ClassPathXmlApplicationContext.

Step 5 of 10

getBean() returns a bean by its name and required type. The ApplicationContext offers this method for retrieving bean instances. Ideally, your application code never calls getBean() at all, so it has no dependency on Spring APIs.

Step 6 of 10

@Import loads @Bean definitions from another configuration class. It plays the same role as the import element in Spring XML. Configuration classes are meta-annotated with @Component, so component scanning finds them too. Reader delegates let one context mix several configuration formats.

Step 7 of 10

Declare a dependency as a @Bean method parameter. A @Bean method can take any number of parameters. Prefer them over @Autowired fields in a configuration class, which is processed early and may force early initialization. Use constructors for mandatory dependencies and setters for optional ones.

Exam trap Two beans that need each other through constructor injection do not wire up. Spring detects the cycle at runtime and throws BeanCurrentlyInCreationException.

Step 8 of 10

Only full @Configuration classes intercept @Bean calls. Spring subclasses every @Configuration class with CGLIB, so calling another @Bean method returns the cached bean. For the same reason, a configuration class must not be final.

Full configuration mode compared with lite mode

Feature Full @Configuration Lite mode
Where it applies Classes annotated with @Configuration Other classes, or @Configuration(proxyBeanMethods=false)
Calling another @Bean method The container returns the cached bean A plain Java call that creates a new instance
Inter-bean dependencies by method call Supported Not supported in plain @Component classes

Exam trap In a @Component class, calling one @Bean method from another does not reuse the singleton. Each call creates a new instance.

Step 9 of 10

Singleton is the default scope. A singleton is per container and per bean. Change the scope on a @Bean method with @Scope. Spring supports 6 scopes, and 4 of them need a web-aware ApplicationContext.

Singleton and prototype beans compared

Feature Singleton Prototype
Default scope Yes No; set it with @Scope
When created With the container, when pre-instantiated (the default) Only when requested
Initialization callbacks Called Called

Exam trap Prototype beans do not get their configured destruction callbacks, even though their initialization callbacks run.

Step 10 of 10

Check

Check: Java Configuration

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Up next: Properties and Profiles · 11 steps + check · 6 min

Domain 1 · Topic 3 of 6

Properties and Profiles

Step 1 of 11

Think first How can one Spring application run with different settings in development and production without changing its code?

Why it matters: Questions ask which property value wins, which beans load under a given profile, or what a SpEL expression does.

Spring Boot reads properties from several places in a fixed order, and later ones override earlier ones. Profiles switch whole parts of the configuration on or off per environment. SpEL works on an object graph at runtime, and bean definitions can use it through #{ } expressions.

Step 2 of 11

Term Meaning
Profile A named part of the configuration that loads only when that profile is active.
spring.profiles.active The Environment property that lists which profiles are active.
SpEL Spring Expression Language: queries and changes an object graph at runtime, with method calls and string templating.
Evaluation context The setting a SpEL expression runs in; it controls which parts of the language are allowed.

Step 3 of 11

Inject, read from the Environment, or bind to an object. @Value puts one value straight into a bean. The Environment abstraction reads values in code. @ConfigurationProperties binds a group of values to a structured object.

Step 4 of 11

Later entries in Boot's fixed order override earlier ones. Command-line options such as --server.port=9000 become properties and beat file-based settings. If .properties and YAML files sit in the same location, .properties wins. Environment variables may use underscores, as in SPRING_CONFIG_NAME.

Exam trap Property files added by annotation join the Environment only when the context refreshes. That is too late for logging.* and spring.main.* settings.

Step 5 of 11

Mark a component or configuration class with @Profile. A profile limits part of the configuration to the environments that need it. Any @Component, @Configuration or @ConfigurationProperties class can carry @Profile. A profile group gives one logical name to several related profiles.

Step 6 of 11

Set spring.profiles.active, or fall back to default. With no active profile, a profile named default is enabled. Its name can be changed with spring.profiles.default.

Profile properties compared

Property What it does Worth knowing
spring.profiles.active Names the active profiles A command-line switch can replace the file value
spring.profiles.include Adds profiles Adds to the active ones instead of replacing them
spring.profiles.default Renames the fallback profile Used only when no profile is active

Step 7 of 11

@ConditionalOnProperty includes configuration based on a property. By default, it matches any property that exists and is not false. When the name attribute lists several properties, every one must pass. @ConditionalOnExpression uses a SpEL result instead.

Exam trap Referencing a bean inside a @ConditionalOnExpression expression initializes it very early. That bean then misses post-processing such as configuration properties binding.

Step 8 of 11

Wrap each expression in #{ } delimiters. Every bean in the context is available by its name, including environment, systemProperties and systemEnvironment. Whitespace between tokens is ignored unless it is inside a string literal.

Step 9 of 11

Choose the evaluation context by how far you trust the expression. Parsing can throw ParseException; getValue() can throw EvaluationException. By default an expression is capped at 10,000 characters and 10,000 operations.

SpEL evaluation contexts compared

Feature SimpleEvaluationContext StandardEvaluationContext
Language coverage A subset of SpEL syntax The complete language
Type references, constructors, bean references Excluded Available
Untrusted expressions Built for limited syntax Must never evaluate them

Step 10 of 11

The compiler is off by default, so expressions are interpreted. spring.expression.compiler.mode can be off, immediate or mixed. In immediate mode, a failing compiled expression throws to the caller. Expressions with assignment or using the conversion service cannot be compiled.

Step 11 of 11

  • Command-line properties override values from application files.
  • spring.profiles.include adds profiles; spring.profiles.active sets them.
  • With no active profile, Spring enables the profile named default.
  • Never evaluate untrusted expressions with StandardEvaluationContext.

Sources: Externalized Configuration :: Spring Boot (opens in a new tab) · Profiles :: Spring Boot (opens in a new tab) · Spring Expression Language (SpEL) :: Spring Framework (opens in a new tab) · Expressions in Bean Definitions :: Spring Framework (opens in a new tab) · Evaluation :: Spring Framework (opens in a new tab) · Creating Your Own Auto-configuration :: Spring Boot (opens in a new tab) · Language Reference :: Spring Framework (opens in a new tab)

Check

Check: Properties and Profiles

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Up next: Annotation-Based Configuration and Component Scanning · 10 steps + check · 6 min

Domain 1 · Topic 4 of 6

Annotation-Based Configuration and Component Scanning

Step 1 of 10

Think first How does Spring decide which annotated classes become beans, and when their lifecycle methods run?

Why it matters: Questions show annotated classes and ask which ones scanning detects, what each bean is called and when callbacks run.

Component scanning finds classes marked with a stereotype annotation and registers each one as a bean. With CommonAnnotationBeanPostProcessor registered, @PostConstruct and @PreDestroy methods then run at the matching lifecycle points.

Step 2 of 10

Term Meaning
Component scanning Searching a base package for stereotype-annotated classes and registering them as beans.
Stereotype annotation @Component or one of its role-specific forms: @Repository, @Service or @Controller.
CommonAnnotationBeanPostProcessor The processor that recognizes the JSR-250 lifecycle annotations @PostConstruct and @PreDestroy.
JSR-330 The standard Dependency Injection for Java annotations, such as @Inject and @Named.

Step 3 of 10

Add @ComponentScan to a @Configuration class. Set its basePackages attribute to a common parent package of your components. In XML, the component-scan element also switches on annotation config, so you rarely need both. An AnnotationConfigApplicationContext already registers the annotation post-processors for you.

Step 4 of 10

Only stereotype-annotated classes are detected by default. Default candidates carry @Component, @Repository, @Service, @Controller or @Configuration. A custom annotation also counts when it is itself annotated with @Component. Without an explicit name, the bean gets the uncapitalized simple class name.

Exam trap A plain class in the scanned package is not registered just because of where it sits. By default it needs a stereotype annotation.

Step 5 of 10

Pick the stereotype that matches the class's layer. @Repository, @Service and @Controller specialize @Component for the persistence, service and presentation layers. For a service class, @Service is the better choice than @Component.

Stereotype annotations compared by layer and extra role

Annotation Layer Extra role
@Component Any, generic Base stereotype; can mark custom annotations
@Repository Persistence (DAO) Enables automatic exception translation
@Service Service Preferred over @Component for service classes
@Controller Presentation Combined with @ResponseBody in @RestController

Step 6 of 10

@PostConstruct and @PreDestroy are JSR-250 callbacks. With CommonAnnotationBeanPostProcessor registered, they run at the same point as Spring's lifecycle interfaces or declared callback methods. Classes defined with @Bean can use them too. Since Jakarta EE 9, they live in jakarta.annotation.

Exam trap Since Jakarta EE 9, these annotations live in jakarta.annotation, not javax.annotation. The old package left the core JDK in JDK 9 and was removed in JDK 11.

Step 7 of 10

A public close or shutdown method runs at shutdown. Beans defined with Java configuration get this destruction callback by default when they have such a method. Add @Bean(destroyMethod = "") if the container should not call it.

Step 8 of 10

XML property values override annotation injection. Spring applies annotation injection before external property injection. So when both wire the same property, the XML value is the one that stays. Spring Boot favors Java configuration, ideally one main @Configuration class.

Exam trap Annotations do not beat XML. They are applied first, so an XML-specified property value replaces them.

Step 9 of 10

Spring supports @Inject and @Named from jakarta.inject. These are standard JSR-330 annotations. The table compares each one with the Spring annotation for the same job.

Spring annotations and their JSR-330 counterparts

Feature Spring JSR-330
Inject a dependency @Autowired @Inject, with no required attribute
Declare a component @Component, composable @Named, not composable
Inject a property value @Value No equivalent
Default scope in Spring Singleton Singleton, though JSR-330's own default is like prototype

Step 10 of 10

  • Scanning detects @Component, its stereotypes and custom annotations meta-annotated with @Component.
  • Use @Service for service classes and @Repository for DAOs, which adds exception translation.
  • XML property values override annotations, because annotation injection happens first.
  • @PostConstruct and @PreDestroy now come from the jakarta.annotation package.

Sources: Classpath Scanning and Managed Components :: Spring Framework (opens in a new tab) · Using @PostConstruct and @PreDestroy :: Spring Framework (opens in a new tab) · Annotation-based Container Configuration :: Spring Framework (opens in a new tab) · Using the @Bean Annotation :: Spring Framework (opens in a new tab) · Configuration Classes :: Spring Boot (opens in a new tab) · Using JSR-330 Standard Annotations :: Spring Framework (opens in a new tab)

Check

Check: Annotation-Based Configuration and Component Scanning

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Up next: Spring Bean Lifecycle · 11 steps + check · 7 min

Domain 1 · Topic 5 of 6

Spring Bean Lifecycle

Step 1 of 11

Think first How does Spring turn bean definitions into ready, possibly proxied objects, and where can your code hook into that process?

Why it matters: Questions describe a bean setup and ask which callback, post-processor or proxy acts, and in what order.

Spring first lets a BeanFactoryPostProcessor edit bean definitions, then creates the beans. Each bean gets its dependencies and then its init callbacks, with every BeanPostProcessor called before and after them. A post-processor may hand back a proxy that adds behavior.

Step 2 of 11

Term Meaning
BeanFactoryPostProcessor Reads and may change bean configuration metadata before ordinary beans are created.
BeanPostProcessor Gets a callback for each new bean, once before and once after its init callbacks.
Initialization callback A method Spring calls once a bean has all its dependencies, such as a @PostConstruct method.
CGLIB proxy The kind of proxy Spring AOP needs to proxy a class rather than an interface, used by default when there is no interface.
@Primary Marks the preferred bean when several beans match a single-valued injection point.

Step 3 of 11

A BeanFactoryPostProcessor edits metadata before beans exist. It reads bean definitions and can change them before the container creates any other bean. Spring ships ready-made ones, such as PropertySourcesPlaceholderConfigurer and PropertyOverrideConfigurer. Register your own from a static @Bean method. That avoids clashes with @Autowired, @Value and @PostConstruct processing in the configuration class.

Exam trap A BeanFactoryPostProcessor works on bean definitions, not on created bean objects. Code that inspects or wraps instances belongs in a BeanPostProcessor.

Step 4 of 11

A BeanPostProcessor runs before and after init callbacks. Spring calls it for every bean it creates. The first call comes before afterPropertiesSet() or a declared init method. The second comes after all init callbacks. It may check for callback interfaces or wrap the bean in a proxy.

The two post-processor types compared

Feature BeanFactoryPostProcessor BeanPostProcessor
Works on Bean configuration metadata Each bean instance the container creates
When it runs Before any other beans are instantiated Before and after each bean's init callbacks
Typical use Placeholder and property override configurers Checking callback interfaces or wrapping a bean in a proxy

Step 5 of 11

@PostConstruct runs first, then afterPropertiesSet(), then init(). This order applies when one bean has several mechanisms with different method names. Each callback runs right after the bean receives all its dependencies. afterPropertiesSet() comes from InitializingBean. In Java configuration, name a plain init method with the initMethod attribute of @Bean.

Exam trap Init callbacks run on the raw bean, not on its proxy. AOP interceptors are not yet applied when the init callback runs.

Step 6 of 11

Prefer @PostConstruct and @PreDestroy over Spring interfaces. They keep beans free of Spring-specific interfaces. They work when CommonAnnotationBeanPostProcessor is registered. DisposableBean instead gives a destroy() method. For @Bean methods, Spring infers a public close or shutdown method as the destroy method by default.

Exam trap The old javax.annotation package was split out in JDK 9 and removed in JDK 11. As of Jakarta EE 9, these annotations live in jakarta.annotation.

Step 7 of 11

Auto-proxying is itself a BeanPostProcessor. It wraps a bean in a proxy that applies the aspects. For a class with no interface, Spring AOP uses a CGLIB proxy by default. CGLIB is repackaged inside spring-core, so you add nothing to the classpath.

Exam trap A BeanPostProcessor, and any bean it references directly, cannot be auto-proxied. No aspects are woven into those beans.

Step 8 of 11

@Configuration classes are CGLIB subclasses at startup. The subclass checks the container for a cached bean before calling your @Bean method. So one @Bean method can call another and still receive the container's bean. That works only in @Configuration classes, not plain @Component classes. Such classes must not be final. With proxyBeanMethods = false, those calls are not intercepted, so use injection.

Step 9 of 11

@DependsOn makes named beans initialize first. The depends-on attribute has the same effect. Separate several bean names with commas, whitespace or semicolons. The dependent bean starts after its dependency and stops before it.

Exam trap The destruction ordering from depends-on covers singleton beans only. The initialization ordering is not limited that way.

Step 10 of 11

Use @Primary or @Qualifier when several beans share a type. @Primary marks the preferred candidate when a single bean must be injected. When exactly one candidate carries it, that bean wins. @Qualifier values narrow the type matches to one bean. Since Spring 6.2, @Fallback marks backup beans, so one remaining regular bean acts as primary.

Exam trap @Qualifier does not replace matching by type. It narrows the set of beans that already match the type down to one.

Step 11 of 11

  • A BeanFactoryPostProcessor changes bean definitions; a BeanPostProcessor works on each created bean.
  • With different method names, init order is @PostConstruct, then afterPropertiesSet(), then the custom init method.
  • For a class without an interface, Spring AOP uses a CGLIB proxy by default.
  • Declare a @Bean method that returns a BeanFactoryPostProcessor as static.
  • Use @Primary or @Qualifier when several beans match one type.

Sources: Container Extension Points :: Spring Framework (opens in a new tab) · Customizing the Nature of a Bean :: Spring Framework (opens in a new tab) · Using @PostConstruct and @PreDestroy :: Spring Framework (opens in a new tab) · AOP Proxies :: Spring Framework (opens in a new tab) · Fine-tuning Annotation-based Autowiring with @Primary or @Fallback :: Spring Framework (opens in a new tab) · Using the @Configuration annotation :: Spring Framework (opens in a new tab) · Using depends-on :: Spring Framework (opens in a new tab) · Fine-tuning Annotation-based Autowiring with Qualifiers :: Spring Framework (opens in a new tab)

Check

Check: Spring Bean Lifecycle

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Up next: Aspect Oriented Programming · 11 steps + check · 7 min

Domain 1 · Topic 6 of 6

Aspect Oriented Programming

Step 1 of 11

Think first How do you choose the right advice type and pointcut when you add a cross-cutting behavior with Spring AOP?

Why it matters: Questions describe a cross-cutting need and ask which advice type, pointcut or proxy behavior applies to it.

An aspect packages behavior that cuts across many services, such as retrying failed operations. Spring wraps advised beans in proxies and runs advice at method execution join points. Pick the least powerful advice that does the job.

Step 2 of 11

Term Meaning
Aspect A bean whose class carries @Aspect, which Spring uses to configure AOP; suited to a requirement that cuts across many services.
Advice The code an aspect runs at a matched join point: before it, after it or around it.
Join point A point where advice can run; in Spring AOP it is always a method execution on a Spring bean.
Pointcut An expression that selects join points, declared with @Pointcut on a method that returns void.
Auto-proxying Spring generating a proxy for an advised bean so it can intercept calls and run the advice.

Step 3 of 11

Aspects hold behavior that cuts across many services. Retrying idempotent operations that fail from concurrency problems is a classic example. Without an aspect, the retry logic would repeat in every service of the service layer. An aspect applies it transparently, so the client never sees the locking failure.

Step 4 of 11

Spring AOP is proxy-based and advises only method executions. When a bean is advised by an aspect, Spring generates a proxy that intercepts its method calls. Only method execution join points on Spring beans are supported. The proxy sits outside the object, so a call from inside the same target is not intercepted.

Exam trap A method that calls another method on the same object does not trigger advice on the inner call. That call never passes through the proxy.

Step 5 of 11

@Aspect marks the class, but scanning also needs @Component. With @AspectJ support on, Spring detects any bean whose class has @Aspect. The org.aspectj:aspectjweaver library, version 1.9 or later, must be on the classpath. AspectJ only parses and matches pointcuts; the runtime stays pure Spring AOP, with no AspectJ compiler or weaver.

Exam trap Putting @Aspect on a class is not enough for component scanning to find it. Add @Component or a qualifying stereotype annotation as well.

Step 6 of 11

Each advice type runs at a different moment of the call. The table compares them. @After is after finally advice and fires for every outcome, while @AfterReturning fires only on a successful return.

The five advice types compared

Advice Runs when Key point
@Before Before the join point runs Cannot change the return value; an exception stops the chain
@AfterReturning After a normal, successful return only Sees the return value but cannot modify it
@AfterThrowing When the method exits by throwing an exception Does not run on a normal return
@After After any outcome, like a finally block Runs for a normal return and for an exception
@Around Around the call, given a ProceedingJoinPoint Returns Object and can call proceed more than once

Exam trap Choosing @After when you want only successful returns is a mistake. It also runs when an exception is thrown, so use @AfterReturning instead.

Step 7 of 11

Pick the weakest advice that meets the need. Do not use around advice when before advice is enough. Before advice never calls proceed(), so it cannot forget to continue down the interceptor chain. Around advice fits when you must call proceed several times, as retry logic does.

Step 8 of 11

Parameter patterns set how many arguments a method may take. In an execution expression, () matches no parameters, (..) matches zero or more, and (*) matches exactly one of any type. Declare a reusable pointcut with @Pointcut on a method returning void. The extra bean designator limits matching to named Spring beans, with wildcards for a set.

Exam trap AspectJ designators such as call, get, set or cflow are not supported. Using them in a Spring AOP pointcut throws an IllegalArgumentException.

Step 9 of 11

Within one aspect, @Around has the highest precedence. For advice in the same @Aspect at the same join point, the order is @Around, @Before, @After, @AfterReturning, then @AfterThrowing. An aspect can also implement the Ordered interface. A retry aspect does this to rank above transaction advice, so each retry gets a fresh transaction.

Step 10 of 11

Spring's advice interfaces mirror the same ideas. Around advice classes implement the AOP Alliance MethodInterceptor interface. ThrowsAdvice is a marker interface with no methods of its own. If a throws-advice method itself throws, its exception replaces the original one the user sees.

Step 11 of 11

  • Spring AOP advises only method executions on Spring beans, through proxies.
  • Calls from inside the target object bypass the proxy and its advice.
  • Use @After for every outcome and @AfterReturning for successful returns only.
  • Choose around advice only when you need control over proceed, such as for retries.

Sources: An AOP Example :: Spring Framework (opens in a new tab) · Enabling @AspectJ Support :: Spring Framework (opens in a new tab) · Declaring a Pointcut :: Spring Framework (opens in a new tab) · Declaring Advice :: Spring Framework (opens in a new tab) · Declaring an Aspect :: Spring Framework (opens in a new tab) · Advice API in Spring :: Spring Framework (opens in a new tab) · @AspectJ support :: Spring Framework (opens in a new tab)

Check

Check: Aspect Oriented Programming

Cards to recall, then questions on this topic. About 2 minutes. Nothing is saved without an account.

Loading the cards and questions…

Domain 1 · after the last topic

Domain 1 review

Each topic's Check tested it on its own; this mixes them.

Mixed quiz

Take the Domain 1 quiz

19 questions mixed from every topic in this domain, each answer explained. Mixing topics is harder than one at a time, and it is what the exam does. No account needed; with a free account your answers also build your estimated exam score.

Flashcards

Domain 1 flashcards

11 cards with this domain's key facts. Flip each one and check what you knew. No account needed.

Test yourself in exam conditions

A full Spring Certified Professional mock exam: 60 questions in 130 minutes, scored against the real exam's pass mark. Your first one is free.

Start a free mock exam
Next domain · 16% of topicsData Management3 topics

Report a problem

On “”. Tell us what looks wrong; we read every report.

What is the problem?

Optional.