開發環境:
Spring 4.0.0+Myeclipse10+JDK1.7
開發過程:
package com.at.beans;
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("name " + name);
}
}
<?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-4.0.xsd">
<!-- 這裏的class是使用反射創建的類對象,相當於HelloWorld helloWorld = new HelloWorld(); -->
<bean id="helloWorld" class="com.at.beans.HelloWorld">
<property name="name" value="spring"></property>
</bean>
</beans>
package com.at.beans;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestHelloWorld {
public static void main(String[] args) {
//1、創建spring的IOC容器對象
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//2、從IOC容器中獲取bean實例
HelloWorld helloWorld=(HelloWorld)context.getBean("helloWorld");
//3、調用方法
helloWorld.hello();
}
}
五月 17, 2017 4:19:32 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5a874bd3: startup date [Wed May 17 16:19:32 CST 2017]; root of context hierarchy
五月 17, 2017 4:19:32 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
name spring
By luoyepiaoxue2014