spring 46 - 61

// bean lifecyle 

// dev process 
// 1 . define your methods for init and destroy .  2. configure the method names in spring config file 





// 1 . define your methods for init and destroy .
// inside class TrackCoach.java 

public void doMyStartupStuff() {
    System.out.println("TrackCoach : inside method doMyStartupStuff");
}

// add a destroy method 
public void doMyCleanupStuff() {
    System.out.println("TrackCoach : inside method doMyCleanupStuff ");
}



// 2. configure the method names in spring config file 

<bean id = "myCoach"
    class = "com.liv2code.springdemo.TrackCoach"
    init-method = "doMyStartupStuff"
    destroy-method = "doMyCleanupStuff">




// java annotation 
// annotations minimizes the xml configurations , xml configurations can be verbose 

// scanning for component classes 
// spring will scan your java classes for special annotations 
// automatically register the beans in the spring container 


// dev process 
// 1. enable component scanning in spring config file 
// 2. add the @Component Annotation to your java class 
// 3. retrieve bean from spring container 




// 1. enable component scanning in spring config file 
// in applicationContext.xml
<context:component-scan base-package = "com.luv2code.springdemo" />
    



// 2. add the @Component Annotation to your java class 

package com.luv2code.springdemo;


@Component("thatSillyCoach")
public class TennisCoach implements Coach {
    @Override
    public String getDailyWorkout() {
        return "practice your backhand volley";
    }
}




package com.luv2code.springdemo;
public interface Coach {
    public String getDailyWorkout();
}







// 3. retrieve bean from spring container 

// AnnotationDemoApp
package com.luv2code.springdemo;

public class AnnotationDemoApp {
    public static void main(String[] args) {
        // read spring config file 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        // get the bean from spring container 
        Coach theCoach = context.getBean("thatSillyCoach", Coach.class);
        
        // call a method on the bean 
        System.out.println(theCoach.getDailyWorkout())''
        
        // close the context
        context.close();   
    }
}






// @Components default bean id 
// spring also supports default bean id 
// default bean id : the class name, make the first letter lower-case

// class name : TennisCoach , Default bean id : tennisCoach 


package com.luv2code.springdemo;


@Component
public class TennisCoach implements Coach {
    @Override
    public String getDailyWorkout() {
        return "practice your backhand volley";
    }
}





public class AnnotationDemoApp {
    public static void main(String[] args) {
        // read spring config file 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        // get the bean from spring container 
        Coach theCoach = context.getBean("tennisCoach", Coach.class);
        
        // call a method on the bean 
        System.out.println(theCoach.getDailyWorkout())''
        
        // close the context
        context.close();   
    }
}


// spring dependency injection with annotation and autowiring 
// for dependency injection, spring can use auto wiring 
// spring will look for a class that matches the property. matches by type: class or interface
// spring will inject it automatically, hence it's autowired 

// autowiring example 

// inject FortuneService into a Coach implementation

// spring will scan @Components

// anyone implements FortuneService interface ? 

// if so, let's inject them. for example: HappyFortuneService 






// autowiring injection type
// constructor injection, setter injection, field injection

// dev process (use constructor injection and autowiring )
// 1. define the dependency interface and class 
// 2. create a constructor in your class for injections 
// 3. configure the dependency injections with @Autowired Annotation




// 1. define the dependency interface and class 
package com.luv2code.springdemo;

public interface FortuneService {
    public String getFortune();
}







package com.luv2code.springdemo;

@Component
public class HappyFortuneService implements FortuneService{
    
    @Override 
    public String getFortune() {
        return "today is your lucky day!";
    }
}




package com.luv2code.springdemo;
public interface Coach {
    
    public String getDailyWorkout();
    
    public String getDailyFortune();
}







package com.luv2code.springdemo;

@Component
public class TennisCoach implements Coach {
    
    private FortuneService fortuneService;
    
    
    // 2. create a constructor in your class for injections 
    // 3. configure the dependency injections with @Autowired Annotation
    @Autowired
    public TennisCoach (FortuneService theFortuneService) {
        fortuneService = theFortuneService;
    }
    
    @Override 
    public String getDailyWorkout() {
        return "practice your backhand volley";
    }
    
    @Override 
    public String getDailyFortune() {
        fortuneService.getFortune();
        
    }
}








// When using autowiring, what if there are multiple FortuneService implementations? 
// Spring has special support to handle this case. Use the @Qualifier annotation

 https://www.baeldung.com/spring-autowire

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

导航