Skip to content

Spring container

  • Generally known as as Application Context (Dispatcher Servlet on Spring MVC projects)
  • Primary functions
  • Create and manage objects (IoC)
  • Inject object's dependencies (Dependency injection)
  • Inversion of Control (IoC): The approach of outsourcing the construction and management of objects

Configuring Spring Container

  • XML configuration file (legacy)
  • Java Annotations (modern)
  • Java Source Code (modern)

XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Define your beans here -->
    <bean id="myCoach"
          class="com.hvitoi.coach.BaseballCoach">
    </bean>

</beans>

Annotations

<beans>
    <!-- enable component scanning -->
    <context:component-scan base-package="com.hvitoi" />
</beans>
  • Annotation to make the class a spring bean, so that spring will register it. In quote is the bean ID
  • If no ID is specified the name is auto generated with the class name with lowercase firstletter
  • Component class defines a bean. Controller and Repository are also annotations for a Component
@Component("thatSillyCoach")
// @Component // autogenerated id
public class TennisCoach implements Coach{
    @Override
    public String getDailyWorkout() {
        return "Practice your backhand volley";
    }
}
public class AppAnnotationConfig {
    public static void main(String[] args) {
        // read spring config
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // get bean from spring container
        Coach theCoach = context.getBean("thatSillyCoach", Coach.class);

        // call method
        System.out.println(theCoach.getDailyWorkout());

        // close context
        context.close();
    }
}

Java source code

  • Create java class annotated with @Configuration
  • This way, a config class will be used instead of a config xml
  • Add component scanning support with @ComponentScan (only for components created with annotations)

  • Automatically creating beans with component scan

@Configuration
@ComponentScan("com.hvitoi")
public class SportConfig {}
  • Manually creating beans with config class
  • Manually @Bean creation is good for situation where the class is not implemented by you (your project)
  • When the class is external and cannot be edited, the bean must be specified manually in the config class/xml
  • You want to make the third-party classes available to your Spring framework application context
@Configuration
public class SportConfig {
    @Bean
    public FortuneService sadFortuneService() {
        return new SadFortuneService();
    }

    @Bean
    public Coach swimCoach() {
        return new SwimCoach(sadFortuneService());
    }
    // Bean ID: swimCoach
    //

}