Skip to main content
Chapter 9 of 18 · 4 sections

Spring: ApplicationContext Configuration

Bootstrap an application context and compare annotation-based, Java, and XML bean definitions.

Original chapter: ApplicationContext Configuration. The text and examples below are preserved from the historical source; read the corrections above before using them.

Section 9.1: Autowiring

Autowiring is done using a sterotype annotation to specify what classes are going to be beans in the ApplicationContext, and using the Autowired and Value annotations to specify bean dependencies. The unique part of autowiring is that there is no external ApplicationContext definition, as it is all done within the classes that are the beans themselves.

Source example · PDF page 40
@Component // The annotation that specifies to include this as a bean
           // in the ApplicationContext
class Book {

   @Autowired // The annotation that wires the below defined Author
               // instance into this bean
   Author author;

   String title = "It";

   Author getAuthor() { return author; }
   String getTitle() { return title; }
}

@Component // The annotation that specifies to include
           // this as a bean in the ApplicationContext
class Author {
   String firstName = "Steven";
   String lastName = "King";

   String getFirstName() { return firstName; }
   String getLastName() { return lastName; }
}

Section 9.2: Bootstrapping the ApplicationContext

Java Config

The configuration class needs only to be a class that is on the classpath of your application and visible to your applications main class.

Source example · PDF page 40
class MyApp {
   public static void main(String[] args) throws Exception {
       AnnotationConfigApplicationContext appContext =
           new AnnotationConfigApplicationContext(MyConfig.class);

        // ready to retrieve beans from appContext, such as myObject.
   }
}

@Configuration
class MyConfig {
   @Bean
   MyObject myObject() {
        // ...configure myObject...
   }
    // ...define more beans...
}

Xml Config

The configuration xml file needs only be on the classpath of your application.

Source example · PDF page 41
class MyApp {
   public static void main(String[] args) throws Exception {
       ClassPathXmlApplicationContext appContext =
           new ClassPathXmlApplicationContext("applicationContext.xml");

        // ready to retrieve beans from appContext, such as myObject.
   }
}

<?xml version="1.0" encoding="UTF-8"?>
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd">
   <bean id="myObject" class="com.example.MyObject">
       <!-- ...configure myObject... -->
   </bean>

   <!-- ...define more beans... -->
</beans>

Autowiring

Autowiring needs to know which base packages to scan for annotated beans (@Component). This is specified via the #scan(String...) method.

Source example · PDF page 41
class MyApp {
   public static void main(String[] args) throws Exception {
       AnnotationConfigApplicationContext appContext =
           new AnnotationConfigApplicationContext();
       appContext.scan("com.example");
       appContext.refresh();

        // ready to retrieve beans from appContext, such as myObject.
   }
}

// assume this class is in the com.example package.
@Component
class MyObject {
    // ...myObject definition...
}

Section 9.3: Java Configuration

Java configuration is typically done by applying the @Configuration annotation to a class to suggest that a class contains bean definitions. The bean definitions are specified by applying the @Bean annotation to a method that returns an object.

Source example · PDF page 41
@Configuration // This annotation tells the ApplicationContext that this class
               // contains bean definitions.
class AppConfig {
    /**
    * An Author created with the default constructor
    * setting no properties
    */
   @Bean // This annotation marks a method that defines a bean
   Author author1() {
       return new Author();
   }

    /**
    * An Author created with the constructor that initializes the
    * name fields
    */
   @Bean
   Author author2() {
       return new Author("Steven", "King");
   }

    /**
    * An Author created with the default constructor, but
    * then uses the property setters to specify name fields
    */
   @Bean
   Author author3() {
       Author author = new Author();
       author.setFirstName("George");
       author.setLastName("Martin");
       return author;
   }

    /**
    * A Book created referring to author2 (created above) via
    * a constructor argument.  The dependency is fulfilled by
    * invoking the method as plain Java.
    */
   @Bean
   Book book1() {
       return new Book(author2(), "It");
   }

    /**
    * A Book created referring to author3 (created above) via
    * a property setter.  The dependency is fulfilled by
    * invoking the method as plain Java.
    */
   @Bean
   Book book2() {
       Book book = new Book();
       book.setAuthor(author3());
       book.setTitle("A Game of Thrones");
       return book;
   }
}

// The classes that are being initialized and wired above...
class Book { // assume package org.springframework.example
   Author author;
   String title;
   Book() {} // default constructor
   Book(Author author, String title) {
       this.author = author;
       this.title= title;
   }

   Author getAuthor() { return author; }
   String getTitle() { return title; }

   void setAuthor(Author author) {
       this.author = author;
   }

   void setTitle(String title) {
       this.title= title;
   }
}

class Author { // assume package org.springframework.example
   String firstName;
   String lastName;

   Author() {} // default constructor
   Author(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }

   String getFirstName() { return firstName; }
   String getLastName() { return lastName; }

   void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   void setLastName(String lastName) {
       this.lastName = lastName;
   }
}

Section 9.4: Xml Configuration

Xml configuration is typically done by defining beans within an xml file, using Spring's specific beans schema. Under the root beans element, typical bean definition would be done using the bean subelement.

Source example · PDF page 43
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- An Author created with the default constructor
       setting no properties -->
   <bean id="author1" class="org.springframework.example.Author" />

    <!-- An Author created with the constructor that initializes the
       name fields -->
   <bean id="author2" class="org.springframework.example.Author">
       <constructor-arg index="0" value="Steven" />
       <constructor-arg index="1" value="King" />
   </bean>
    <!-- An Author created with the default constructor, but
       then uses the property setters to specify name fields -->
   <bean id="author3" class="org.springframework.example.Author">
       <property name="firstName" value="George" />
       <property name="lastName" value="Martin" />
   </bean>

    <!-- A Book created referring to author2 (created above) via
       a constructor argument -->
   <bean id="book1" class="org.springframework.example.Book">
       <constructor-arg index="0" ref="author2" />
       <constructor-arg index="1" value="It" />
   </bean>

    <!-- A Book created referring to author3 (created above) via
       a property setter -->
   <bean id="book1" class="org.springframework.example.Book">
       <property name="author" ref="author3" />
       <property name="title" value="A Game of Thrones" />
   </bean>
</beans>

// The classes that are being initialized and wired above...
class Book { // assume package org.springframework.example
   Author author;
   String title;

   Book() {} // default constructor
   Book(Author author, String title) {
       this.author = author;
       this.title= title;
   }

   Author getAuthor() { return author; }
   String getTitle() { return title; }

   void setAuthor(Author author) {
       this.author = author;
   }

   void setTitle(String title) {
       this.title= title;
   }
}

class Author { // assume package org.springframework.example
   String firstName;
   String lastName;

   Author() {} // default constructor
   Author(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }

   String getFirstName() { return firstName; }
   String getLastName() { return lastName; }

   void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   void setLastName(String lastName) {
       this.lastName = lastName;
   }
}
Buy me a coffee
Daily Practice
0/0