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
@@ -7131,6 +7206,7 @@ method that returns `true` or `false`. For example, here is the actual
7131
7206
See the {api-spring-framework}/context/annotation/Conditional.html[
7132
7207
`@Conditional` javadocs] for more detail.
7133
7208
7209
+
7134
7210
[[beans-java-combining]]
7135
7211
==== Combining Java and XML configuration
7136
7212
@@ -7403,6 +7479,7 @@ certain profile of bean definitions in situation A, and a different profile in
7403
7479
situation B. Let's first see how we can update our configuration to reflect
7404
7480
this need.
7405
7481
7482
+
7406
7483
[[beans-definition-profiles-java]]
7407
7484
==== @Profile
7408
7485
@@ -7572,6 +7649,7 @@ The `spring-bean.xsd` has been constrained to allow such elements only as the
7572
7649
last ones in the file. This should help provide flexibility without incurring
7573
7650
clutter in the XML files.
7574
7651
7652
+
7575
7653
[[beans-definition-profiles-enable]]
7576
7654
==== Activating a profile
7577
7655
@@ -7618,6 +7696,7 @@ Declaratively, `spring.profiles.active` may accept a comma-separated list of pro
7618
7696
-Dspring.profiles.active="profile1,profile2"
7619
7697
----
7620
7698
7699
+
7621
7700
[[beans-definition-profiles-default]]
7622
7701
==== Default profile
7623
7702
@@ -7648,6 +7727,8 @@ profile is enabled, the _default_ profile will not apply.
7648
7727
The name of the default profile can be changed using `setDefaultProfiles()` on
7649
7728
the `Environment` or declaratively using the `spring.profiles.default` property.
7650
7729
7730
+
7731
+
7651
7732
[[beans-property-source-abstraction]]
7652
7733
=== PropertySource abstraction
7653
7734
@@ -7725,6 +7806,8 @@ any `foo` property in any other `PropertySource`. The
7725
7806
API exposes a number of methods that allow for precise manipulation of the set of
7726
7807
property sources.
7727
7808
7809
+
7810
+
7728
7811
=== @PropertySource
7729
7812
7730
7813
The {api-spring-framework}/context/annotation/PropertySource.html[`@PropertySource`]
@@ -7782,6 +7865,7 @@ as a default. If no default is specified and a property cannot be resolved, an
7782
7865
`IllegalArgumentException` will be thrown.
7783
7866
7784
7867
7868
+
7785
7869
=== Placeholder resolution in statements
7786
7870
7787
7871
Historically, the value of placeholders in elements could be resolved only against
@@ -7804,6 +7888,8 @@ property is defined, as long as it is available in the `Environment`:
7804
7888
----
7805
7889
7806
7890
7891
+
7892
+
7807
7893
[[context-load-time-weaver]]
7808
7894
== Registering a LoadTimeWeaver
7809
7895
0 commit comments