我在这就简要给出了,不详细说明了,
首先,导入jar文件,如下图所示:
创建一个类用于测试:
在src下创建一个名为SpringApplicationContext.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"
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>
之后配置web.xml——往web.xml中加入下面的部分(表示以Spring开头且以.xml结尾的文件),如下所示:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:Spring*.xml
</param-value>
</context-param>
之后,用如下方式得到bean,即实例化了的对象:
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());
}
}
来源:oschina
链接:https://my.oschina.net/u/2518341/blog/664873