Skip to content

Commit

Permalink
Restore annotated code samples
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Aug 14, 2019
1 parent ec74055 commit 6b031b9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 56 deletions.
29 changes: 17 additions & 12 deletions src/docs/asciidoc/core/core-aop.adoc
Expand Up @@ -481,33 +481,38 @@ pointcut expressions by name. The following example shows three pointcut express
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
// matches if a method execution join point represents the execution of any public method
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}
private void anyPublicOperation() {} // <1>
// matches if a method execution is in the trading module
@Pointcut("within(com.xyz.someapp.trading..*)")
private void inTrading() {}
private void inTrading() {} // <2>
// matches if a method execution represents any public method in the trading module
@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}
private void tradingOperation() {} // <3>
----
<1> `anyPublicOperation` matches if a method execution join point represents the execution
of any public method.
<2> `inTrading` matches if a method execution is in the trading module.
<3> `tradingOperation` matches if a method execution represents any public method in the
trading module.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// matches if a method execution join point represents the execution of any public method.
@Pointcut("execution(public * *(..))")
private fun anyPublicOperation() {}
private fun anyPublicOperation() {} // <1>
// matches if a method execution is in the trading module
@Pointcut("within(com.xyz.someapp.trading..*)")
private fun inTrading() {}
private fun inTrading() {} // <2>
// matches if a method execution represents any public method in the trading module
@Pointcut("anyPublicOperation() && inTrading()")
private fun tradingOperation() {}
private fun tradingOperation() {} // <3>
----
<1> `anyPublicOperation` matches if a method execution join point represents the execution
of any public method.
<2> `inTrading` matches if a method execution is in the trading module.
<3> `tradingOperation` matches if a method execution represents any public method in the
trading module.

It is a best practice to build more complex pointcut expressions out of smaller named
components, as shown earlier. When referring to pointcuts by name, normal Java visibility
Expand Down
26 changes: 14 additions & 12 deletions src/docs/asciidoc/core/core-appendix.adoc
Expand Up @@ -860,14 +860,10 @@ we can parse our custom XML content, as you can see in the following example:
import java.text.SimpleDateFormat;
// We use the Spring-provided AbstractSingleBeanDefinitionParser to handle a lot of
// the basic grunt work of creating a single BeanDefinition.
public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
public class SimpleDateFormatBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { // <1>
protected Class getBeanClass(Element element) {
// We supply the AbstractSingleBeanDefinitionParser superclass with the type that our
// single BeanDefinition represents.
return SimpleDateFormat.class;
return SimpleDateFormat.class; // <2>
}
protected void doParse(Element element, BeanDefinitionBuilder bean) {
Expand All @@ -884,6 +880,11 @@ we can parse our custom XML content, as you can see in the following example:
}
----
<1> We use the Spring-provided `AbstractSingleBeanDefinitionParser` to handle a lot of
the basic grunt work of creating a single `BeanDefinition`.
<2> We supply the `AbstractSingleBeanDefinitionParser` superclass with the type that our
single `BeanDefinition` represents.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
Expand All @@ -896,13 +897,9 @@ we can parse our custom XML content, as you can see in the following example:
import java.text.SimpleDateFormat
// We use the Spring-provided AbstractSingleBeanDefinitionParser to handle a lot of
// the basic grunt work of creating a single BeanDefinition.
class SimpleDateFormatBeanDefinitionParser : AbstractSingleBeanDefinitionParser() {
class SimpleDateFormatBeanDefinitionParser : AbstractSingleBeanDefinitionParser() { // <1>
override fun getBeanClass(element: Element): Class<*>? {
// We supply the AbstractSingleBeanDefinitionParser superclass with the type that our
// single BeanDefinition represents.
override fun getBeanClass(element: Element): Class<*>? { // <2>
return SimpleDateFormat::class.java
}
Expand All @@ -919,6 +916,11 @@ we can parse our custom XML content, as you can see in the following example:
}
}
----
<1> We use the Spring-provided `AbstractSingleBeanDefinitionParser` to handle a lot of
the basic grunt work of creating a single `BeanDefinition`.
<2> We supply the `AbstractSingleBeanDefinitionParser` superclass with the type that our
single `BeanDefinition` represents.


In this simple case, this is all that we need to do. The creation of our single
`BeanDefinition` is handled by the `AbstractSingleBeanDefinitionParser` superclass, as
Expand Down
55 changes: 37 additions & 18 deletions src/docs/asciidoc/core/core-beans.adoc
Expand Up @@ -5599,24 +5599,27 @@ following example:
public class MovieRecommender {
@Autowired
@Offline
@Offline // <1>
private MovieCatalog offlineCatalog;
// ...
}
----
<1> This line adds the `@Offline` annotation.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class MovieRecommender {
@Autowired
@Offline
@Offline // <1>
private lateinit var offlineCatalog: MovieCatalog
// ...
}
----
<1> This line adds the `@Offline` annotation.

Now the bean definition only needs a qualifier `type`, as shown in the following example:

Expand Down Expand Up @@ -5915,21 +5918,24 @@ as demonstrated in the following example:
private MovieFinder movieFinder;
@Resource(name="myMovieFinder") // This line injects a @Resource
@Resource(name="myMovieFinder") // <1>
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
----
<1> This line injects a `@Resource`.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
class SimpleMovieLister {
@Resource(name="myMovieFinder") // This line injects a @Resource
@Resource(name="myMovieFinder") // <1>
private lateinit var movieFinder:MovieFinder
}
----
<1> This line injects a `@Resource`.


If no name is explicitly specified, the default name is derived from the field name or
Expand Down Expand Up @@ -5986,17 +5992,18 @@ named "customerPreferenceDao" and then falls back to a primary type match for th
@Resource
private CustomerPreferenceDao customerPreferenceDao;
// The context field is injected based on the known resolvable dependency
// type: ApplicationContext
@Resource
private ApplicationContext context;
private ApplicationContext context; // <1>
public MovieRecommender() {
}
// ...
}
----
<1> The `context` field is injected based on the known resolvable dependency type:
`ApplicationContext`.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
Expand All @@ -6005,14 +6012,15 @@ named "customerPreferenceDao" and then falls back to a primary type match for th
@Resource
private lateinit var customerPreferenceDao: CustomerPreferenceDao
// The context field is injected based on the known resolvable dependency
// type: ApplicationContext
@Resource
private lateinit var context: ApplicationContext
private lateinit var context: ApplicationContext // <1>
// ...
}
----
<1> The `context` field is injected based on the known resolvable dependency type:
`ApplicationContext`.

[[beans-value-annotations]]
=== Using `@Value`
Expand Down Expand Up @@ -6341,24 +6349,27 @@ is meta-annotated with `@Component`, as the following example shows:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // Causes @Service to be treated in the same way as @Component
@Component // <1>
public @interface Service {
// ...
}
----
<1> The `Component` causes `@Service` to be treated in the same way as `@Component`.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Target(AnnotationTarget.TYPE)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
@Component // Causes @Service to be treated in the same way as @Component
@Component // <1>
annotation class Service {
// ...
}
----
<1> The `Component` causes `@Service` to be treated in the same way as `@Component`.

You can also combine meta-annotations to create "`composed annotations`". For example,
the `@RestController` annotation from Spring MVC is composed of `@Controller` and
Expand Down Expand Up @@ -7779,20 +7790,23 @@ To enable component scanning, you can annotate your `@Configuration` class as fo
.Java
----
@Configuration
@ComponentScan(basePackages = "com.acme") // This annotation enables component scanning
@ComponentScan(basePackages = "com.acme") // <1>
public class AppConfig {
...
}
----
<1> This annotation enables component scanning.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Configuration
@ComponentScan(basePackages = ["com.acme"]) // This annotation enables component scanning
@ComponentScan(basePackages = ["com.acme"]) // <1>
class AppConfig {
// ...
}
----
<1> This annotation enables component scanning.


[TIP]
Expand Down Expand Up @@ -9655,7 +9669,7 @@ the following example shows:
public class AppConfig {
@Bean("dataSource")
@Profile("development") // The standaloneDataSource method is available only in the development profile
@Profile("development") // <1>
public DataSource standaloneDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
Expand All @@ -9665,21 +9679,24 @@ the following example shows:
}
@Bean("dataSource")
@Profile("production") // The jndiDataSource method is available only in the production profile
@Profile("production") // <2>
public DataSource jndiDataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}
}
----
<1> The `standaloneDataSource` method is available only in the `development` profile.
<2> The `jndiDataSource` method is available only in the `production` profile.

[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
@Configuration
class AppConfig {
@Bean("dataSource")
@Profile("development") // The standaloneDataSource method is available only in the development profile
@Profile("development") // <1>
fun standaloneDataSource(): DataSource {
return EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
Expand All @@ -9689,11 +9706,13 @@ the following example shows:
}
@Bean("dataSource")
@Profile("production") // The jndiDataSource method is available only in the production profile
@Profile("production") // <2>
fun jndiDataSource() =
InitialContext().lookup("java:comp/env/jdbc/datasource") as DataSource
}
----
<1> The `standaloneDataSource` method is available only in the `development` profile.
<2> The `jndiDataSource` method is available only in the `production` profile.

[NOTE]
====
Expand Down

0 comments on commit 6b031b9

Please sign in to comment.