SSH环境搭建之Spring环境搭建篇

SSH环境搭建之Spring环境搭建篇

一、引入Spring所使用的JAR文件

二、在src目录下创建beans.xml(Spring的容器文件)

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9            http://www.springframework.org/schema/context
10            http://www.springframework.org/schema/context/spring-context-2.5.xsd
11            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13 
14 </beans>

三、创建测试类并测试

1.实体类 TestService.java

 1 package com.xiaonei.test;
 2 
 3 /**
 4  * Created by zhangshengjian on 2017/1/19.
 5  */
 6 public class TestService {
 7 
 8     private String name;
 9 
10     public String getName() {
11         return name;
12     }
13 
14     public void setName(String name) {
15         this.name = name;
16     }
17 }

2.将实体类对应内容配置到beans.xml文件中

1 <bean id="testService" class="com.xiaonei.test.TestService">
2         <property name="name" value="ok"/>
3 </bean>

3.测试类 TestApp.java

 1 package com.xiaonei.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * Created by zhangshengjian on 2017/1/19.
 8  */
 9 public class TestApp {
10 
11     public static void main(String[] args) {
12         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
13         TestService testService = (TestService) ac.getBean("testService");
14         System.out.println(testService.getName());
15     }
16 }

4.测试结果

posted @ 2017-01-19 09:56  坚持到放弃  阅读(230)  评论(0编辑  收藏  举报