Unformatted text preview:

Dependency p y Injection j and Spring Problem area Large information systems contains a huge number of classes that work together How H tto wire i th the classes l ttogether th Example The StudentSystem To improve your skills in Java development you decide to develop a student system You Y d decide id tto use a file fil to t store t student t d t information i f ti You create a class FileStudentDAO responsible for writing and reading to the file You create a class StudentSystem responsible for performing the logic of the system You ve learned that it s a good thing to program to interfaces Service layyer Responsible for performing useful operations on students The StudentSystem interface StudentSystem implements DefaultStudentSystem int getNrOfStudents String subjectCode Persistence e layer depends depends interface St d tDAO StudentDAO implements implements FileStudentDAO Collection Student getAllStudents Responsible for adding deleting and getting students from some storage medium A file The DefaultStudentSystem public class DefaultStudentSystem implements StudentSystem private StudentDAO studentDAO new FileStudentDAO public int getNrOfStudents String subjectCode Collection Student students studentDAO getAllStudents int count 0 for Student student students if student student getSubjects contains getSubjects contains subjectCode count return count The StudentDAO reference is instantiated with a concrete class Works Or Or The system is a big success University of Oslo wants to adopt it You use a file f to store the student information University of Oslo uses a MySQL database to store their student information Works Or Or You make a new implementation of the StudentDAO for University of Oslo a MySQLStudentDAO interface StudentDAO implements FileStudentDAO implements MySQLStudentDAO Works Or Or Won t 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 StudentDAO Dependent both on the StudentDAO interface and the p implementation Works Or 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 Use Dependency Injection More specific term derived from the term Inversion of Control Dependency Injection Traditional Interface StudentDAO Using Dependency Injection Main class DefaultStudentSystem Interface StudentDAO Implementation Fil St d tDAO FileStudentDAO Main class DefaultStudentSystem Implementation Fil St d tDAO FileStudentDAO Configuration metadata IoC Container Spring Dependency Injection Objects define their dependencies only through constructor arguments or setter methods Dependencies D d i are injected i j t d into i t objects bj t b by a container t i like Spring Inversion of Control Control Two major types of dependency injection Setter injection preferred in Spring Constructor injection Spring Configuration Bean A class that is managed by a Spring IoC container Setter based DI Provide a public set method for the d dependency d reference f public class DefaultStudentSystem implements StudentSystem private StudentDAO studentDAO public void setStudentDAO StudentDAO studentDAO this studentDAO studentDAO public int getNrOfStudents String subjectCode List students studentDAO getAllStudents method logic goes here here Spring uses the setter to inject the dependency behind the scenes Dependency Injection with Spring C Configuration f How to instantiate configure f and assemble the objects in your application The Spring container accepts many configuration formats g most common Java p properties p XML based configuration or programmatically DOCTYPE beans PUBLIC SPRING DTD BEAN EN http www springframework org dtd spring beans dtd beans Bean definition bean id studentDAO class no uio inf5750 impl FileStudentDAO beans Dependency Injection with Spring DOCTYPE beans PUBLIC SPRING DTD BEAN EN http www springframework org dtd spring beans dtd Bean identifier must be unique beans bean id studentDAO class no uio inf5750 impl FileStudentDAO bean id studentSystem class no uio inf5750 impl DefaultStudentSystem property name studentDAO ref bean bean studentDAO studentDAO property bean Package qualified class name normally the implementation class Refers to the studentStore attribute in the Java class beans StudentStore injected into StudentRegister Refers to the studentStore bean Dependency Injection with Spring DOCTYPE beans PUBLIC SPRING DTD BEAN EN http www springframework org dtd spring beans dtd beans bean id studentDAO class no uio inf5750 impl MySQLStudentDAO Change to use the MySQL implementation instead of the File implementation bean id studentSystem class no uio inf5750 impl DefaultStudentSystem property name studentDAO ref bean bean studentDAO studentDAO property bean beans University of Oslo can now use the system by altering one configuration line without changing the compiled code Dependency Injection Advantages of Dependency Injection Easier to swap implementations of dependencies The system can be re re configured configured without changing the compiled code Easier testing Improved re usability of your classes Cleaner code Bean properties Bean properties can be values defined inline as well not only references to other beans Spring s S i XML XML based b d configuration fi ti supports t Straight values primitives Strings Collections Lists Lists Sets Sets Maps Properties Example Straight values public class DefaultStudentSystem implements StudentSystem private int maxNrOfStudentsPerCourse Java bean public bli void id setMaxNrOfStudentsPerCourse M N OfS d P C iint nr this maxNrOfStudentsPerCourse nr bean bean id studentSystem id studentSystem class no uio inf5750 impl DefaultStudentSystem property name maxNrOfStudentsPerCourse value 100 value property property bean Spring XML configuration file beans xml Example Collections public class DefaultStudentSystem implements StudentSystem private Map subjects Java bean public bli void id setSubjects S bj M Map subjects bj this subjects subjects bean id studentSystem class no uio inf5750 impl DefaultStudentSystem property name name subjects subjects entry key value INF5750 value key value Open Source Software Development value entry entry key value INF5760 value key value Health Information Systems value entry property bean Spring


View Full Document

Cal State East Bay CS 6320 - Dependency Injection

Loading Unlocking...
Login

Join to view Dependency Injection and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Dependency Injection and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?