spring初体验笔记,spring的使用

//创建容器,
ApplicationContext ac=new ClasspathXmlApplicationContext("beans.xml");
WelcomeService ws=(WelcomeService)ac.getBean("ws");
ws.sayHello();

在创建容器的时候参考beans.xml文件将对象组装好,不需要手动new一个对象了,

以下是beans.xml文件内容,已经给定message的value值

<?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.xsd">

<bean id="ws" class="com.tian.springdemo.service.WelcomeService">
<property name="message" value="hello spring"/>
</bean>
</beans>

spring将bean的信息放到xml文件中,通过xml配置内容,在spring中直接实例化,通过getbean方法获得bean

 

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念