我在這就簡要給出了,不詳細說明了, web
首先,導入jar文件,以下圖所示: spring
建立一個類用於測試: 測試
在src下建立一個名爲SpringApplicationContext.xml的文件,然後該文件中的內容以下所示: ui
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="user" class="com.yun.buildHibernate.entity.User">
<property name="id" value="1"/>
<property name="name" value="耶律齊元"/>
<property name="password" value="dmj432156789"/>
<property name="email" value="dmj@163.com"/>
<property name="address" value="雲大昆明"/>
</bean>
</beans>
spa
以後配置web.xml——往web.xml中加入下面的部分(表示以Spring開頭且以.xml結尾的文件),以下所示: xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:Spring*.xml
</param-value>
</context-param>
對象
以後,用以下方式獲得bean,即實例化了的對象: get
private void testSpring(){
ApplicationContext ac = new ClassPathXmlApplicationContext("SpringApplicationContext.xml");
User user = (User) ac.getBean("user");
if(user == null){
Out.println("獲得的user的值是null");
}else{
Out.println(user.toString());
}
}
it