Spring之001:快速入门, 了解配置文件

Posted 浅弋、璃鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring之001:快速入门, 了解配置文件相关的知识,希望对你有一定的参考价值。


title: 一、Spring快速入门, 了解配置文件
date: 2021-01-13 15:54:23
tags:
- Java
- spring
categories:
- Java
- spring

一、spring介绍:

1. spring是什么:

Spring是分层的Java SE/EE应用full-stack轻量级开源框架, 以IoC(反转控制)和AOP(面向切面编程)为内核;
提供展现层SpringMVC持久层Spring JDBC Template以及业务层事务管理等众多的企业级应用技术, 还能整合开源世界众多著名的第三方框架爱和类库;

2. spring发展;

3.spring优势:

  • 方便解耦, 简化开发:通过Spring提供的IoC容器, 可以将对象间的依赖交由Spring进行控制,避免硬编码所造成的的过渡耦合;用户不必再为单例模式类、属性文件解析等底层需求写代码, 而更专注于上层应用;
  • AOP编程的支持: 许多不易用传统OOP实现的功能可以通过AOP轻松实现;
  • 声明式事务支持: 通过配置的方式, 解放事务管理代码;
  • 方便程序测试:
  • 方便集成各种优秀的框架:
  • 降低了JavaEE API的使用难度: spring对javaEE进行了薄封装, 使API使用难度降低;
  • java源码典范: spring设计的妙啊;

4. spring的体系结构:

二、Spring快速入门:

1. spring程序开发步骤:

  • 导入spring的maven坐标;
  • 编写DAO(如: userDaolmpl)接口和实现类;
  • 创建spring核心xml配置文件;
  • 在xml配置文件中配置userDaolmpl;
  • 通过spring的API获取Bean实例;

2. 代码演示:

  • 导入spring:
    /spring/spring_ioc/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.zhouchengfei</groupId>
    <artifactId>spring_ioc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
    </dependencies>

</project>
  • 编写DAO(如: userDaolmpl)接口和实现类:
    spring/spring_ioc/src/main/java/com/itheima/dao/UserDao.java
package com.itheima.dao;

public interface UserDao {
    public void save();
}

spring/spring_ioc/src/main/java/com/itheima/dao/impl/UserDao.java

package com.itheima.dao.impl;
import com.itheima.dao.UserDao

public class UserDao implements UserDao {
    public void save() {
        System.out.println("sace running......");
    }
}
  • 创建spring核心xml配置文件;
    spring/spring_ioc/src/main/resources/applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- add bean -->
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
</beans>
  • 通过spring的API获取Bean实例:
    spring/spring_ioc/src/main/java/com/itheima/demo/UserDaoDemo.java
package com.itheima.demo;

import com.itheima.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserDaoDemo {
    public static void main(String[] args) {
        // 通过spring的API获取Bean实例
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) app.getBean("userDao");
        userDao.save();
    }
}

三、spring配置文件:

spring/spring_ioc/src/main/resources/applicationContext.xml

1. Bean标签:

1.1 基本配置:

  • id: 唯一标识;
  • class: 全限定名; 必须存在"无参构造函数";

1.2 范围配置:

scope: 指对象的作用范围, 取值如下:

取值说明
singleton(默认值)单例的
prototype多例的
requestWeb项目中, Spring创建一个Bean对象, 将对象存入到request域中
sessionWeb项目中, Spring创建一个Bean对象, 将对象存入到session域中
global sessionWeb项目中, 应用在Portlet环境, 如果没有Portlet环境, global session相当于session

使用junit进行测试scope属性:

a. singletonprototype:
  • 注册junit:

pring/spring_ioc/src/main/resources/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<project ...>
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.zhouchengfei</groupId>
    <artifactId>spring_ioc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        ...
    </properties>
    <dependencies>
       ...

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
</project>
  • 单元测试代码:
    spring/spring_ioc/src/test/java/com/itheima/test/SpringTest.java
package com.itheima.test;
import org.junit.Test;

import com.itheima.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    // 测试scope属性
    public void test1() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao1 = (UserDao) app.getBean("userDao");
        UserDao userDao2 = (UserDao) app.getBean("userDao");
        System.out.println(userDao1);
        System.out.println(userDao2);
    }
}
b. 实例创建时机不同:

spring/spring_ioc/src/main/java/com/itheima/dao/impl/UserDaoImpl.java

package com.itheima.dao.impl;
import com.itheima.dao.UserDao;

public class UserDaoImpl implements UserDao {
    public UserDaoImpl() {
        // 增加一个无参构造方法
        System.out.println("UserDaoImpl创建......");
    }

    public void save() {
        System.out.println("sace running......");
    }
}

测试结果:

  • scope=“singleton”:
UserDaoImpl创建......
com.itheima.dao.impl.UserDaoImpl@1fe20588
com.itheima.dao.impl.UserDaoImpl@1fe20588

注意:
对象的创建是在加载配置文件的时就创建了;

  • scope=“prototype”:
UserDaoImpl创建......
UserDaoImpl创建......
com.itheima.dao.impl.UserDaoImpl@14d3bc22
com.itheima.dao.impl.UserDaoImpl@12d4bf7e

注意:
对象的创建是在加载配置文件的时没有创建, 而是在getBean的时候创建;

c.结论:
singletonprototype
Bean的实例化个数1个多个
Bean的实例化时机当Spring核心文件被加载时, 实例化配置的Bean示例当调用getBean()时实例化Bean
Bean的生命周期1. 对象创建: 当应用加载, 创建容器时, 对象就被创建了;
2. 对象运行: 只要容器在, 对象一直活着;
3. 对象销毁: 当应用卸载, 销毁容器时, 对象就被销毁;
1. 对象创建: 当时用对象时, 创建新的对象示例;
对象运行: 只要对象在使用, 就一直活着;
3. 对象销毁: 当对象长时间不使用, 被java GC;

1.3 生命周期配置:

  • init-method: 指定类中的初始化方法;
  • destroy-method: 指定类中销毁方法名称;

spring/spring_ioc/src/main/java/com/itheima/dao/impl/UserDaoImpl.java

package com.itheima.dao.impl;
import com.itheima.dao.UserDao;

public class UserDaoImpl implements UserDao {
    public UserDaoImpl() {
        System.out.println("UserDaoImpl创建......");
    }

    public void init() {
        System.out.println("这是初始化方法...");
    }
    public void destroy() {
        System.out.println("这是销毁方法...");
    }

    public void save() {
        System.out.println("sace running......");
    }
}

spring/spring_ioc/src/main/resources/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" init-method="init" destroy-method="destroy"></bean>
</beans>

spring/spring_ioc/src/main/resources/applicationContext.xml

package com.itheima.test;
import org.junit.Test;

import com.itheima.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    // 测试生命周期
    public void test2() {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao1 = (UserDao) app.getBean("userDao");
        System.out.println(userDao1);
        app.close(); // 手动关闭容器;
        // ApplicationContext: 没有关闭方法, 但是其子类ClassPathXmlApplicationContext 有关闭方法
    }
}

结果:

...
信息: ...
信息: ...
UserDaoImpl创建......
这是初始化方法...
com.itheima.dao.impl.UserDaoImpl@61dd025
1月 14, 2021 6:41:51 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: ...
这是销毁方法...

1.4 Bean实例化配置:

  • 无参构造方法实例化;
  • 工厂静态方法实例化;
  • 工厂实例方法实例化;

a.工厂静态方法:

spring/spring_ioc/src/main/java/com/itheima/factory/StaticFactory.java

package com.itheima.factory;

import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;

public class StaticFactory {
    public static UserDao getUserDao() {
        return new UserDaoImpl();
    }
}

spring/spring_ioc/src/main/resources/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<!--    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" init-method="init" destroy-method="destroy"></bean>-->
    <bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>
</beans>

spring/spring_ioc/src/test/java/com/itheima/test/SpringTest.java

package com.itheima.test;
import org.junit.Test;

import com.itheima.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    // 测试静态工厂构造
    public void test2() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao1 = (UserDao) app.getBean("userDao");
        System.out.println(userDao1);
    }
}

结果:

UserDaoImpl创建......
com.itheima.dao.impl.UserDaoImpl@1a04f701

b.工厂实例方法:

spring/spring_ioc/src/main/java/com/itheima/factory/DynamicFactory.java

package com.itheima.factory;

import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;

public class DynamicFactory {
    public UserDao getUserDao() {
        return new UserDaoImpl();
    }
}

spring/spring_ioc/src/main/resources/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<!--    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl" init-method="init" destroy-method="destroy"></bean>-->
    <!--  工厂静态构造    -->
<!--    <bean id="userDao" class="com.itheima.factory.StaticFactory" factory-method="getUserDao"></bean>-->
    <!--  工厂实例构造  -->
    <bean id="factory" class="com.itheima.factory.DynamicFactory"></bean>
    <bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>
</beans>

1.5 Bean的依赖注入–对象:

a.一般方式注入:

方式: 在业务代码中获取dao
spring/spring_ioc/src/main/java/com/itheima/service/UserService.java

package com.itheima.service;

public interface UserService {
    public void save();
}

spring/spring_ioc/src/main/java/com/itheima/service/impl/UserServiceImpl.java

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl implements UserService {
    public void save() {
       // 从容器中获取Dao
       ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
       UserDao userDao = (UserDao) app.getBean("userDao");
       userDao.save();
   }
}

spring/spring_ioc/src/main/resources/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
    <!--  依赖注入  -->
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"></bean>
</beans>

spring/spring_ioc/src/main/java/com/itheima/demo/UserController.java

// 业务代码:
package com.itheima.demo;


import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserController {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) app.getBean("userService");
        userService.save();
    }
}

结果:

信息: ...
信息: ...
UserDaoImpl创建......
信息: ...
信息: ...
UserDaoImpl创建......
sace running......

b.依赖注入分析:

  • 依赖注入: 它是spring框架核心IOC的具体实现;
  • 在编写程序时,通过控制反转, 把对象创建交给spring, 但是代码中不可能出现没有依赖的情况;
  • IOC解耦只是降低了他们的依赖关系, 但不会消除; 例如: 业务层仍会调用持久层的方法;
  • 这种业务层和持久层的依赖关系, 在使用spring之后, 就让spring类维护了;
  • 简单的说, 就是坐等框架把持久层对象传入业务层, 而不用自己来获取;

c.依赖注入的方式:

  • 构造方法;
  • set方法;

d.使用set方法注入:

spring/spring_ioc/src/main/java/com/itheima/service/UserService.java

package com.itheima.service;

public interface UserService {
    public void save();
}

spring/spring_ioc/src/main/java/com/itheima/service/impl/UserServiceImpl.java

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void save() {
        this.userDao.save();
    }

    //    public void save() {
//        // 从容器中获取Dao
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//        UserDao userDao = (UserDao) app.getBean("userDao");
//        userDao.save();
//    }
}

spring/spring_ioc/src/main/resources/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
    <!--  依赖注入: set方法  -->
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
    <bean id=以上是关于Spring之001:快速入门, 了解配置文件的主要内容,如果未能解决你的问题,请参考以下文章

001-快速构建系统(Spring Boot + ant Design新版全开源)简介

Spring Boot入门样例-001-Java和Maven安装配置

Spring框架的IOC之注解方式的快速入门

Spring Boot 入门 (5) 深入Actuator

hadoop快速入门之DKH安装准备

MyBatis 框架之快速入门程序