1.Spring框架
1.1 Spring框架的介绍
1.1.1 Spring框架的概述
Spring是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
1.1.2 Spring框架的优点
- 方便解耦,简化开发,Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理。IOC的作用。
- AOP编程的支持,Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能。(可扩展性)
- 声明式事务的支持,只需要通过配置就可以完成对事务的管理,而无需手动编程。
- 方便程序的测试,Spring对Junit4支持,可以通过注解方便的测试Spring程序。
- 方便集成各种优秀框架,Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持。
- 降低JavaEE API的使用难度,Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。
1.2 Spring的IOC核心技术
1.2.1 什么是IOC
IOC – Inverse of Control,控制反转,将对象的创建权力反转给Spring框架!!
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。
解决问题:使用IOC可以解决的程序耦合性高的问题!!Spring的工厂读取配置文件。
1.2.2 具体实现
核心配置文件: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">
<!--把UserServiceImpl 交给Spring的IOC容器去管理,Spring管理对象的类叫作bean-->
<bean id = "us" class = "com.qcby.service.UserServiceImpl"></bean>
</beans>
接口与实现类:
package org.example.service;
public interface UserService {
public void hello();
}
package org.example.service.impl;
import org.example.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void hello() {
System.out.println("Hello IOC!!!");
}
}
测试类:
package org.example.test;
import org.example.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {
@Test
public void run1(){
// 使用Spring的工厂,读取配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// 通过工厂获取类,getBean+配置文件中的bean的id
UserService userService = (UserService) applicationContext.getBean("userService");
System.out.println(userService);
userService.hello();
}
}
测试运行:
1.2.3. IOC技术总结
ApplicationContext接口,工厂的接口,使用该接口可以获取到具体的Bean对象。该接口下有两个具体的实现类。 springCould 配置中心
ClassPathXmlApplicationContext,加载类路径下的Spring配置文件。
FileSystemXmlApplicationContext,加载本地磁盘下的Spring配置文件。
1.2.4. Spring框架的Bean管理的配置文件方式
- id属性,Bean起个名字,在约束中采用ID的约束,唯一,取值要求:必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号 id:不能出现特殊字符。
- class属性,Bean对象的全路径。
- scope属性,scope属性代表Bean的作用范围。//作用域
- singleton 单例(默认值),最常用的方式。IOC容器中只会存在一个实例,配置文件一加载对象就会被创建。
- prototype 多例。每从IOC容器中获取对象,才会创建实例
- request 应用在Web项目中,每次HTTP请求都会创建一个新的Bean
- session 应用在Web项目中,同一个HTTP Session 共享一个Bean
- Bean对象的创建和销毁的两个属性配置。说明:Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法
- init-method,当bean被载入到容器的时候调用init-method属性指定的方法
- destroy-method,当bean从容器中删除的时候调用destroy-method属性指定的方法
单例与多例的区别:
- 单例的对象销毁:跟着容器工厂的关闭才销毁
- 多例的对象销毁:垃圾回收机制进行回收的
接下来进行验证:
UserServiceImpl方法:
package com.qcby.service;
public class UserServiceImpl implements UserService{
/**
* 接口中的方法实现
*/
public void hello(){
System.out.println("Hello IOC!!!");
}
public static void init(){
System.out.println("创建方法,init-method!!!");
}
public static void destory(){
System.out.println("销毁方法,destory-method!!!");
}
}
run1方法:
/**
* Spring创建
* 耦合度低
*/
@Test
public void run1(){
//Spring的工厂对象的方法 bean工厂
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("===============");
// 工厂对象的方法
UserService us1 = (UserService) ac.getBean("us");
UserService us2 = (UserService) ac.getBean("us");
System.out.println(us1);
System.out.println(us2);
}
singleton 单例:
applicationContext.xml核心:
<bean id = "us" class = "com.qcby.service.UserServiceImpl" init-method="init" destroy-method="destory"/>
运行结果:
prototype 多例:
applicationContext.xml核心:
<bean id = "us" scope="prototype" class = "com.qcby.service.UserServiceImpl" init-method="init" destroy-method="destory"/>
运行结果:
1.2.5 实例化Bean对象的三种方式
- 默认是无参数的构造方法(默认方式,基本上使用)
<bean id="us" class="com.qcby.service.UserServiceImpl" />
- 静态工厂实例化方式
/**
* 静态工厂方式
*/
public class StaticFactory {
// 静态工厂方式
public static UserService createUs(){
System.out.println("通过静态工厂的方式创建UserServiceImpl对象...");
// 编写很多业务逻辑 权限校验
return new UserServiceImpl();
}
}
<bean id="us" class="com.qcby.StaticFactory" factory-method="createUs" />
- 实例工厂实例化方式
/**
*
* 动态工厂方式
*
*/
public class Dfactory {
public UserService createUs(){
System.out.println("实例化工厂的方式...");
return new UserServiceImpl();
}
}
<bean id="dfactory" class="com.qcby.Dfactory" />
<bean id="us" factory-bean="dfactory" factory-method="createUs" />
1.3 DI依赖注入
- IOC和DI的概念
- IOC:Inverse of Control,控制反转,将对象的创建权反转给Spring!!
- DI:Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中!!
- 不论是使用Setter还是Constructor,在类中必须实现Setter或者构造器方法
1.3.1 Set注入
持久层(Dao):
业务层(Service):
xml文件:
运行结果:
1.3.2 构造器注入
xml文件:
测试代码:
运行结果:
1.3.3 数组、集合、properties
java文件:
package com.qcby.DI注入.Others;
import java.util.*;
public class CollectionBean {
private String[] strs;
private List<String> list;
private Map<String, String> map;
private Properties properties;
public void setStrs(String[] strs) {
this.strs = strs;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "CollectionBean{" +
"strs=" + Arrays.toString(strs) +
", list=" + list +
", map=" + map +
", properties=" + properties +
'}';
}
}
xml文件:
<!--集合的注入-->
<bean id="collection" class="com.qcby.DI注入.Others.CollectionBean">
<!--数组的注入-->
<property name="strs">
<array>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</array>
</property>
<!--List的注入-->
<property name="list">
<list>
<value>熊大</value>
<value>熊二</value>
</list>
</property>
<!--map的注入-->
<property name="map">
<map>
<entry key="name" value="zhangsan"/>
<entry key="age" value="20"/>
</map>
</property>
<!--properties的注入-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="paaword">123456</prop>
</props>
</property>
</bean>
1.4 多配置文件方式
在src的目录下又多创建了一个配置文件,现在是两个核心的配置文件,那么加载这两个配置文件的方式有两种!
- 主配置文件中包含其他的配置文件:
<import resource="applicationContext2.xml"/>
2. 工厂创建的时候直接加载多个配置文件:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");