您的位置:首页 > 移动开发

Spring applicationContext.xml文件解析

2015-07-17 09:34 253 查看
ssh的框架大部分就是在代码中解析xml文件,然后xml文件中配置对应其他关系,话不多说,show code:

所需头文件必不可少,所有的bean都写在这里:

<?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"> </beans>


定义了bean的ID和类

<bean id="hd" class="com.btlh.zz.hb.spring.HelloWorld"></bean>


代码中加载xml文件,获取bean,注意getBean();方法中的参数要和xml文件中ID保持一致

ApplicationContext  context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hd = (HelloWorld)context.getBean("hd");
hd.hello();


未完待续
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: