March 7, 2015

Spring Fundamentals

Spring overview

  • Framework to reduce complexity around EJB
  • Allows enterprise development without Application Server
  • POJO based and Interface driven
  • Lightweight and unobtrusive compared to older J2EE methodoligies
  • Uses AOP and Proxies to remove Cross-Cutting Concerns
  • Increases Scalability, Testabiliy and Maintainability
  • Puts focus on the Business
  • Can be seen as a glorified HashMap
  • Can be used as a Registry

Project setup

  • Add libraries to build path
  • Define mappings in applicationContext.xml
  • ApplicationContext can be instantiated in main() using ClassPathXmlApplicationContext()

XML Configuration


  • Can be defined in applicationContext
  • Needs ID or Name
    • Id has to be valid xml identifier
    • Name can contain special chars
  • Default no-args constructor
    • Setter injection uses properties on beans
    • Construction injection uses constructor-arg element
  • Regular class with getters and setters


  • Spring wires beans together instead of mapping in xml
  • Can be wired by:
    • Type: bean of property type must be unique in the container
    • Name: wire by property name
    • Constructor: same as type but for constructors
    • None
  • Can be configured in xml or class


  • Uses component scanner from the context namespace
  • 3 main types of annotations (stereotype annotations)
    • @Service and @Repository extend @Component but do not add features
    • @Component used for components/beans, any POJO
    • @Service, service tier where business logic is contained
    • @Repository, data access tier (dao’s)
  • Can perform Autowiring on member variables, constructor and setters


  • Light weight dependency injection specification for Java
  • Simple annotations
  • Spring offers more features

Java Configuration

  • No need to configure with xml, we can use Java annotations
  • Not all spring releases are fully supported
  • @Configuration annotation replace applicationContext at class level
  • Methods used in conjunction with @Bean are used to get instances of beans
  • No applicationContext.xml needed, we use an app config class instead
  • Beans can be fetched from the context

Setter injection

  • Setter injection is as simple as a method call (mystery of injection goes away)
  • Call the setter

Cosntructor injection

  • Same as setter injection but uses constructor instead
  • Requires constructor in implementation class (CustomerServiceImpl here)


  • Add @ComponentScan annotation on the app config class
  • Just like xml configuration we mark what we want with @AutoWired (byName uses bean name byType uses instance type)
  • Possible to mix configurations with injection and autowired


  • Classes and instances can have different scope
  • Defines with @Scope in java or xml bean attribtue
  • Singleton is default
  • Prototype gives new instance for each request from bean
  • Web scopes (Request, Session, GlobalSession)covered in Spring MVC

Property files

  • Good to keep environmental information out of app
  • Context Namespace has utilities to help load files

XML configuration

Java configuration

  • Use @PropertySource(“”) annotation in AppConfig
  • To use in POJO, bean configuration is needed
  • Much cleaner in xml