1、定义接口Action
package ioc.iocSample;
public interface Action {
public void greet();
}
2、实现接口Action
package ioc.iocSample;
public class Welcome implements Action {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void greet() {
System.out.println("Welcome"+name);
}
}
3、文件beans.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="greet" class="ioc.iocSample.Welcome">
<property name="name">
<value>Spring IoC</value>
</property>
</bean>
</beans>
4、测试类:对bean进行实例化
package ioc.iocSample;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
/**
* 加载配置文件
*/
ClassPathResource resource=new ClassPathResource("ioc/iocSample/applicationContext.xml");
/**
* 根据配置文件构造BeanFactory对象
*/
BeanFactory factory=new XmlBeanFactory(resource);
/**
* 取得具体业务逻辑的JavaBean
*/
Action action=(Action) factory.getBean("greet");
/**
* 调用javaBean 中的具体方法
*/
action.greet();
}
}