SpringIOC的一个小例子 有一处不明白
package com.jike.spring.chapter01;
//接口
public interface IHelloMessage {
public String sayHello();
}
package com.jike.spring.chapter01;
public class HelloChina implements IHelloMessage {
@Override
public String sayHello() {
return "大家好";
}
}
package com.jike.spring.chapter01;
public class HelloWorld implements IHelloMessage {
@Override
public String sayHello() {
return "Hello World!";
}
}
package com.jike.spring.chapter01;
public class Person {
//因为Person类需要引用 IHelloMessage 所以要把IHelloMessage写成一个属性
private IHelloMessage helloMessage;
public IHelloMessage getHelloMessage() {
return helloMessage;
}
public void setHelloMessage(IHelloMessage helloMessage) {
this.helloMessage = helloMessage;
}
public String sayHello(){
return this.helloMessage.sayHello(); //调用IHelloMessage这个接口来输出问候信息
}
}
package com.jike.spring.chapter01;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class Main {
public static void main(String[] args) {
//利用FileSystemResource 来读取配置文件
Resource r = new FileSystemResource("helloMessage.xml");
//使用XmlBeanFactory来加载配置文件 启动IOC容器
BeanFactory f = new XmlBeanFactory(r);
//从IOC容器中获取Person类的实例
Person lizi = (Person)f.getBean("chaoji");
//需要用Person实例来输出问候信息
String s = lizi.sayHello();
System.out.println(s);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "helloWorld" class = "com.jike.spring.chapter01.HelloWorld">
</bean>
<bean id = "helloChina" class = "com.jike.spring.chapter01.HelloChina">
</bean>
<bean id = "chaoji" class = "com.jike.spring.chapter01.Person">
<property name = "helloMessage" ref = "helloWorld">
</property>
</bean>
</beans>
这个例子的意思是 让一个人 在国内的时候说中文 大家好 在国外说helloworld 很简单的SpringIOC 小例子
但是有一点不明白 Person类中的 return this.helloMessage2.sayHello(); 这句话不明白 这句话的目的是什么啊 为什么要写这句话。
...全文
相关推荐
simple-springioc:这是一个简单的SpringIOC
"simple-springioc"项目解析该项目名为"simple-springioc",显然是一个用于演示Spring IOC基本概念和使用的简化版本。
SpringIOC DI循环依赖实例详解
SpringIOC DI 循环依赖实例详解SpringIOC 是一个基于依赖注入(Dependency Injection,简称 DI)的容器,用于管理 Bean 的生命周期。
SpringIOC refresh()初始化代码实例
SpringIOC refresh()初始化代码实例是 Spring 框架中一个非常重要的初始化步骤,负责实例化所有不需要延迟初始化的单例bean。
springioc加载流程图.pdf
springioc加载流程图.pdf
SpringIOC 详解
SpringIOC Spring 框架是一个流行J2EE的开源产品,它提供了统一的应用架构方式、大量的中间层功能模块,大大简化了传统J2EE的开发模式。它的核心代码均来自于真实的项目,是由Spring


Java SE
