一個(gè)IoC的Demo程序
beans-config.xml
resource為接口,有ClassPathResource,F(xiàn)ileSystemResource,InputStreamResource,ServletContextRescource或UrlResource等,都可作為XmlBeanFactory構(gòu)造函數(shù)的參數(shù),這兒為XML。BeanFactory管理不同形態(tài)的物件,可以是XML。
HelloBean.java
beans-config.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.5.xsd">
<bean id="helloBean" class="sjtu.rfid.HelloBean">
<property name="helloWord">
<value>Hello! Eric!</value>
</property>
</bean>
</beans>
SpringDemo.java<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.5.xsd">
<bean id="helloBean" class="sjtu.rfid.HelloBean">
<property name="helloWord">
<value>Hello! Eric!</value>
</property>
</bean>
</beans>
package sjtu.rfid;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class SpringDemo {
public static void main(String[] args){
ClassPathResource resource = new ClassPathResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloBean hello = (HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
ClassPathResource在指定classpath中尋找配置文件import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class SpringDemo {
public static void main(String[] args){
ClassPathResource resource = new ClassPathResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(resource);
HelloBean hello = (HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
resource為接口,有ClassPathResource,F(xiàn)ileSystemResource,InputStreamResource,ServletContextRescource或UrlResource等,都可作為XmlBeanFactory構(gòu)造函數(shù)的參數(shù),這兒為XML。BeanFactory管理不同形態(tài)的物件,可以是XML。
org.springframework.beans.factory.BeanFactory是Spring IoC容器的實(shí)際代表者,IoC容器負(fù)責(zé)容納此前所描述的Bean,并對(duì)Bean進(jìn)行管理。BeanFactory是IoC容器的核心接口,加載配置文件的方法:
1.Resource resource = new FileSystemResource("beans-config.xml");
2.ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"ApplicationContext.xml", "ApplicationContext2.xml"});
BeanFactory factory = context;
HelloBean.java
package sjtu.rfid;
public class HelloBean {
private String HelloWord;
public String getHelloWord() {
return HelloWord;
}
public void setHelloWord(String helloWord) {
HelloWord = helloWord;
}
}
public class HelloBean {
private String HelloWord;
public String getHelloWord() {
return HelloWord;
}
public void setHelloWord(String helloWord) {
HelloWord = helloWord;
}
}