Spring. Dependency Injection
The client delegates to call to another object the responsibility of providing its dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myFortune" class="com.example.demo.HappyFortuneService">
</bean>
<bean id = "myCoach" class="com.example.demo.TrackCoach">
<constructor-arg ref="myFortune"/>
</bean>
</beans>
package com.example.demo;
public class TrackCoach implements Coach {
private FortuneService fortuneService;
public TrackCoach(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Run a hard 5k";
}
@Override
public String getDailyFortune() {
return "Just do it: " + fortuneService.getFortune();
}
}Setter Injection
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myFortune" class="com.example.demo.HappyFortuneService">
</bean>
<bean id = "myCoach" class="com.example.demo.BaseballCoach">
<constructor-arg ref="myFortune"/>
</bean>
<bean id="myCricketCoach" class="com.example.demo.CricketCoach">
<property name="fortuneService" ref="myFortune"/>
</bean>
</beans>package com.example.demo;
public class CricketCoach implements Coach{
private FortuneService fortuneService;
public CricketCoach() {
System.out.println("CricketCoach: inside no-arg constructor");
}
public void setFortuneService(FortuneService fortuneService) {
System.out.println("CricketCoach: inside setter method - setFortuneService");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Practice fast bowling for 15 minutes";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}package com.example.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetterDemoApp {
public static void main(String[] args) {
// load the spring configuration file
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieve bean from spring container
CricketCoach coach = context.getBean("myCricketCoach", CricketCoach.class);
// call methods on the bean
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
// close the context
context.close();
}
}Injecting Literal Values
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="myFortune" class="com.example.demo.HappyFortuneService">
</bean>
<bean id = "myCoach" class="com.example.demo.BaseballCoach">
<constructor-arg ref="myFortune"/>
</bean>
<bean id="myCricketCoach" class="com.example.demo.CricketCoach">
<property name="fortuneService" ref="myFortune"/>
<property name="emailAddress" value="example@gmail.com"/>
<property name="team" value="Tornado"/>
</bean>
</beans>package com.example.demo;
public class CricketCoach implements Coach {
private FortuneService fortuneService;
// add new fields for emailAddress and team
private String emailAddress;
private String team;
public CricketCoach() {
System.out.println("CricketCoach: inside no-arg constructor");
}
public void setFortuneService(FortuneService fortuneService) {
System.out.println("CricketCoach: inside setter method - setFortuneService");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Practice fast bowling for 15 minutes";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
System.out.println("CricketCoach: inside setter method - setEmailAddress");
this.emailAddress = emailAddress;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
System.out.println("CricketCoach: inside setter method - setTeam");
this.team = team;
}
}package com.example.demo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SetterDemoApp {
public static void main(String[] args) {
// load the spring configuration file
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// retrieve bean from spring container
CricketCoach coach = context.getBean("myCricketCoach", CricketCoach.class);
// call methods on the bean
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
System.out.println(coach.getEmailAddress());
System.out.println(coach.getTeam());
// close the context
context.close();
}
}Injecting values from a Properties File
foo.emai=test@gmail.com
foo.team=Bastards<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="sport.properties"/>
<bean id="myFortune" class="com.example.demo.HappyFortuneService">
</bean>
<bean id = "myCoach" class="com.example.demo.BaseballCoach">
<constructor-arg ref="myFortune"/>
</bean>
<bean id="myCricketCoach" class="com.example.demo.CricketCoach">
<property name="fortuneService" ref="myFortune"/>
<property name="emailAddress" value="${foo.emai}"/>
<property name="team" value="${foo.team}"/>
</bean>
</beans>All other classes the same.
Комментарии
Отправить комментарий