spring入门步骤

步骤一:下载spring的开发包

http://repo.spring.io/webapp/search/artifact/?0&q=spring-framework
解压:(Spring目录结构:)
* docs :API和开发规范.
* libs :jar包和源码.
* schema :约束.

步骤二:创建Web项目,引入spring开发jar包

核心jar包四个:bean/Core/Context/Expression Language
两个开发包 : log日志包/log4j包

spring入门步骤

步骤三:创建包结构

    com.itheima.spring.demo1

    * UserService

    * UserServiceImpl

步骤四:创建Spring的配置文件

文件名最好叫:  applicationContext.xml

引入Schema约束:    查找方法>

* spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html

1
2
3
4
5
6
7
<? 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 >

完成配置:

1
2
3
4
5
6
7
8
9
<? 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">
<!-- bean definitions here -->
     < bean id = "studentService" class = "com.mickeymouse.ioc.StudentServiceImpl" ></ bean >
</ beans >

步骤五:编写测试代码

1
2
3
4
5
6
7
8
9
@Test
     public void test1(){
         //获取配置文件
         String path = "applicationContext.xml";
         //加载配置文件
         ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
         StudentService studentService = (StudentService) applicationContext.getBean("studentService");     
         studentService.addStudent();
     }



来自为知笔记(Wiz)


你可能感兴趣的:(spring入门步骤)