Spring入门

2018-05-05  本文已影响0人  嗷老板

一、Spring的概述

1、什么是Spring

  Spring是一个企业中常用的一站式的解决框架,用于解决web开发当中各个层级之间的问题,说白了就是spring可以支持我们各个层级包括我们的数据展示层,我们的业务层,我们的持久层等都有对应的解决方案。

2、Spring的优点

3、Spring的架构体系

Spring的架构体系

二、Spring框架的入门案例

第一步:首先创建maven web工程

跳过骨架选择

创建maven工程
选择web工程
选择web工程

解决JDK版本问题和web.xml文件缺失的问题

解决JDK版本问题
  在pom.xml文件中,加入下面的插件

  <build>  
    <plugins>  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <configuration>  
                <source>1.7</source>
                <target>1.7</target>  
            </configuration>  
        </plugin>  
    </plugins>  
  </build>

创建web.xml文件
  在webapp目录下创建WEB-INF文件夹,在WEB-INF中创建web.xml文件,加入以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>01_helloWorld</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

最后更新项目

第三步:导入Spring框架的核心jar包

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
    </dependencies>

第四步:创建接口和实现类

UserDao接口

package com.itheima.dao;

public interface UserDao {
    public void sayHello();
}

实现类UserDaoImpl

package com.itheima.dao;

public class UserDaoImpl implements UserDao {

    private String userName;

    @Override
    public void sayHello() {
        System.out.println("HelloWorld");
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

第五步:在applicationContext.xml中配置Spring容器

<?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 id="userDao" class="com.itheima.dao.UserDaoImpl">
        <property name="userName" value="张三"></property>
    </bean>

</beans>

第六步:开发测试用例

    @Test
    public void getUser() throws Exception {
        //创建spring容器的对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //利用容器对象创建UserDao对象
        UserDao bean = (UserDao) context.getBean("userDao");
        //调用方法
        bean.sayHello();
    }
上一篇 下一篇

猜你喜欢

热点阅读