4
4
5
5
[[beans-introduction]]
6
6
== Introduction to the Spring IoC container and beans
7
+
7
8
This chapter covers the Spring Framework implementation of the Inversion of Control
8
9
(IoC) footnote:[See pass:specialcharacters,macros[<<background-ioc>>] ] principle. IoC
9
10
is also known as __dependency injection__ (DI). It is a process whereby objects define
@@ -44,6 +45,7 @@ among them, are reflected in the __configuration metadata__ used by a container.
44
45
45
46
[[beans-basics]]
46
47
== Container overview
48
+
47
49
The interface `org.springframework.context.ApplicationContext` represents the Spring IoC
48
50
container and is responsible for instantiating, configuring, and assembling the
49
51
aforementioned beans. The container gets its instructions on what objects to
@@ -290,6 +292,44 @@ locations, for example, through "${...}" placeholders that are resolved against
290
292
system properties at runtime.
291
293
====
292
294
295
+ The import directive is a feature provided by the beans namespace itself. Further
296
+ configuration features beyond plain bean definitions are available in a selection
297
+ of XML namespaces provided by Spring, e.g. the "context" and the "util" namespace.
298
+
299
+
300
+ [[groovy-bean-definition-dsl]]
301
+ ==== The Groovy Bean Definition DSL
302
+
303
+ As a further example for externalized configuration metadata, bean definitions can also
304
+ be expressed in Spring's Groovy Bean Definition DSL, as known from the Grails framework.
305
+ Typically, such configuration will live in a ".groovy" file with a structure as follows:
306
+
307
+ [source,java,indent=0]
308
+ [subs="verbatim,quotes"]
309
+ ----
310
+ beans {
311
+ dataSource(BasicDataSource) {
312
+ driverClassName = "org.hsqldb.jdbcDriver"
313
+ url = "jdbc:hsqldb:mem:grailsDB"
314
+ username = "sa"
315
+ password = ""
316
+ settings = [mynew:"setting"]
317
+ }
318
+ sessionFactory(SessionFactory) {
319
+ dataSource = dataSource
320
+ }
321
+ myService(MyService) {
322
+ nestedBean = { AnotherBean bean ->
323
+ dataSource = dataSource
324
+ }
325
+ }
326
+ }
327
+ ----
328
+
329
+ This configuration style is largely equivalent to XML bean definitions and even
330
+ supports Spring's XML configuration namespaces. It also allows for importing XML
331
+ bean definition files through an "importBeans" directive.
332
+
293
333
294
334
295
335
[[beans-factory-client]]
@@ -305,8 +345,7 @@ The `ApplicationContext` enables you to read bean definitions and access them as
305
345
[subs="verbatim,quotes"]
306
346
----
307
347
// create and configure beans
308
- ApplicationContext context =
309
- new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
348
+ ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
310
349
311
350
// retrieve configured instance
312
351
PetStoreService service = context.getBean("petStore", PetStoreService.class);
@@ -315,12 +354,46 @@ The `ApplicationContext` enables you to read bean definitions and access them as
315
354
List<String> userList = service.getUsernameList();
316
355
----
317
356
318
- You use `getBean()` to retrieve instances of your beans. The `ApplicationContext`
357
+ With Groovy configuration, bootstrapping looks very similar, just a different context
358
+ implementation class which is Groovy-aware (but also understands XML bean definitions):
359
+
360
+ [source,java,indent=0]
361
+ [subs="verbatim,quotes"]
362
+ ----
363
+ ApplicationContext context = new GenericGroovyApplicationContext("services.groovy", "daos.groovy");
364
+ ----
365
+
366
+ The most flexible variant is `GenericApplicationContext` in combination with reader
367
+ delegates, e.g. with `XmlBeanDefinitionReader` for XML files:
368
+
369
+ [source,java,indent=0]
370
+ [subs="verbatim,quotes"]
371
+ ----
372
+ GenericApplicationContext context = new GenericApplicationContext();
373
+ new XmlBeanDefinitionReader(ctx).loadBeanDefinitions("services.xml", "daos.xml");
374
+ context.refresh();
375
+ ----
376
+
377
+ Or with `GroovyBeanDefinitionReader` for Groovy files:
378
+
379
+ [source,java,indent=0]
380
+ [subs="verbatim,quotes"]
381
+ ----
382
+ GenericApplicationContext context = new GenericApplicationContext();
383
+ new GroovyBeanDefinitionReader(ctx).loadBeanDefinitions("services.groovy", "daos.groovy");
384
+ context.refresh();
385
+ ----
386
+
387
+ Such reader delegates can be mixed and matched on the same `ApplicationContext`,
388
+ reading bean definitions from diverse configuration sources, if desired.
389
+
390
+ You can then use `getBean` to retrieve instances of your beans. The `ApplicationContext`
319
391
interface has a few other methods for retrieving beans, but ideally your application
320
392
code should never use them. Indeed, your application code should have no calls to the
321
393
`getBean()` method at all, and thus no dependency on Spring APIs at all. For example,
322
- Spring's integration with web frameworks provides for dependency injection for various
323
- web framework classes such as controllers and JSF-managed beans.
394
+ Spring's integration with web frameworks provides dependency injection for various web
395
+ framework components such as controllers and JSF-managed beans, allowing you to declare
396
+ a dependency on a specific bean through metadata (e.g. an autowiring annotation).
324
397
325
398
326
399
@@ -497,6 +570,8 @@ If you are using Java-configuration, the `@Bean` annotation can be used to provi
497
570
see <<beans-java-bean-annotation>> for details.
498
571
****
499
572
573
+
574
+
500
575
[[beans-factory-class]]
501
576
=== Instantiating beans
502
577
@@ -7100,6 +7175,7 @@ method that returns `true` or `false`. For example, here is the actual
7100
7175
See the {api-spring-framework}/context/annotation/Conditional.html[
7101
7176
`@Conditional` javadocs] for more detail.
7102
7177
7178
+
7103
7179
[[beans-java-combining]]
7104
7180
==== Combining Java and XML configuration
7105
7181
@@ -7372,6 +7448,7 @@ certain profile of bean definitions in situation A, and a different profile in
7372
7448
situation B. Let's first see how we can update our configuration to reflect
7373
7449
this need.
7374
7450
7451
+
7375
7452
[[beans-definition-profiles-java]]
7376
7453
==== @Profile
7377
7454
@@ -7541,6 +7618,7 @@ The `spring-bean.xsd` has been constrained to allow such elements only as the
7541
7618
last ones in the file. This should help provide flexibility without incurring
7542
7619
clutter in the XML files.
7543
7620
7621
+
7544
7622
[[beans-definition-profiles-enable]]
7545
7623
==== Activating a profile
7546
7624
@@ -7587,6 +7665,7 @@ Declaratively, `spring.profiles.active` may accept a comma-separated list of pro
7587
7665
-Dspring.profiles.active="profile1,profile2"
7588
7666
----
7589
7667
7668
+
7590
7669
[[beans-definition-profiles-default]]
7591
7670
==== Default profile
7592
7671
@@ -7617,6 +7696,8 @@ profile is enabled, the _default_ profile will not apply.
7617
7696
The name of the default profile can be changed using `setDefaultProfiles()` on
7618
7697
the `Environment` or declaratively using the `spring.profiles.default` property.
7619
7698
7699
+
7700
+
7620
7701
[[beans-property-source-abstraction]]
7621
7702
=== PropertySource abstraction
7622
7703
@@ -7692,6 +7773,8 @@ any `foo` property in any other `PropertySource`. The
7692
7773
API exposes a number of methods that allow for precise manipulation of the set of
7693
7774
property sources.
7694
7775
7776
+
7777
+
7695
7778
=== @PropertySource
7696
7779
7697
7780
The {api-spring-framework}/context/annotation/PropertySource.html[`@PropertySource`]
@@ -7749,6 +7832,7 @@ as a default. If no default is specified and a property cannot be resolved, an
7749
7832
`IllegalArgumentException` will be thrown.
7750
7833
7751
7834
7835
+
7752
7836
=== Placeholder resolution in statements
7753
7837
7754
7838
Historically, the value of placeholders in elements could be resolved only against
@@ -7771,6 +7855,8 @@ property is defined, as long as it is available in the `Environment`:
7771
7855
----
7772
7856
7773
7857
7858
+
7859
+
7774
7860
[[context-load-time-weaver]]
7775
7861
== Registering a LoadTimeWeaver
7776
7862
0 commit comments