Spring框架

Spring介绍

Spring框架是Java应用最广泛的框架,它的成功来源于理念,而不是技术本身。

  • IOC:控制反转
  • AOP:面向切面编程
  • DI:依赖注入

非侵入式设计:无需继承框架提供的任何一个类,更换框架,代码基本上不需要大改。

JavaBean

springbean是javabean的扩展,但是已经完全不是一回事儿了。

现在的JavaBean作为对象,要求每个属性提供setget方法,spring的bean只需要为接收设置的值注入提供set方法。

Spring优势

  • 低侵入 / 低耦合
  • 声明式事务(基于AOP)
  • 方便继承其他框架
  • 降低了Java的开发难度(扯)
  • 提供了JavaWEB三层的每一层的解决方案

Spring能帮我们干什么?

装修:

  1. 自己找工人,自己买材料,自己监工。
  2. 找一个装修公司,拿钱就完事,啥也不管。(spring)

装修公司从哪里来?

  1. 自己开一个。
  2. 从已有的装修工公司中找一个。(spring)

IOC

控制反转。

IOC容器!!!

AOP

  • AOP,面向切面编程,主要用来解决一些系统层面上的问题。比如日志事务权限
  • 在不改变原有的逻辑的基础上,增加一些额外的功能。
  • AOP是OOP(面向对象)补充和完善。
  • AOP横切技术,剖解开对象的内部,把一些和业务无关,却可以为业务共同的调用的逻辑封装起来,减少重复代码的使用。

通知

增强处理(Advice),就是你想要的功能,日志,事务,权限,安全...

你先定义好,然后再想用的地方,Adivce配合Aspect的一段处理代码。

连接点

JoinPoint。

就是spring允许你通知的地方。

一个service层的方法需要配置通知,获取这个方法的参数,方法名,返回值....

切入点

PointCut。

一个类中有20个方法,让这20个方法中的15个在方法执行之前、执行之后或者抛出异常时干点什么,使用切入点表达式来筛选连接点。

切面

通知和切入点的结合。在什么个点上去干什么事。

织入

被通知的对象,真正的业务逻辑,可以毫不知情。两者互相之前可能并不知情,专注于自己该做的事。

把对象放进容器中,对象一旦放入容器中,这个对象在我当前的项目中就可以共享,而且默认还是单例的 id:这个对象在IOC容器中的唯一标识

class:要放到spring的IOC容器中的全类名 现在,我已经把User这个类的对象放进了spring的IOC容器,交给了spring管理User类的对象

这个user对象实际上就是通过调用User类的无参构造器创建出来的

属性注入: 1、构造器注入:根据参数名,根据索引位置,根据参数类型 2、setter注入:调用set方法来进行属性注入(常用)

如果我们的bean要注入的属性也是一个对象,那这个对象也要先注册到IOC容器中 align:对齐 ref:依赖,当使用ref属性,依赖于当前容器中已经存在的bean,进行属性的注入

​ scope:定义bean的作用域 prototype:原型 singleton:单例(默认) request:一次请求 session:一次会话

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--  开启自动装配注解  -->
<context:component-scan base-package="com.jsoft"/>
<!--  管理数据源  -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm?useSSL=false&useUnicode=true&characterEncoding=utf-8"/>
</bean>
<!--  把自定义的转换器注册进IOC容器  -->
<bean id="stringToDateConverter" class="com.jsoft.converter.StringToDateConverter"/>
<!--  我们需要配置原生的类型转换器的属性,让spring原生的类型转换器使用我们自定义的转换器  -->
<!-- 
        ConversionServiceFactoryBean这个类是Spring原生的转换器处理类
        spring去容器中找 ConversionServiceFactoryBean,根据id去找的
        这个id就不能随意写,就需要我们固定写法。
     -->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<!--  要注入的属性是我们IOC容器中已经存在的stringToDateConverter  -->
<!--  依赖注入  -->
<ref bean="stringToDateConverter"/>
</set>
</property>
</bean>
<bean id="account" class="com.jsoft.entity.Account">
<property name="id" value="1"/>
<property name="username" value="admin"/>
<property name="birthday" value="1990-09-20"/>
</bean>
<!--     <bean id="address" class="com.jsoft.entity.Address" scope="singleton"> -->
<!--         <property name="info" value="高新区"></property> -->
<!--     </bean> -->
<!--     <bean id="address1" class="com.jsoft.entity.Address" scope="singleton"> -->
<!--         <property name="info" value="南关区"></property> -->
<!--     </bean> -->
<!-- 
        autowire:自动装配
            byName:根据属性名去IOC容器中找名字相同的bean进行自动注入
            byType:根据属性的类型自动注入
     -->
<!--     <bean id="user" class="com.jsoft.entity.User"> -->
<!-- &lt;!&ndash;        <constructor-arg value="jsoft" name="name"></constructor-arg>&ndash;&gt; -->
<!--         <property name="name" value="JSOFT"></property> -->
<!-- &lt;!&ndash;        <property name="address" ref="address"></property>&ndash;&gt; -->
<!--         <property name="hobbies"> -->
<!--             <array value-type="java.lang.String"> -->
<!--                 <value>篮球</value> -->
<!--                 <value>足球</value> -->
<!--             </array> -->
<!--         </property> -->
<!--         <property name="duties"> -->
<!--             <list value-type="java.lang.String"> -->
<!--                 <value>董事长</value> -->
<!--                 <value>总经理</value> -->
<!--             </list> -->
<!--         </property> -->
<!--         <property name="carts"> -->
<!--             <set value-type="java.lang.String"> -->
<!--                 <value>韭菜</value> -->
<!--                 <value>鸡蛋</value> -->
<!--             </set> -->
<!--         </property> -->
<!--         <property name="map"> -->
<!--             <map> -->
<!--                 <entry key="父亲" value="马云"></entry> -->
<!--                 <entry key="母亲" value="张瑛"></entry> -->
<!--             </map> -->
<!--         </property> -->
<!--         <property name="properties"> -->
<!--             <props> -->
<!--                 <prop key="阿里巴巴">达摩院</prop> -->
<!--             </props> -->
<!--         </property> -->
<!--     </bean> -->
</beans>
posted @   清欢qing  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示