一、理解内聚和耦合
怎样的架构的程序,我们认为是一个优秀的架构?
我们考虑的标准:可维护性好,可扩展性好,性能。
什么叫可扩展性好?
就是可以做到,不断的增加代码,但是可以不修改原来的代码的程序。
如何让程序的可维护性好,可扩展性好呢?
业界有一个公认的标准,高内聚,低耦合。
高内聚:就是尽量将代码写在与之功能描述一致的模块中。
架构设计时的内聚高低是指,设计某个模块或者关注点时,模块或关注点内部的一系列相关功能的相关程度的高低。
例如:下单模块:
一般情况下,下单模块都会有如下的信息,订单的信息,产品的信息及谁下的单(买家信息)。这是基本的,那么我们设计的时候就要把相关的功能内聚到一起
例如我们在设计数据库操作辅助类提供的方法有:
通过这样的方式,那么这个组件只负责数据库操作。
低耦合:就是尽量减少类与类之间的直接关系。(重点)
这是一个简单的低耦合的设计,电器与插座之间是低耦合的关系,就算我替换了不同的插座,电器依然可以正常的工作。因此简单的描述如下,就是A模块与B模块存在依赖关系,那么当B发生改变时,A模块仍然可以正常工作,那么就认为A与B是低耦合的。
这是一个简单的低耦合的设计,电器与插座之间是低耦合的关系,就算我替换了不同的插座,电器依然可以正常的工作。因此简单的描述如下,就是A模块与B模块存在依赖关系,那么当B发生改变时,A模块仍然可以正常工作,那么就认为A与B是低耦合的
二、Spring概述
1.Spring是什么
Spring是一个JavaEE轻量级的一站式开发框架。
JavaEE: 就是用于开发B/S的程序。(企业级)
轻量级:使用最少代码启动框架,然后根据你的需求选择,选择你喜欢的模块使用。
一站式:提供了表示层,服务层,持久层的所有支持。
2. Spring框架出现的背景
在世界第一套有Java官方Sun公司推出的企业级开发框架EJB出现后,瞬间风靡全球。被各大公司所应用。
Spring之父,Rod Jonhson是一个音乐博士,在Sun公司的大力推广下,也成为EJB框架的使用者。
在深入研究完EJB框架(由Sun主导开发的一个JavaEE开发框架),无法接收这么一个框架被吹成世界第一,
其中突出被他吐槽最厉害的一个点就EJB的重量级,就是只要使用EJB里面任何一个组件。都要将所有EJB的jar导进去。
于是他就提供了一个他的解决方案:轻量级的一站式企业开发框架。
那么什么是轻量级呢?
就是除内核模块(4个jar),其他模块由开发者自由选择使用,同时支持整合其他框架。
也可以称为就是可插拔式开发框架,像插头和插座一样,插上就用。这就是Spring框架核心理念。
那么什么是一站式呢?
就是Spring框架提供涵盖了JavaEE开发的表示层,服务层,持久层的所有组件功能。也就是说,原则上,学完一套Spring框架,不用其他框架就可以完成网站一条流程的开发。
如图:
3. 为什么要用spring
Spring可以帮助我们管理软件开发过程中的对象,以及如何创建和维护对象之间的关系
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,可以将组件的耦合度降至最低,即实现解耦,便于系统以后的维护和升级
3.1 spring的重要性
三层架构:
看图说话,看图总结? Spring 在三层架构的哪一层?
SpringMVC、stratus2、Servlet解决表现层问题,
Mybatis解决持久层问题,
那么Spring呢?解决什么问题?
Spring无可替代:dao提供支持、service提供事务支持、web提供了spring mvc框架支持等。
Spring是一个一站式的企业级(JavaEE)开发框架,意味着,仅仅使用一个Spring框架就可以满足JavaEE开发的表示层,服务层,持久层的开发。
Spring强调的理念是,轻量级。意味着Spring提供的功能模块,除了内核模块以外,开发人员可以选择性使用。
所以,Spring框架在现实开发中,主要的功能用于整合各种开发来开发项目。
4. Spring框架jar包
4.1. 框架包的下载
Spring官方提供的Maven下载。
但是基于简单入门的原则,我们要通过导入包的方式来学习。需要下载框架的zip包
路径为:http://repo.springsource.org/libs-release-local/org/springframework/spring/
4.2. 目录说明
–根目录
–类库规则
–包说明
包名 | 说明 |
---|---|
spring-aop-4.2.9.RELEASE.jar | 实现了AOP的支持 |
spring-aspects-4.2.9.RELEASE.jar | AOP框架aspects支持包 |
spring-beans-4.2.9.RELEASE.jar | 内核支撑包,实现了处理基于xml对象存取 |
spring-context-4.2.9.RELEASE.jar | 内核支撑包,实现了Spring对象容器 |
spring-context-support-4.2.9.RELEASE.jar | 容器操作扩展包,扩展了一些常用的容器对象的设置功能 |
spring-core-4.2.9.RELEASE.jar | 内核支撑包,Spring的内核 |
spring-expression-4.2.9.RELEASE.jar | 内核支撑包,实现了xml对Spring表达式的支持 |
spring-instrument-4.2.9.RELEASE.jar | 提供了一些类加载的的工具类 |
spring-instrument-tomcat-4.2.9.RELEASE.jar | 提供了一些tomcat类加载的的工具类,实现对应Tomcat服务的调用 |
spring-jdbc-4.2.9.RELEASE.jar | SpringJDBC实现包,一个操作数据库持久层的子框架 |
spring-jms-4.2.9.RELEASE.jar | 集成jms的支持,jms:Java消息服务。 |
spring-messaging-4.2.9.RELEASE.jar | 集成messaging api和消息协议提供支持 |
spring-orm-4.2.9.RELEASE.jar | ORM框架集成包,实现了Hibernate,IBatis,JDO的集成。 |
spring-oxm-4.2.9.RELEASE.jar | Spring OXM对主流O/X Mapping框架做了一个统一的抽象和封装。就是对应XML读写框架的支持 |
spring-test-4.2.9.RELEASE.jar | Spring集成JUnit测试 |
spring-tx-4.2.9.RELEASE.jar | 事务代理的支持 |
spring-web-4.2.9.RELEASE.jar | SpringWeb通用模块 |
spring-webmvc-4.2.9.RELEASE.jar | SpringMVC子框架 |
spring-webmvc-portlet-4.2.9.RELEASE.jar | Spring对门户技术(portlet)的支持 |
spring-websocket-4.2.9.RELEASE.jar | Spring对websocket的支持 |
三. Spring入门示例
1. 程序中耦合 概念理解
需求:使用Spring框架不用new创建一个对象。
2. 配置步骤说明
- 添加依赖
- 创建一个普通的类
- 创建一个Spring配置文件(通过xsd规则文件生成)
- 编写一个测试类,使用ApplicationContext的子类对象根据配置文件创建容器。并且在容器里面获得创建的对象
3. 配置步骤
3.1. 第一步:搭建环境
1.创建一个Maven项目
2.导入包,String的基础支撑包和依赖的日志包复制到lib文件下,并且加入项目中
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wbsLolaR-1597664042092)(assets/wpsc80.tmp.jpeg)]
加入核心依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
查看Maven依赖关系图
3.2. 第二步:创建配置文件
3.3. 第三步:创建对象到容器里面
- 创建一个类
public class HelloService { public void say(){ System.out.println("--你好世界!--"); } }
- 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"> <!-- bean:代表SpringIOC容器中的一个类 class:改类的全类名 --> <bean class="com.itdfbz.Service.HelloService" id="helloService"></bean> </beans>
- 测试使用getBean获得容器中的对象。
package com.itdfbz.spring; import com.itdfbz.Service.HelloService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring01 { @Test public void test1(){ //获取SpringIOC容器 ApplicationContext app= new ClassPathXmlApplicationContext("application.xml"); //根据name/id获取 HelloService helloService = (HelloService) app.getBean("helloService"); //根据字节码对象获取 // HelloService helloService = app.getBean(HelloService.class); helloService.sayHello(); } }
- 通过代码得到,Spring框架果然不用new就可以创建对象。
四. Spring容器
4.1容器
4.2容器的实例化
在Spring 容器读取Bean配置创建Bean实例之前,必须对它进行实例化。只有在容器实例化后,才可以从容器里获取Bean实例并使用。
Spring提供了多种类型的容器实现。
- BeanFactory:容器的基本实现。
- ApplicationContext:提供了更多的高级特性,是BeanFactory的子接口。
BeanFactory是Spring 框架的基础设施,面向Spring本身;ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非底层的BeanFactory 。
无论使用何种方式,配置文件是相同的。
ConfigurableApplicationContext扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close() ,让ApplicationContext具有启动、刷新和关闭上下文的能力。
ClassPathXmlApplicationContext:通过classpath路径直接获得加载的xml文件(推荐使用)
FileSystemXmlApplicationContext:通过文件路径来获得加载的xml文件。
注意:ApplicationContext在初始化上下文时就实例化所有单例的 Bean。
4.3. ApplicationContext类图结构图(了解)
通过在AbstractXmlApplicationContext类文件中ctrl+h和ctrl+alt+shit+u查看继承关系:
通过结构图可以看到,Spring容器顶级接口是BeanFactory,ApplicationContext是它的子接口。
六. 基于Spring框架的IoC实现
1. 说明需求
CustomerClient调用CustomerService的save()方法。将调用CustomerServiceImpl的对象实现的save()切换成调用CustomerServiceImpl2对象实现的save()。
注意:重点观察CustomerClient,切换过程中有没有修改该类的代码。
如果将CustomerClient调用的CustomerServiceImpl的对象修改为CustomerServiceImpl2的对象,而不用修改CustomerClient的代码。那么说明代码的调用权限从CustomerClient转移到了服务方。
6.3. 项目目录结构
6.4. 示例代码
CustomerService接口代码
public interface CustomerService {
/**
* 保存方法
*/
public void save();
}
CustomerServiceImpl子类
public class CustomerServiceImpl implements CustomerService {
@Override
public void save() {
System.out.println("-保存客户-CustomerServiceImpl");
}
}
CustomerServiceImpl2子类
public class CustomerServiceImpl2 implements CustomerService {
@Override
public void save() {
System.out.println("保存2");
}
}
CustomerClient类(调用方)
public class CustomerClient {
//1.声明一个父接口的引用
private CustomerService customerService;
//2.使用set方法注入对象,我们将通过方法注入的对象的方式称为依赖注入
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
public void save(){
//调用服务端的方法
customerService.save();
}
}
配置文件spring-config.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">
<!-- <bean>标签:用于声明一个类,在启动Spring框架的时候根据该配置的类创建对象到容器里面 name -->
<!-- <bean name="customerServiceImpl" class="cn.nyse.service.impl.CustomerServiceImpl"></bean> -->
<!--
CustomerServiceImpl修改为CustomerServiceImpl2的配置
-->
<bean name="customerServiceImpl" class="com.itdfbz.service.impl.CustomerServiceImpl2"></bean>
<bean name="customerClient" class="com.itdfbz.client.CustomerClient">
<!-- 对应set方法关联的对象 customerService
ref:关联对应的set方法,关联规则:xxx对应setXxx();如:customerService() 对应setCustomerService()
ref:指向容器中的对象
-->
<property name="customerService" ref="customerServiceImpl"></property>
</bean>
</beans>
测试代码
package com.itdfbz.app;
import com.itdfbz.client.CustomerClient;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientTest {
@Test
public void save(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
CustomerClient customerClient = context.getBean("customerClient", CustomerClient.class);
//调用方法
customerClient.save();
}
}
七. 配置标签
1. bean标签的配置
1.1. bean标签作用
用于声明一个类,在启动Spring框架的时候根据该配置的类创建对象到容器里面
1.2. 属性说明
<!-- <bean>标签:用于声明一个类,在启动Spring框架的时候根据该配置的类创建对象到容器里面
name:设置对象名(唯一标识符)
id:设置对象名(唯一标识符,功能和name一样)
class:用于指定对象对应的类名
scope:用于设置的对象的作用范围,可选参数如下:
*singleton:单例(默认)
对象出生:当程序加载配置文件创建容器时,创建
对象活着:只要容器还在,一直活着
对象死亡:应用停止,容器销毁,对象死亡
*prototype:多例(原型对象)
对象出生:当程序加载配置文件创建容器后,通过getBean调用的时候,创建
对象活着:只要对象被使用,一直活着
对象死亡:对象长时间不用,会被Java垃圾回收机制回收
*reqeust:web项目中,代表请求一次创建一次此对象
*session:web项目中,代表创建一次回话创建一次此对象
*globalSession:web项目中,应用域集群环境,如果没有集群环境相当于session
init-method:设置创建对象的时候,调用初始化方法
destroy-method:设置对象被回收时,调用注销的方法
-->
<bean class="com.itdfbz.entity.Customer" scope="singleton"></bean>
1.3. Bean作用范围
在 Spring 中, 可以在 元素的 scope 属性里设置 Bean 的作用域.
默认情况下, Spring 只为每个在 IoC 容器里声明的 Bean 创建唯一一个实例, 整个 IoC 容器范围内都能共享该实例:所有后续的 getBean() 调用和 Bean 引用都将返回这个唯一的 Bean 实例.该作用域被称为 singleton(单例模式), 它是所有 Bean 的默认作用域.
<?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 name="customerDAOImpl" class="cn.nyse.dao.impl.CustomerDAOImpl"></bean>
<!--
原型对象:prototype
单例:singleton(默认)
-->
<bean name="customerService" scope="singleton"class="cn.nyse.service.CustomerService">
<property name="customerDAO" ref="customerDAOImpl"></property>
</bean>
<!-- <bean name="customerService" scope="prototype" class="cn.nyse.service.CustomerService"> -->
<!-- <property name="customerDAO" ref="customerDAOImpl"></property> -->
<!-- </bean> -->
</beans>
1.4 Bean延时实例化
在ApplicationContext实现的默认行为就是在启动时候将所有singleton bean 进行实例化,如果不想启动时实例化,可以使用元素的lazy-init属性改变
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4MnOtzoo-1597664042095)(assets/clipboard11.png)]
1.5 Bean生命周期
Spring IoC 容器可以管理 Bean 的生命周期, Spring 允许在 Bean 生命周期的特定点执行定制的任务.
Bean的生命周期
—指定初始化方法
—指定销毁方法,仅适用于singleton模式
Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理.(了解)
Bean 后置处理器对 IoC 容器里的所有 Bean 实例逐一处理, 而非单一实例. 其典型应用是: 检查 Bean 属性的正确性或根据特定的标准更改 Bean 的属性.
对Bean 后置处理器而言, 需要实现
接口. 在初始化方法被调用前后, Spring 将把每个 Bean 实例分别传递给上述接口的以下两个方法:
2. 实例化Bean的三种方式
2.1. 通过class直接创建
<bean name="customerService" class="cn.nyse.service.CustomerService">
- 1
2.2. 通过静态方法工厂创建(了解)
–静态工厂类
public class CreateFactory {
/**
* 使用一个静态工厂类,通过字符串创建一个对象
*
* @param className
* @return
*/
public static Object create() {
return new CustomerService();
}
}
–静态工厂配置
<bean name="customerService" factory-method="create" class="cn.nyse.factory.CreateFactory">
<property name="customerDAO" ref="customerDAOImpl"></property>
</bean>
2.3. 通过实体工厂创建(了解)
–实体工厂,注意create方法没有static
public class CreateFactory {
/**
* 使用一个静态工厂类,通过字符串创建一个对象
*
* @param className
* @return
*/
public Object create() {
return new CustomerService();
}
–配置方式
<?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 name="customerDAOImpl" class="cn.nyse.dao.impl.CustomerDAOImpl"></bean>
<!--非静态的方法,必须需要对象来调用,所以必须要创建工厂类的对象 -->
<bean name="createFactory" class="cn.nyse.factory.CreateFactory" ></bean>
<bean name="customerService" factory-method="create" factory-bean="createFactory" >
<property name="customerDAO" ref="customerDAOImpl"></property>
</bean>
</beans>
八. Spring依赖注入
DI全称是Dependency Injection,基本原理是将一起工作具有关系的对象,通过构造方法参数或者方法参数传入建立关联 ,容器的工作就是创建bean时注入那些依赖关系
依赖:某个类以局部变量的形式出现在另一个类中,二者是依赖关系
假如A类的某个方法中,使用了B类,那么就说A类依赖于B类,它们是依赖关系。
A类的某个方法使用B类,可能是方法的参数是B类,也可能是在方法中获得了一个B类实例。但无论是哪种情况,B类在A类中都是以局部变量的形式存在的。
因此,A类中有B类型的局部变量,就说A类依赖于B类。
DI主要有二种注入方式,setter注入构造器注入和构造器注入
spring中的DI:原来通过set方法或者构造方法给属性赋值,现在由spring来完成调用注入
1. set方法注入
–类代码
package com.itdfbz.entity;
import java.util.Date;
public class User {
private int age;
private String name;
private Date birthDate;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public void register(){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
System.out.println("生日:"+birthDate);
}
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
", birthDate=" + birthDate +
'}';
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="xxx" class="java.util.Date"></bean>
<bean name="user" class="com.itdfbz.entity.User">
<property name="age" value="20"></property>
<property name="name" value="xiaobiao"></property>
<property name="birthDate" ref="xxx"></property>
</bean>
</beans>
2. 构造方法注入
–类的实现
public class User {
private int age;
private String name;
private Date birthDate;
public User() {
}
public User(int age, String name, Date birthDate) {
this.age = age;
this.name = name;
this.birthDate = birthDate;
}
}
–对应配置文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userBirthday" class="java.util.Date"></bean>
<bean id="user" class="com.itdfbz.entity.User">
<!--
name:形参名称
value:值
ref:引用类型值
-->
<!--
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="birthDate" ref="userBirthday"></constructor-arg>
<constructor-arg name="name" value="xiaozhun"></constructor-arg>
-->
<!--
index:形参索引
-->
<constructor-arg index="0" value="20"></constructor-arg>
<constructor-arg index="1" value="xiaofang"></constructor-arg>
<constructor-arg index="2" ref="userBirthday"></constructor-arg>
</bean>
</beans>
3. 注入集合数据
在处理的数据中,
有标量类型=基础数据类型以及包装类+String – value属性
也有Spring容器里面的对象 –ref属性
还要很多数据JDK内置的数据结构:
- 键值对 Map 、Properties
- 数组
- 集合Set、List
–类代码
package com.itdfbz.entity;
import java.util.*;
public class Pojo {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,Object> myMap;
private Properties myProps;
public String[] getMyStrs() {
return myStrs;
}
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public List<String> getMyList() {
return myList;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public Set<String> getMySet() {
return mySet;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public Map<String, Object> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, Object> myMap) {
this.myMap = myMap;
}
public Properties getMyProps() {
return myProps;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
@Override
public String toString() {
return "Pojo{" +
"myStrs=" + Arrays.toString(myStrs) +
", myList=" + myList +
", mySet=" + mySet +
", myMap=" + myMap +
", myProps=" + myProps +
'}';
}
}
–配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="pojo" class="com.itdfbz.entity.Pojo">
<!--数组属性注入-->
<property name="myStrs">
<array>
<value>111</value>
<value>222</value>
<value>333</value>
</array>
</property>
<!--集合属性注入-->
<property name="myList">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<!--set属性注入-->
<property name="mySet">
<set>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</set>
</property>
<!--map集合注入-->
<property name="myMap">
<map>
<entry key="a" value="1"></entry>
<entry key="b" value="2"></entry>
<entry key="c" value="3"></entry>
</map>
</property>
<!--properties属性集合注入-->
<property name="myProps">
<props>
<prop key="name">张三</prop>
<prop key="address">广州</prop>
<prop key="age">20</prop>
</props>
</property>
</bean>
</beans>
注意事项:集合与数组的标签可以互相调换是使用的。但是不建议调换使用
九. 获得Properties文件的值
1. 说明
Spring配置文件支持通过Properties文件的Key获得对应的值。实现该功能是通过
PropertySourcesPlaceholderConfigurer指定Properties的路径
通过${Key}来获得Properties文件对应Key的值
2. 示例代码
2.1. 第一步:创建项目导入包
图略
2.2. 第二步:编写一个普通的CustomerService类
package com.itdfbz.entity;
import java.util.Date;
public class User {
private int age;
private String name;
private Date birthDate;
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public void register(){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
System.out.println("生日:"+birthDate);
}
}
2.3. 第三步:编写Properties文件键值对
修改IDEA默认配置文件编码
注意:使用utf-8编码
user.age = 18
user.name =小标
2.4. 第四步:编写配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="now" class="java.util.Date"></bean>
<bean name="user" class="com.itdfbz.entity.User">
<property name="birthDate" ref="now"></property>
<property name="name" value="${user.name}"></property>
<property name="age" value="${user.age}"></property>
</bean>
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:user.properties"></property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
</beans>
2.5. 第五步:测试
–测试结果,可以获得Properties文件的值,测试成功
十. 总结
- 如何评判一个程序的架构是好的架构
答:可维护性好,可扩展性好,性能好(算法)
- 如何能实现程序的可维护性好,可扩展性好呢?
答:业界有个公认的标准,高内聚(分类),低耦合。
低耦合:尽量的减少类与类之间的直接关联的关系。
Spring是通过Ioc(控制反转)的理念实现解耦的。
如果要实现Ioc的理念,必须要做到不能使用new就可以创建对象。Spring必须要实现不能new就可以创建对象。
我们将这种Spring实现的不用new就可以创建对象的方式并指定属性的方式为 依赖注入(DI)
所以我们学习Spring必须要理解IOC。
使用Ioc解耦有什么用呢?
如果符合Ioc的结构理念,可以让程序实现可插拔。(维护性好,扩展性好体现)
Spring的可插拔,除了内核的四个包,其他功能模块,随意让开发人员选择使用。
控制反转与依赖(IoC)注入的关系(DI)
控制反转:就是实现将代码的控制权(调用权)从调用方转移到被调用方
依赖注入:不用new 就可以创建对象并给予属性值
控制反转的实现=依赖注入+面向接口的编程思想
作者:易兮科技 | 来源:https://blog.csdn.net/m0_47157676