J2EE中简单搭建Spring4

我在这就简要给出了,不详细说明了, 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

相关文章
相关标签/搜索