dependency injection and - universitetet i oslo ?· dependency injection with springdependency...

Download Dependency Injection and - Universitetet i Oslo ?· Dependency Injection with SpringDependency Injection…

Post on 04-Jun-2018

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Dependency Injectionp y j

    and

    SpringSpring

  • Problem areaProblem area

    Large information systems contains a huge number of classes that work togetherH t i th l t th ? How to wire the classes together?

  • Example: The StudentSystemExample: The StudentSystem

    To improve your skills in Java development, you decide to develop a student systemY d id t fil t t t d t i f ti You decide to use a file to store student information

    You create a class FileStudentDAO responsible for writing and reading to the filewriting and reading to the file

    You create a class StudentSystem responsible for performing the logic of the systemperforming the logic of the system

    Youve learned that its a good thing to program to interfacesinterfaces

  • The StudentSystemResponsible for The StudentSystemperforming useful operations on

    students

    >yer

    StudentSystem

    - int getNrOfStudents( String subjectCode )

    DefaultStudentSystem

    Ser

    vice

    lay

    >St d tDAO

    depends

    e la

    yer

    StudentDAO

    - Collection getAllStudents()

    FileStudentDAOimplements

    Per

    sist

    ence

    Responsible for adding,deleting and gettingstudents from some

    storage medium A file

  • The DefaultStudentSystemThe DefaultStudentSystempublic class DefaultStudentSystem implements StudentSystempublic class DefaultStudentSystem implements StudentSystem{

    private StudentDAO studentDAO = new FileStudentDAO();

    public int getNrOfStudents( String subjectCode )public int getNrOfStudents( String subjectCode ){

    Collection students = studentDAO.getAllStudents();

    int count = 0;The StudentDAO

    reference isint count = 0;

    for ( Student student : students ){

    if ( student getSubjects contains( subjectCode ) )

    reference is instantiated with a

    concrete class

    if ( student.getSubjects.contains( subjectCode ) ){

    count++;}

    }}

    return count;}

    }}

  • Works! Or ?Works! Or...?

    The system is a big success University of Oslo wants to adopt it!

    You usef

    University of Oslo usesa MySQL database toa file to store the

    student information

    a MySQL database to store their student

    information

  • Works! Or ?Works! Or...?

    You make a new implementation of the StudentDAO for University of Oslo, a MySQLStudentDAO:

    StudentDAO

    FileStudentDAO MySQLStudentDAO

  • Works! Or ?Works! Or...?

    Wont work! The FileStudentDAO is hard-coded into the StudentSystem:

    public class DefaultStudentSystem implements StudentSystem{

    private StudentDAO studentDAO = new FileStudentDAO();...

    The DefaultStudentSystem implementation is responsible for obtaining a StudentDAOresponsible for obtaining a StudentDAO

    Dependent both on the StudentDAO interface and the implementationp

  • Works! Or ?Works! Or...?

    How to deploy the StudentSystem at different locations? Develop various versions for each location?

    Time consuming Confusing and error-prone Requires more efforts for versioning Requires more efforts for versioning

    Use Dependency Injection! More specific term derived from the term Inversion of ControlMore specific term derived from the term Inversion of Control

  • Dependency InjectionDependency Injection

    Traditional Using Dependency Injection

    Interface/StudentDAO

    Main class/DefaultStudentSystem

    Interface/StudentDAO

    Main class/DefaultStudentSystem

    Implementation/Fil St d tDAO

    Implementation/Fil St d tDAOFileStudentDAO FileStudentDAO

    IoC Container(Spring)

    Configurationmetadata

  • Dependency InjectionDependency Injection

    Objects define their dependencies only through constructor arguments or setter-methodsD d i i j t d i t bj t b t i Dependencies are injected into objects by a container (like Spring)

    Inversion of Control Inversion of Control... Two major types of dependency injection

    Setter injection (preferred in Spring) Setter injection (preferred in Spring) Constructor injection

  • Spring ConfigurationSpring Configuration

    Bean: A class that is managed by a Spring IoC container Setter based DI: Provide a public set-method for the

    d d f

    public class DefaultStudentSystem implements StudentSystem{

    dependency reference

    {private StudentDAO studentDAO;

    public void setStudentDAO( StudentDAO studentDAO ){

    Spring uses the setterto inject the dependency

    behind the scenes{this.studentDAO = studentDAO;

    }

    public int getNrOfStudents( String subjectCode )

    behind the scenes

    public int getNrOfStudents( String subjectCode ){

    List students = studentDAO.getAllStudents();

    // method logic goes here// method logic goes here...}

    }

  • Dependency Injection with SpringDependency Injection with Spring

    C f f Configuration: How to instantiate, configure, and assemble the objects in your application

    The Spring container accepts many configuration The Spring container accepts many configuration formats

    XML based configuration most common, Java properties g , p por programmatically

  • Dependency Injection with SpringDependency Injection with Spring

  • Dependency Injection with SpringDependency Injection with Spring

  • Dependency InjectionDependency Injection

    Advantages of Dependency Injection Easier to swap implementations of dependencies

    The system can be re configured without changing the compiled The system can be re-configured without changing the compiled code

    Easier testing Improved re-usability of your classes Cleaner code

  • Bean propertiesBean properties

    Bean properties can be values defined inline as well (not only references to other beans)S i XML b d fi ti t Springs XML-based configuration supports Straight values (primitives, Strings...) Collections (Lists Sets Maps) Collections (Lists, Sets, Maps) Properties

  • Example: Straight valuesExample: Straight valuespublic class DefaultStudentSystem

    implements StudentSystem{

    private int maxNrOfStudentsPerCourse;

    bli id M N OfS d P C ( i )

    Java bean

    public void setMaxNrOfStudentsPerCourse( int nr ){

    this.maxNrOfStudentsPerCourse = nr;}

    100

    Spring XMLconfiguration file

    (beans.xml)/property

  • Example: Collectionspublic class DefaultStudentSystem

    Example: Collectionsimplements StudentSystem

    {private Map subjects;

    bli id S bj ( M bj )

    Java bean

    public void setSubjects( Map subjects ){

    this.subjects = subjects;}

    Spring XML

    Spring XML

    configuration file(beans.xml)

    INF5750Open Source Software Development

    INF5760Health Information Systems

  • SummarySummarypublic class DefaultStudentSystem

    implements StudentSystemClass where dependencies

    implements StudentSystem{

    private StudentDAO studentDAO;

    public void setStudentDAO( StudentDAO studentDAO )

    are being injected

    {this.studentDAO = studentDAO;

    }

    Configuration

    Fil St d tDAO

    The implementationto inject

    b id d S "

    FileStudentDAO

    IoC Container(Spring)

    p p y

  • IoC using AnnotationsIoC using Annotations

    Annotation is a meta-tag applied to classes, methods... Associated with program behaviour

    @C t @Component Defines a class as a Spring container managed component

    @Autowired @Autowired Tells Spring to inject a component

    Classpath scanning for components Classpath scanning for components

  • ResourcesResources

    Lots of books on Spring Rod Johnson, Juergen Hoeller: Expert One-on-One J2EE

    Development without EJBDevelopment without EJB Justin Gehtland, Bruce A. Tate: Better, Faster, Lighter Java Craig Walls and Ryan Breidenbach: Spring in Action

    The Spring reference documentation www.springframework.org