演示效果
创建PersonService的接口和实现类,创建WeatherService的接口和实现类,PersionService实现类依赖WeatherService,PersonService有goToSchool的接口,实现这个接口的实现会调用WeatherService的ifRain,如果不会下雨就输出去上学,创建ApplicationContext容器,并且通过该容器获取person实现类的bean,并且调用goToSchool方法。
引入相关包
4.0.2.RELEASE
org.springframework
spring-context
${springframework.version}
在pom文件中添加dependency。本文只需要引入spring-context的包,spring-context包已经依赖了spring-core和spring-beans的包。
使用xml的配置方式
Java
public interface WeatherService {
boolean ifRain();
}
public class WeatherServiceImpl implements WeatherService{
@Override
public boolean ifRain() {
return false;
}
}
public interface PersonService {
void goToSchool(String name);
}
public class PersonServiceImpl implements PersonService {
private WeatherService weatherService;
//在这里需要set,否则xml中无法注入
public void setWeatherService(WeatherService weatherService) {
this.weatherService = weatherService;
}
@Override
public void goToSchool(String name) {
if (weatherService.ifRain()) {
System.out.println("because rain");
return;
}
System.out.println(name + " go to school");
}
}
applicationContext.xml 配置文件
<!-- 在 personService中注入weatherService -->
测试
public static void main(String[] args) {
//创建容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取personService的bean
PersonService personService = (PersonService)applicationContext.getBean("personService");
//调用方法
personService.goToSchool("huang");
}
使用xml + 注解的方式
Java
public interface WeatherService {
boolean ifRain();
}
@Service("weatherService")
public class WeatherServiceImpl implements WeatherService{
@Override
public boolean ifRain() {
return false;
}
}
public interface PersonService {
void goToSchool(String name);
}
@Service("personService")
public class PersonServiceImpl implements PersonService {
@Resource
private WeatherService weatherService;
@Override
public void goToSchool(String name) {
if (weatherService.ifRain()) {
System.out.println("because rain");
return;
}
System.out.println(name + " go to school");
}
}
applicationContext.xml 配置文件
测试
public static void main(String[] args) {
//创建容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取personService的bean
PersonService personService = (PersonService)applicationContext.getBean("personService");
//调用方法
personService.goToSchool("huang");
}
使用纯注解
Java
public interface WeatherService {
boolean ifRain();
}
public class WeatherServiceImpl implements WeatherService{
@Override
public boolean ifRain() {
return false;
}
}
public interface PersonService {
void goToSchool(String name);
}
public class PersonServiceImpl implements PersonService {
private WeatherService weatherService;
//在这里需要set,否则xml中无法注入
public void setWeatherService(WeatherService weatherService) {
this.weatherService = weatherService;
}
@Override
public void goToSchool(String name) {
if (weatherService.ifRain()) {
System.out.println("because rain");
return;
}
System.out.println(name + " go to school");
}
}
@Configuration
public class BeanConfig {
@Bean(name = "personService")
public PersonService personService() {
PersonServiceImpl personService = new PersonServiceImpl();
personService.setWeatherService(weatherService());
return personService;
}
@Bean(name = "weatherService")
public WeatherService weatherService() {
return new WeatherServiceImpl();
}
}
测试
public static void main(String[] args) {
//创建容器
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
//获取personService的bean
PersonService personService = (PersonService)applicationContext.getBean("personService");
//调用方法
personService.goToSchool("huang");
}