一、Spring Framework
1.1 简介
- 2002年,首次推出了Spring框架的雏形:interface21框架
- Spring Framework创始人Rod Johnson,一位音乐学专业的博士
- Spring框架理念,简化java开发
官网:https://spring.io maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
1.2 优点
- Spring是一个开源免费的框架
- Spring是一个轻量级、非侵入式的框架
- 控制反转(IOC),面向切面编程
- 支持事务的处理,支持对各种框架的整合支持
1.3 缺点
- 配置繁琐
1.4 Spring组成
二、IOC
2.1 理论推导
查看以下代码
public class UserService{
private UserDao userDao=new UserDao();
}
使用set接口实现
public class UserService{
private UserDao userDao;
//利用set进行动态值的注入
public void setUserDao(UserDao userDao){
this.userDao=userDao;
}
}
之前的代码,程序是主动创建对象,控制权在程序员手上。使用set注入之后,程序不再具有主动性,而变成被动接受对象。这种控制权的转变就是控制反转
2.2 IOC本质
控制反转IOC,是一种设计思想,DI是实现IOC的一种方法;也有人认为DI是IOC的另外一种说法。
没有IOC的程序中,我们使用面向对象的编程,对象的创建与对象之间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制。控制反转后将对象的创建转移给第三方
- 采用XML方式配置Bean的时候,Bean的定义信息和实现是分离的
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
<?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">
<!--使用Spring来创建对象,在Spring中这些称之为Bean -->
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
- 采用注解的方式可以把两者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到零配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
public String name;
@Value("dabendan")
public void setName(String name){
this.name=name;
}
}
2.3 IOC创建对象的方式
1、使用无参构造创建对象,默认
2、使用有参构造创建对象
1)下标赋值
<bean id ="user" class="com.kuang.pojo.User">
<constructor-arg index="0" value="狂神说Java"/>
</bean>
2)类型
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg type="java.lang.String" value="qingjiang"/>
</bean>
3)通过参数名来设置
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg name="name" value="qingjaing"/>
</bean>
三、Spring配置
3.1别名
<alias name="user" alias="userNew"/>
3.2 Bean的配置
<!--
id :bean的唯一标识符,也就是相当于我们学的对象名
class : bean 对象所对应的全限定名:包名+类型
name : 也是别名,而且name 可以同时取多个别名
-->
<bean id ="userT" class="com.kuang.pojo.userT" name="user2 u2 u3">
<property name="name" value="西部开源"/>
</bean>
3.3 import
import一般用于团队开发使用,它可以将多个配置文件,导入合并一个。假设这个项目有多个人开发,这三个人负责不同类的开发,我们可以利用import将所有人的beans.xml合并为一个总的
- applicationContext.xml
<import resource="beans.xml"/>
<import resource="beans2.xml"/>
<import resource="beans3.xml"/>
四、依赖注入
4.1 构造器注入
4.2 set方法注入
依赖注入:set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
1、复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
2、真实测试对象
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
3、beans.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 id="address" class="com.kuang.pojo.Address">
<property name="address" value="中国"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!-- 普通注入-->
<property name="name" value="笨蛋"/>
<!-- bean注入-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<value>西哟及</value>
<value>水浒传</value>
<value>三国</value>
</array>
</property>
<!-- list-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>网游戏</value>
<value>看书</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="455555555555555"/>
<entry key="银行卡" value="jjjjjj"/>
</map>
</property>
<property name="games">
<set>
<value>王者</value>
<value>LOL</value>
<value>BOB</value>
</set>
</property>
<property name="wife">
<null></null>
</property>
<property name="info">
<props>
<prop key="学号">20190525</prop>
<prop key="姓名">耳机</prop>
</props>
</property>
</bean>
</beans>
4、测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student)context.getBean("student");
System.out.println(student.toString());
}
}
4.3 拓展方式注入
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p命名空间的注入,可以直接注入属性的值:property-->
<bean id="user" class="com.kuang.pojo.User" p:name="qingjiang" p:age="18"/>
<!-- c命名空间的注入,通过构造器注入:construct-arg-->
<bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="qingjiang"/>
注意:p命名和c命名不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
4.4 Bean的作用域
1、单例模式
<bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="qingjaing"
scope="singleton"/>
2、原型模式
<bean id="accountService" class="com.something.DefaultService" scope="prototype"/>
五种作用域中,request、session和global session三种作用域仅在基于web的应用中使用
五、Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配方式
1、在xml中显示配置
2、在Java中显示配置
3、隐式的自动装配bean
5.1 基于xml的自动装配
ByName自动装配
<bean id="people" class="com.kuang.pojo.People" autowire="byName">
<property name="name" value="qinjang"/>
</bean>
ByType自动装配
<bean class="com.kuang.pojo.Cat"/>
<bean class="com.kuamg.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People" autowire="byType">
<property name="name" value="qingjaing" />
</bean>
5.2 基于注解的自动装配
1、导入约束
2、配置注解支持
<?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">
<!-- 开启注解支持-->
<context:annotation-config/>
</beans>
@Autowired
直接在属性上使用即可,也可以在set方法上使用。此注解是按照ByType方式自动装配的
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解@Autowired完成的时候,我们可以使用@Qualifier(value=“xx”)去配置@Autowired使用,指定一个唯一的bean对象注入。这两个注解一起使用等价于ByName装配
@Resource
public class People{
@Resource(name = "cat2")
private Cat cat;
@Resource
private Dog dog;
}
@Resource默认是通过ByName方式实现,如果找不到名字,则通过ByType来实现,如果两个都找不到,则报错
六、使用注解开发
@Component
public class User {
public String name;
@Value("dabendan")
public void setName(String name){
this.name=name;
}
}
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层
- dao [@Repository]
- service [@Service]
- controller [@Controller]
这四个注解功能都是一样的,都是代表将某个类注入到Spring中
@Scope注解可以标注当前类的作用域
小结:
xml与注解:
- xml更加万能,适用于任何场合,维护简单方便
- 注解不是自己的类使用不了,维护相对复杂
xml与注解最佳实战
- xml用来管理bean
- 注解只负责完成属性的注入
- 要想让注解生效,必须开启注解支持
<context:component-scan base-package="com.kuang"/>
<context:annotation-config/>
七、使用Java配置Spring
@Configuration
@ComponentScan("com.kuamg.pojo")
@Import(KuangConfig.class)
public class KuangConfig{
/**
注册一个bean
方法的名字,相当于bean标签中的id属性
方法的返回值,相当于bean标签中的class属性
*/
@Bean
public User user(){
return new User(); //返回要注入的bean对象
}
}
public class MyTest{
public static void main(String[] args){
ApplicationContext context=new AnnotationConfigApplicationContext(KuangConfig.class);
User getUser=(User)context.getBean("user");
System.out.println(getUser.getName());
}
}
八、代理模式
代理模式分类:
- 静态代理
- 动态代理
8.1 静态代理
角色分析:
- 抽象角色:一般会使用接口或者抽象类来解决
- 真实角色:被代理的角色
- 代理角色:代理真实角色,一般代理角色会增加一些附属操作
- 客户:访问代理对象的人
接口
//租房
public interface Rent{
public void rent();
}
真实角色
//房东
public class Host implements Rent{
public void rent(){
System.out.println("房东要出租房子")
}
}
代理角色
//中介
public class Proxy implements Rent{
private Host host;
public Proxy(){}
public Proxy(Host host){
this.host=host;
}
public void rent(){
seeHouse();
host.rent();
hetong();
fare();
}
//看房
public void seeHouse(){
System.out.println("中介带你看房")
}
//签合同
public void hetong(){
System.out.println("签租赁合同")
}
//收中介费
public void fare(){
System.out.println("收中介费")
}
客户
public class Client{
public static void main(String[] args){
Host host=new Host();
Proxy proxy=new Proxy(host);
proxy.rent();
}
}
代理模式的好处:
- 可以使真实角色的操作更加纯粹,不用去关注一些公共个业务
- 公共的业务交给代理角色,实现业务分工
- 公共的业务发生扩展的时候,方便集中管理
缺点:
- 一个真实的角色就会产生一个代理角色,代码量会翻倍
8.2 动态代理
- 动态代理和静态代理角色一样
- 动态代理的代理类是动态生成的
- 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
基于接口 ----JDK的动态代理
基于类------ cglib
九、AOP
9.1 什么是AOP
面向切面编程,通过预编译的方式和运行期动态代理实现程序功能同一维护的技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中一个重要的内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使业务逻辑各部分之间的耦合度降低,提高程序的可用性,同时提高了开发效率
9.2 AOP在Spring中的作用
提供声明式事务,允许用户自定义切面
- 横切关注点:跨越应用程序多个模块的方法或者功能。与业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志、安全、缓存、事务
- 切面:横切关注点被模块化的特殊对象,它是一个类
- 通知:切面必须要完成的工作,它是类中的一个方法
- 目标:被通知的对象
- 代理:向目标对象应用通知之后创建的对象
- 切入点:切面通知执行地点的定义
- 连接点:与切入点匹配的执行点
SpringAop中,通过Advice定义的横切逻辑,Spring支持5种类型的Advice
AOP在不改变原有代码的情况下,去增加新的功能
9.3 Spring实现AOP
方式一:使用Spring的API接口
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
方式二:自定义实现AOP
public class DiyPointCut {
public void before(){
System.out.println("======方法执行前======");
}
public void after(){
System.out.println("======方法执行后======");
}
}
方式三:使用注解实现
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.kuang.service.UserServiceImpl.* (..))")
public void before(){
System.out.println("========方法执行前");
}
@After("execution(* com.kuang.service.UserServiceImpl.* (..))")
public void after(){
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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解支持-->
<context:annotation-config/>
<bean id="userService" class="com.kuang.service.UserServiceImpl"/>
<bean id="log" class="com.kuang.log.Log"/>
<bean id="afterLog" class="com.kuang.log.AfterLog"/>
<!--配置Aop方式一-->
<aop:config>
<!--切入点 expression:表达式 execution(要执行的位置) -->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!-- 执行环绕接口-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
<!-- 方式二-->
<!--<bean id="diy" class="com.kuang.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.* (..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>-->
<!-- 方式三-->
<bean id="annotationPointCut" class="com.kuang.diy.AnnotationPointCut"/>
<aop:aspectj-autoproxy/>
</beans>
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}