Spring Spring基本配置和IOC
2015-08-20 22:45
405 查看
1、导包:
spring.jar
commons-logging.jar
2、建包 cn.google.spring.sh.ioc.createobject.HelloWorld
beans:把一个类放入spring容器中,该类就是一个bean
id:唯一标示符
class:类全名
4、建测试包 test/cn.google.spring.test
5、结果
输出“hello by spring”
spring.jar
commons-logging.jar
2、建包 cn.google.spring.sh.ioc.createobject.HelloWorld
public class HelloWorld{ public void hello(){ s.o.p("hello by spring"); } }3、建 spring的配置文件,名称为applicationContext.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="helloWorld" class="cn.google.spring.sh.ioc.createobject.HelloWorld"> </beans>说明:
beans:把一个类放入spring容器中,该类就是一个bean
id:唯一标示符
class:类全名
4、建测试包 test/cn.google.spring.test
public class CreateObjectTest{ @Test public void testCreateObject(){ //启动spring 容器 ApplicationContext context = ClassPathXmlApplicationContext("applicationContext.xml"); // 从spring容器中把对象取出来 HelloWorld helloWorld = context.getBean("helloWorld"); //对象调用方法 helloWorld.hello(); } }
5、结果
输出“hello by spring”
相关文章推荐