Section 4 Flashcards
Ce problema rezolva Spring ?
In loc sa modificam codul sursa, putem schimba fisierul de configurare applicationContext.xml. Asta inseamna ca aplicatia devine configurabila ( va trebui sa inteleg chestia asta probabil printr-un exemplu )
Ce sunt Spring Beans ?
Spring Beans se numesc clasele create de Spring Container
create a Spring application using configuration file and dependenci injection ( DI )
//Config file
// public interface Coach {
public String getDailyWorkout(); public String getDailyFortune();
}
//interface public interface FortuneService {
public String getFortune();
}
// public class HappyFortuneService implements FortuneService {
@Override public String getFortune() { return "Today is your lucky day!"; }
}
// public class BaseballCoach implements Coach {
private FortuneService fortuneService;
public BaseballCoach(FortuneService fortuneService) { this.fortuneService = fortuneService; }
@Override public String getDailyWorkout() { return "Spend 30 minutes on batting practice"; } @Override public String getDailyFortune() { return fortuneService.getFortune(); }
}
//public class HelloSpringApp {
public static void main(String[] args) {
//load spring configuration file ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/java/com/springboot/demo/applicationContext.xml");
//retrieve bean from spring container Coach theCoach = context.getBean("myCoach", Coach.class); System.out.println(theCoach.getDailyWorkout());
System.out.println(theCoach.getDailyFortune());
// close context context.close();
}
}
Cum se numesc obiectele create de Spring Container ?
Spring Beans
De ce se foloseste pattern-ul Spring?
Se foloseste pentru a nu fi nevoit sa modifici codul sursa, astfel se poate modifica doar fisierul de configurare.
Folosind:
- fisierul applicationContext.xml ( care se afla pe location dekstop/Udemy/Section4
- Clasele: HelloSpringApp, TrackCoach
- Creati un Spring Bean
- Printati rezultatul metodei getDailyWorkout();
“Run a hard 5k”
public class HelloSpringApp {
public static void main(String[] args) {
//load spring configuration file - This is container ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:src/main/java/com/springboot/demo/applicationContext.xml");
//retrieve bean from spring container Coach myCoach = context.getBean("myCoach", Coach.class); System.out.println(myCoach.getDailyWorkout());
// close context context.close(); }
}