spring 28-32

// class 28 -32

// dependency , same thing as helper objects 

// inject object's dependencies (dependency injection)

// our coach already provides daily workouts 
// will also provide daily fortunes 
// new helper : fortune service 
// this is a dependency, dependency = helper 
// because the coach depends on the fortune service in order to serve up the daily fortunes 

// injection type 

// two most commons one: constructor injection and setter injection 

// will talk about "auto-wiring" later

// dev process - constructor injection
// 1. define the dependency interface and class 
// 2. create a constructor in your class for injections
// 3. configure the dependency injection in spring config file 




// 1. define the dependency interface and class 
// file : FortuneService.java 
public interface FortuneService {
    public String getFortune();
}


// file: HappyFortuneService.java
public class HappyFortuneService implements FortuneService {
    @Override
    public String getFortune() {
        return "today is your lucky day!";
    }
}


// 2. create a constructor in your class for injections

// File: BaseballCoach.java 
public class BaseballCoach implements Coach {
    
    // define a private field for the dependency
    private FortuneService fortuneService;
    
    // define a constructor for the dependency injection
    public BaseballCoach(FortuneService theFortuneService) {
        fortuneService = theFortuneService;
    }
    
    @Override 
    public String getDailyWorkout() {
        return "spend 30 mins on batting practice";
    }
    
    @Override 
    public String getDailyFortune() {
        
        // use my fortuneService to get a fortune
        // dependency = helper
        return fortuneService.getFortune();
    }
}



// File: TrackCoach.java 
public class TrackCoach implements Coach {
    
    // define a private field for the dependency
    private FortuneService fortuneService;
    
    // define a constructor for the dependency injection
    public TrackCoach(FortuneService theFortuneService) {
        fortuneService = theFortuneService;
    }
    
    @Override 
    public String getDailyWorkout() {
        return "run a hard 5 k";
    }
    
    @Override 
    public String getDailyFortune() {
        
        // use my fortuneService to get a fortune
        // dependency = helper
        return "just do it " + fortuneService.getFortune();
    }
}






// 3. configure the dependency injection in spring config file 
// file : applicationContext.xml

<bean id = "myFortuneService"
    class = "com.luv2code.springdemo.HappyFortuneService">
</bean> 

<bean id = "myCoach"
     class = "com.luv2code.springdemo.BaseballCoach">
     
         <!-- set up constructor injection -->
         // give the id of the bean you want to inject 
        <constructor-arg ref = "myFortuneService" />
</bean> 



// how spring processes your config file 


<bean id = "myFortuneService"
    // fully qualified class name 
    class = "com.luv2code.springdemo.HappyFortuneService">
</bean> 

// in spring framework
HappyFortuneService myFortuneService = new HappyFortuneService();







<bean id = "myCoach"
     class = "com.luv2code.springdemo.BaseballCoach">
        <constructor-arg ref = "myFortuneService" />
</bean> 

// in spring framework 
BaseballCoach myCoach = new BaseballCoach(myFortuneService);




// add new method to the coach interface
public interface Coach {
    public String getDailyWorkout();
    
    public String getDailyFortune();
}


// our main app
package com.luv2code.springdemo;


public class HelloSpringApp {
    
    public static void main (String[] args) {
        //load the spring config file 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        // retrieve bean from spring container
        Coach theCoach = context.getBean("myCoach", Coach.class);
        
        // call methods on the bean
        System.out.println(theCoach.getDailyWorkout());
        
        // let's call our new method for fortunes 
        System.out.println(theCoach.getDailyFortune());
    
        // close the context
        context.close();
    }
}

 

   
上一篇:java实现分布式项目搭建的方法


下一篇:小米Note3 MIUI9可以用的XPosed框架