spring 32- 41

// setter injection 

// inject dependencies by calling setter methods on your class 

// dev process 
// 1. create setter methods in your class for injections 
// 2. configure the dependency injection in spring config file 



// step 1 : create setter methods in your class for injections 

// file : CricketCoach.java 

public class CricketCoach implements Coach {
    private FortuneService fortuneService;
    
    public CricketCoach {
        
    }
    
    public void setFortuneService(FortuneService fortuneService) {
        this.fortuneService = fortuneService; 
    }
}


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

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

<bean id = "myCricketCoach"
    class = "com.luv2code.springdemo.CricketCoach">
    
    // setter injection
    <property name = "fortuneService" ref = "myFortuneService" />
        
</bean> 
        
        

        
// basically,    



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

// spring 
HappyFortuneService myFortuneService = new HappyFortuneService();






=================

<bean id = "myCricketCoach"
    class = "com.luv2code.springdemo.CricketCoach">
    
    // setter injection
    <property name = "fortuneService" ref = "myFortuneService" />
        
</bean> 
    
        

        
        
// spring         
CricketCoach myCricketCoach = new CricketCoach();
myCricketCoach.setFortuneService(myFortuneService);
        

        
        
============
    
// cricketCoach 
package com.luv2code.springdemo;

public class CricketCoach implements Coach 
    private String emailAddress;
    private String team;
    
    private FortuneService fortuneService;
    
    // create a no-arg constructor 
    public CricketCoach() {
        System.out.println("circket coach " inside a no arg constructor");
    }
                           
    // create setter methods in your class for injections
    public void setFortuneService(FortuneService fortuneService) {
        this.fortuneService = fortuneService; 
    }
                           
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    
     public String getEmailAddress(String emailAddress) {
        return emailAddress;
    }                      
                           
    
    public void setTeam (String team) {
        this.team = team;
    }
     
     public String getTeam (String team) {
        return team
    }                      
    
    @Override 
    public String getDailyWorkout() {
        return "practice fast bowing for 5 mins";
    }
    
    @Override 
    public String getDailyFortune() {
        fortuneService.getFortune();
    }
}
     
                           
                           
                           
                           
// 2. configure the dependency injection in spring config file 

                           
// 2. load properties file in spring config file 
<context: property-placeholder location = "classpath: sport.properties"/>                           
                           
                           
                           
<bean id = "myFortuneService"
    class = "com.luv2code.springdemo.HappyFortuneService">
</bean> 
                           
                           
                           
<bean id = "myCoach"
    class = "com.luv2code.springdemo.TrackCoach">
    
    // set up constructor injection 
    <constructor-arg ref = "myFortuneService" />
        
</bean> 
                           
                           

<bean id = "myCricketCoach"
    class = "com.luv2code.springdemo.CricketCoach">
    
    // set up setter injection
    <property name = "fortuneService" ref = "myFortuneService" />
    
    // inject literal values
                           
    // setEmailAddress                       
    <property name = "emailAddress" value = "thebestcoach@luv2code.com" />
    // 3 reference values from properties file                       
    <property name = "emailAddress" value ="${foo.email}" />  
                           
    // setTeam                       
    <property name = "team" value = "Sunrise hyderbad" />
    // 3 reference values from properties file                        
    <property name = "team" value ="${foo.team}" />                       
</bean>  
            
                           
                           
                           
                           
                           
                           
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
        CricketCoach theCoach = context.getBean("myCricketCoach", CricketCoach.class);
        
        // call methods on the bean
        System.out.println(theCoach.getDailyWorkout());
        
        // let's call our new method for fortunes 
        System.out.println(theCoach.getDailyFortune());
        
        // call our new methods to get the literal values
        System.out.println(theCoach.getEmailAddress());
        System.out.println(theCoach.getTeam());
    
        // close the context
        context.close();
    }
}
                           

                           
                           
// injecting values from a properties file (it's like a config like i think ) 
//   dev process : 1 . create properties file    2. load properties file in spring config file   3 reference values from properties file                        
                           
// 1 . create properties file
// file : sport.properties                            
foo.email = haha@gmail.com
foo.team = in n out buger                         

                           
// 2. load properties file in spring config file                            
// see above 
                           
// 3 reference values from properties file           
// see above ${the prop name}"  

 

posted on 2019-09-10 15:47  猪猪&#128055;  阅读(117)  评论(0)    收藏  举报

导航