Service层的使用 - Spring框架 - IOC
目录
Spring框架
Spring框架的概述
Spring框架的优点
Spring两大核心功能
Spring - IOC核心技术
IOC的概述
IOC的入门程序
IOC技术
ApplicationContext接口
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
Spring - Bean
Bean管理的配置文件方式
Bean对象实例化的三种方式
多配置文件方式
Service层(业务逻辑层)使用的技术是Spring框架。
Spring框架
Spring框架的概述
Spring是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其它层的松耦合问题,因此,它将面向接口的编程思想贯穿整个系统应用。
Spring是于2003年兴起的一个轻量级的Java开发框架。
Spring框架的优点
- IOC的作用:方便解耦,简化开发。Spring可以将所有对象创建和依赖关系维护,交给Spring管理。
- AOP编程的支持:Spring提供面向切面编程,可以方便实现对程序进行权限拦截、运行监控等功能。
- 声明式事务支持:只需通过配置就可完成对事务的管理,无需手动编程。
- 通过注解,方便程序测试:Spring对Junit4支持,可通过注解的方式测试Spring程序。
- 方便集成各种优秀框架(如:Struts2、Hibernate、MyBatis等)。
- 通过封装,降低JavaEE API的使用难度:Spring对JavaEE开中非常难用的API(JDBC、Java Mail、远程调用等),都提供了封装,使这些API应用难度大大降低。
Spring两大核心功能
- Spring IOC:控制反转
- Spring AOP:面向切面编程
Spring - IOC核心技术
IOC的概述
控制反转(Inverse of Control),将对象的创建权力反转给Spring框架。
作用:用来降低代码之间耦合度高的问题。Spring的工厂读取配置文件。
IOC的入门程序
1. 创建maven的Java项目
2. pom.xml导入spring依赖
<dependencies><!-- spring核心依赖 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><!-- spring内部日志 --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!-- 通用日志 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>
</dependencies>
3. 编写接口和实现类,编写具体的实现方法
// 接口类
package com.qcby.service;public interface UserService {public void hello();
}
// 实现类
package com.qcby.service;public class UserServiceImpl implements UserService {@Overridepublic void hello(){System.out.println("Hello IOC!");}
}
4. 编写Spring核心的配置文件:在目录src -> resource下创建applicationContext.xml的配置文件,该名称可任意起名,但一般默认为改名
<?xml version="1.0" encoding="UTF-8"?>
<!-- 来源于pom.xml中引入的Spring依赖jar包 -->
<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"><!-- IOC管理bean --><bean id="userService" class="com.qcby.service.UserServiceImpl"></bean>
</beans>
bean只能管理类,不能管理接口
5. 把log4j.properties的配置文件拷贝到resources目录下,做为log4j的日志配置文件。
- 日志打印级别
- debug:调试信息
- info:运行时信息
- warn:警告信息
- error:错误信息
6. 编写测试方法
package com.qcby;import com.qcby.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class UserTest{//传统写法@Testpublic void run(){UserServiceImpl userService = new UserServiceImpl();userService.hello();System.out.println(userService);}/*** spring写法* 入门程序*/@Testpublic void run1(){// 使用Spring的工厂ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");// 通过工厂获得bean类:UserService userService = (UserService) applicationContext.getBean("userService");// 调用方法userService.hello();System.out.println(userService);}
}
工作流程:创建容器 → 读取配置 → 创建 Bean → 获取 Bean → 使用 Bean
IOC技术
ApplicationContext接口
是Spring框架中IOC容器的核心接口。
- 主要职责:
- 创建和管理Bean:负责实例化、配置和组装应用程序中的Bean对象(这些对象在 Spring 中被称为 Bean)。
- 注入依赖:自动解决Bean之间的依赖关系。
- 提供高级功能:国际化消息支持、资源加载、时间发布机制、AOP集成
- 有两个具体的实现类:
- ClassPathXmlApplicationContext:加载类路径下的Spring配置文件
- FileSystemXmlApplicationContext:加载本地磁盘下的Spring配置文件
ClassPathXmlApplicationContext
是 ApplicationContext 接口的一个 具体实现类,专门用于从 类路径(classpath) 加载 XML 格式 的配置文件。
名称解析:
- ClassPath:从 classpath(编译后的 src/main/resources 目录)查找文件
- Xml:使用 XML 文件作为配置方式
- ApplicationContext:应用上下文实现
基本用法:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
FileSystemXmlApplicationContext
是 ApplicationContext 接口的另一个重要实现类,用于从 文件系统路径 加载 XML 配置文件。
名称解析:
- FileSystem:从文件系统(硬盘目录)加载文件
- Xml:使用 XML 文件作为配置方式
- ApplicationContext:应用上下文实现
基本用法:
绝对路径,在任意目录下都可读取
ApplicationContext context = new FileSystemXmlApplicationContext("C:/config/applicationContext.xml");
Spring - Bean
Bean管理的配置文件方式
- id属性:Bean起名,采用ID约束,唯一
- 命名规则:
- 必须以字母开始;
- 可以使用字母、数字、连接符(-)、下划线(_)等;
- 不能出现特殊字符、空格、点号等。
- class属性:Bean对象的全路径
- scope属性:Bean的作用域
- singleton:单例,默认值,最常用的方式
- prototype:多例
- request:应用在Web项目中,每次HTTP请求都会创建一个新的Bean
- session:应用在Web项目中,同一个HTTP Session共享一个Bean
- init-method:初始化,当Bean被载入到容器的时候调用该属性指定的方法
- destroy-method:销毁,当Bean冲容器中删除的时候调用该属性指定的方法
- 单例的对象销毁:跟着容器工厂关闭才销毁
- 多例的对象销毁:垃圾回收机制进行回收
Bean对象实例化的三种方式
依然使用IOC入门程序中代码
方式一:无参数的构造方法(默认方法,最常用)
直接在applicationContext.xml配置文件中写Bean对象
<bean id="userService" class="com.qcby.service.impl.UserServiceImpl">
方式二:静态工厂实例化方法
在上述代码之外,在添加一个静态工厂类
package com.qcby.demo;import com.qcby.service.UserService;
import com.qcby.service.impl.UserServiceImpl;/*
实例化bean方式:
方式二:静态工厂实例化
*/
public class StaticFactory {/** 静态工厂方法* UserService:返回类型是接口* createUs:方法名*/public static UserService createUs(){System.out.println("通过静态工厂方式创建UserServiceImpi对象");// 返回接口类,不是具体实现类return new UserServiceImpl();}
}
applicationContext.xml配置文件中写Bean对象
<bean id="userService" class="com.qcby.demo.StaticFactory" factory-method="createUs">
- factory-method="createUs":指定要调用的静态工厂方法
当时三:实例工厂实例化方法
在上述代码之外,在添加一个实例工厂类
package com.qcby.demo;import com.qcby.service.UserService;
import com.qcby.service.impl.UserServiceImpl;/*
实例化bean方式:
方式三:实例工厂实例化*/
public class DemoFactory {// 动态工厂方式public UserService createUs(){System.out.println("实例工厂的方式");return new UserServiceImpl();}
}
applicationContext.xml配置文件中写Bean对象
<!--1. 创建工厂实例-->
<bean id="demoFactory" class="com.qcby.demo.DemoFactory"></bean>
<!--2. 通过工厂实例的方法创建bean-->
<bean id="dus" factory-bean="demoFactory" factory-method="createUs"></bean>
- factory-bean="demoFactory":指定Bean工厂
多配置文件方式
假设有两个配置文件:applicationContext.xml和applicationContext01.xml
有两种加载配置文件的方式:
方式一:直接在工厂创建时加载多个配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml","applicationContext01.xml");
方式二:在主配置文件中包含其它的配置文件
<import resource="applicationContext01.xml"/>
