Spring详细解读

spring框架

1.Spring框架是一个轻量级的开源的JavaEE的容器框架。

2.Spring 可以解决企业级应用开发的复杂性。

3.Spring有两个核心部分:IOC和AOP。

IOC:控制反转

把创建对象的过程交给Spring进行管理

(1)IOC概念原理

概念:
把对象创建和对象之间的调用过程,都交给spring来管理
为了降低耦合度

底层:
Xml解析、工厂模式、反射

工厂模式:
降低了两个类之间的耦合度
Ioc过程 (进一步的降低耦合度)
在这里插入图片描述

(2)IOC接口(beanFactory)

1.IOC思想基于IOC容器完成,IOC容器底层就是对象工厂。
2.Spring提供IOC容器实现两种方式:(两个接口)
(1)BeanFactory:IOC容器基本实现,是Spring内部使用的接口,不推荐开发人员进行使用
①加载配置文件的时候不会创建里边的对象,而是在你获取或者适用对象的时候才会创建。
②加载配置文件的时候就会直接加载里边配置的对象。
(2)ApplicationContext:BeanFactory接口的子接口,提供了更多更强大的功能,一般由开发人员
进行使用。
(3)ApplicationContext接口有实现类(如下图)
在这里插入图片描述
File:盘符路径全路径
Class:项目里边的路径src路径

(3)IOC操作Bean管理

①Bean管理指的是两个对象
②Spring创建对象
③Spring注入对象

(4)Bean管理操作有两种方式

1.基于xml来创建对象

<bean id="user" class="com.xiao.spring.pojo.User"></bean>

在spring配置文件中,使用bean标签添加对应的属性,就可以实现创建。创建对象的时候也是默认执行无参构造方
法来完成对象的创建。
2.基于xml来注入属性
DI:依赖注入,就是注入属性
第一种方式就是使用set方法进行注入(创建一个类和对应的set方法通过xml配置文件注入)

<!--    set方法注入属性-->
    <bean id="user" class="com.xiao.spring.pojo.User">
<!--        使用property完成属性注入-->
<!--            name:类里边的属性名称-->
<!--            value:想属性注入的值-->
        <property name="name" value=""/>
    </bean>

第二种使用有参构造(在new对象的时候就直接给他值)进行注入通过xml配置文件用有参构造注入属性

<!--    有参构造注入属性-->
    <bean id="user" class="com.xiao.spring.pojo.User">
        <constructor-arg  name="name" value="你好"></constructor-arg>
        <constructor-arg  name="address" value="河南"></constructor-arg>
    </bean>

第三种就是通过p命名空间来注入

<?xml version="1.0" encoding="UTF-8"?>
<!--xmlns:p="http://www.springframework.org/schema/p" 在上边导入p命名空间 -->
<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" class="com.xiao.spring.pojo.User" p:name="你好">
    </bean>
</beans>
(5)IOC操作Bean管理(xml注入其他类型属性)

1.字面量
向属性里边注入null值

<bean id="user" class="com.xiao.spring.pojo.User">
        <property name="name">
            <null/>
        </property>
    </bean>

项属性里边注入特殊符号

<bean id="user" class="com.xiao.spring.pojo.User">
     <property name="address">
         <value><![[<<中国>>]]></value>
     </property>
 </bean>

2.注入属性-外部Bean
创建两个类service和dao’类 在servicee里边调用dao的方法

<!-- service和dao的对象创建-->
<bean id="userService" class="com.xiao.spring.service.UserService">
<!--向userService对象中注入Dao对象-->
        <property name="userDao" ref="userSDaoImpl"></property>
    </bean>
<bean id="userSDaoImpl" class="com.xiao.spring.service.UserDaoImpl"></bean>

3.注入属性-内部Bean和级联赋值

<bean id="userService" class="com.xiao.spring.service.UserService">
        <property name="name" value=""></property>
        <property name="dept">
            <bean name="dept" class="com.xiao.spring.pojo.Dept">
                <property name="dname" value="部门"></property>
            </bean>
        </property>
</bean>

4.注入集合
数组
list集合
map集合
set集合

<bean id="user" class="com.xiao.spring.pojo.User">
        <property name="courses">
            <array>
                <value></value>
                <value></value>
            </array>
        </property>
    </bean>
    <property name="list">
        <list>
            <value></value>
            <value></value>
        </list>
    </property>
    <property name="maps">
        <map>
            <entry key="" value=""></entry>
            <entry key="" value=""></entry>
        </map>
    </property>
    <property name="sets">
        <set>
            <value></value>
            <value></value>
        </set>
    </property>	

向list集合中注入对象

<!--向list集合中注入对象-->
    <bean id="user" class="com.xiao.spring.pojo.User">
        <property name="list">
            <list>
                <ref bean=""></ref>
                <ref bean=""></ref>
            </list>
        </property>
    </bean> 
    <!--创建多个对象-->
    <bean id="list1" class="com.xiao.spring.pojo.Student">
        <property name="sname" value="同学"></property>
    </bean>
    <bean id="list2" class="com.xiao.spring.pojo.Student">
        <property name="sname" value="学习"></property>
    </bean>
(6)IOC操作Bean管理(bean作用域)

1.在Spring里边,设置创建bean实例是单例还是多实例
2.在Spring里边,默认情况是单例。
3.设置单例多实例(scope)
第一个值,默认值,singleton,表示是一个单例
第二个值 prototype 多实例

<!--    单实例-->
    <bean id="user" class="com.xiao.spring.pojo.User" scope="singleton">
        <property name="list" ref="nihaoList"></property>
    </bean>
<!--    多实例-->
    <bean id="user" class="com.xiao.spring.pojo.User" scope="prototype">
        <property name="list" ref="nihaoList"></property>
    </bean>

singleton和prototype 的区别
Singleton 单实例 prototype 多实例
设置scope值是singleton时候,加载spring配置文件的时候就会创建单实例对象
设置scope的值是prototype时候,不是在加载spring配置文件的时候创建对象的,是在调用getBean方法的时候创建多实例对象的

(7)bean的声明周期

从对象的创建到销毁的过程

步骤
1.通过构造器创建bean实例(无参的构造)
2.为bean的属性设置值和对其他bean的引用(调用set方法)
3.调用bean的初始化方法
4.Bean就可以使用了
5.当容器关闭的时候调用bean的销毁的方法

Bean的后置处理器
1.通过构造器创建bean实例(无参数构造)
2.为bean的属性设置值和对其他bean引用(调用set方法)。
3.把bean实例传递bean后置处理器的方法.
4.调用bean的初始化的方法(需要进行配置初始化的方法)。
5.把bean实例传递bean后置处理器的方法工
6.bean可以使用了(对象获取到了)。
7.当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)
配置后置处理器

<bean id="myBeanPost" class="org.springframework.beans.factory.config.BeanPostProcessor"></bean>
(8)xml中自动装配

1.什么是自动装配
(1)根据指定装配规则(属性名或者属性类型),spring自动将匹配的属性值进行注入
(2)演示自动装配过程
a.根据属性的名称自动注入(byName)
b.根据类型注入(byType)

<!--    实现自动装配
            bean标签属性autowired,配置自动装配
            autowired属性常用的两个值
               byName:根据属性的名称自动注入
               byType: 根据类型注入    -->
    <bean id="emp" class="com.xiao.spring.spring.autowire.Emp" autowire="byType">

    </bean>
(9)引入外部的属性文件

1.直接配置数据库信息
1)配置德鲁伊连接池

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/xxx"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>

2.通过引入外部的配置数据库文件
1)创建一个外部的数据库信息文件properties

prop.driverClassName=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/xxx
prop.userName=root
prop.password=root

2)把外部的properties引入到spring配置文件中
引入名称空间context
在这里插入图片描述

引入外部配置文件
配置连接池

		<!--    将外部属性文件引入-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--    配置数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClassName}"></property>
        <property name="url" value="${prop.urlr}"></property>
        <property name="username" value="${prop.userName}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>
(10)IOC操作Bean注解方式

Spring针对Bean管理中创建对象提供的注解
@Component
@Service
@Controller
@Repository
上边的注解功能都是一样的,都可以用来创建Bean实例
基于注解创建对象
1.引入依赖 (aop)
2.开启组件扫描(在配置文件中开启)
①引入context 命名空间②开启组件扫描

<context:component-scan base-package="com.xiao.spring"></context:component-scan>

③创建类 添加注解
3.自定义扫包
设置扫扫描那些内容

 <context:component-scan base-package="com.xiao.spring">
<!--        设置只扫描带Controller注解的类-->
        <context:include-filter type="xxxx" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

设置不扫描哪些内容

    <context:component-scan base-package="com.xiao.spring">
        <!--        设置不扫描带Controller注解的类 其余的全部扫描-->
        <context.exclude-filter type="xxxx" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>		

4.给予注解方式实现属性注入
(1)@AutoWired 根据属性类型进行自动装配

(2)@Qualifier 根据属性的名称进行注入 要和@AutoWired一起来使用

@Autowired //根据类型进行注入
@Qualifier(value = "userDapImpl") //根据属性名进行注入

(3)@Resource 可以根据类型注入 也可以根据名称注入

(4)@Value 注入普通属性

完全的注解开发

1.创建配置类,替代xml配置文件

 @Configuration //这个注解表示这个类是一个配置类
 @ComponentScan(basePackages = {"com.xiao.spring"})//代替XML文件中的扫描路径

2.编写测试累

ApolicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class)

AOP:面向切面

特点
1.什么是aop

(1)面向切面编程,利用AOP可以对业务业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率。
(2)再不改变源代码的情况下 给项目增加新的功能。
例子:
在这里插入图片描述

2.AOP底层原理

1.AOP底层使用动态代理
(1)两种情况
一种是有接口的情况,使用JDK的动态代理
在这里插入图片描述

二种是没有接口的情况,使用户CGLIB的动态代理

在这里插入图片描述

编写JDK动态代理代码

1.创建接口,定义方法
2.创建接口实现类

AOP术语
1.连接点
(1)类里边的那些方法可以被增强,这些方法称为连接点
2.切入点
(1)实际被真正增强的方法,称为切入点
3.增强(通知)

(1)实际增强的逻辑部分称为通知(增强)
(2)通知有多种类型
①前置通知
②后者通知
③环绕通知
④异常通知
⑤最终通知

4.切面

AOP操作(准备)
1.Spring框架中一般基于AspectJ实现AOP操作

(1)是什么是AspectJ
		AspectJ不是Spring组成部分,独立AOP框架,一般把AspectJ和Spring框架爱一起来使用进行AOp操作
(2)给予AspectJ实现AOP操作
		1.基于xml配置文件实现
		2.给予注解方式 实现(使用)
(3)在项目工程里边引入AOP相关依赖
(4)切入点表达式
	切入点表达式的作用:知道对那个类里边的那个方法进行增强
	语法结构
	Execution([权限修饰符][返回类型][全类路径]([参数列表]))
	例子
	对UserDao类中的add方法进行增强
	Execution(*com.xiao.spring.dao.UserDao.add(...))
	对UserDao类中的所有方法进行增强
	Execution(*com.xiao.spring.dao.UserDao.*(...))
	对Dao包下的所有类中的所有方法进行增强
	Execution(*com.xiao.spring.dao.*.*(...))

AOP操作(AspectJ注解)
1.编写一个类

//被增强的类
@Component
public class User{
	public void add(){
		System.out.println("add方法")
	}
}

2.创建增强类(编写增强逻辑)
在增强类里边,创建方法,让不同方法代表不同的通知类型

//增强的类
@Component
@Aspect
public class UserProxy{
	//前置通知
	@Befter
	public void before(){
		System.out.println(" before增强方法")
	}
}	

3.导入命名空间 context 和aop
在这里插入图片描述
4.开启注解扫描
5.开启Aspect生成代理对象

<!--    开启注解扫描-->
    <context:component-scan base-package="com.xiao.spring"></context:component-scan>
<!--    开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

6.两个类加上@Component
7.在增强类上添加@Aspect
配置不同类型的通知。
在增强类里边的方法上添加@Before,使用切入点表达式来配置

//增强的类
@Component
@Aspect
public class UserProxy{
	//前置通知
	@Before(* com.xiao.spring.aop.User.add(..))
	public void before(){
		System.out.println(" 前置通知")
	}
	//最终通知 ,方法执行之后进行
	@After(* com.xiao.spring.aop.User.add(..))
	public void before(){
		System.out.println(" 最终通知 ,方法执行之后进行")
	}
	//后置通知 ,方法返回值之后进行
	@AfterReturning(* com.xiao.spring.aop.User.add(..))
	public void before(){
		System.out.println(" before增强方法")
	}
	//异常通知
	@AfterThrowing(* com.xiao.spring.aop.User.add(..))
	public void before(){
		System.out.println(" 异常通知")
	}
	//环绕通知
	@Around(* com.xiao.spring.aop.User.add(..))
	public void before(ProceedingJoinPoint pr)throws Throwable{
		System.out.println(" 环绕之前")
		pr.proceed();
		System.out.println(" 环绕之后")
	}
}	

8.编写测试类Test

public void testAopAnno(){
	ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
	User user=context.getBeans("user",User.class);
	user.add();
}	

4.spring特点

(1)方便解耦,简化开发。
(2)AOP编程的支持
(3)方便程序的测试(junit4)
(4)方便和其它框架进行整合。
(5)降低JavaEE API的使用难度
(6)方便进行事务操作

5.spring 的jar包下载地址 https://repo.spring.io/release/org/springframework/spring/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值