一、Spring基础知识
1、基础知识
1. IoC:控制反转——反转控制 ——设计思想
控制:控制权 反转:控制权的转移、某种方式的改变 被控制主体:对象、资源 控制者:Spring——IoC容器 控制权:创建、获取、查找——对象、资源——权利/方式 IoC类型 : 注入方法:构造函数注入、属性注入、接口注入 Spring支持:构造函数注入、属性注入
设计思想:面向对象、主从换位、主动变被动
java类间关系:继承(实现)、依赖、关联、聚合、组合
依赖:不同类之间的相互引用
控制权:创建对象、查找对象、外部资源的获取、获取对象
控制者:容器————IoC容器————注入组合对象
被控制者:外部资源——对象——文件
反转:权利交给第三方——IoC容器
容器权利:注入组合对象
传统方式(正转主动):对象内部直接控制——new对象创建对象——程序主动创建依赖
正转:开发者在对象中主动控制、去直接获取依赖对象
正传:类内部主动创建依赖对象、导致类与类之间高耦合、难于测试
当前方式(反转被动):IoC容器来控制对象创建、外部资源获取
反转:由容器来帮忙创建及注入依赖对象
反转:由容器帮我们查找及注入依赖对象
反转:对象只是被动的接受依赖对象、所以是反转
反转内容:依赖对象的获取被反转了
方法:被动等待IoC容器来创建并注入其所需要的资源
总结:将获取对象的权利交给Spring框架
总结:IoC容器帮对象找相应的依赖对象并注入、不由对象主动去找
总结:spring来负责控制对象的生命周期和对象间的关系
总结:
1、所有类在spring容器中登记、告诉spring你是个什么东西,你需要什么东西,
2、spring会在系统运行到适当的时候,把你要的东西主动给你,同时也把你交给其他需要你的东西。————DI
3、所有的类的创建、销毁都由 spring来控制,也就是说控制对象生存周期的不再是引用它的对象,而是spring。
4、对于某个具体的对象而言,以前是它控制其他对象,现在是所有对象都被spring控制,所以这叫控制反转。
控制反转IoC(Inversion of Control):
是说创建对象的控制权进行转移,
以前创建对象的主动权和创建时机是由自己把控的,
而现在这种权力转移到第三方,
IoC容器实现转移:
专门用来创建对象的工厂,你要什么对象,它就给你什么对象,有了 IoC容器,依赖关系就变了,原先的依赖关系就没了,它们都依赖IoC容器了,通过IoC容器来建立它们之间的关系。
2. DI:依赖注入 ——容器决定——对象的获取注入
java1.3后:反射
反射:允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性
依赖注入实现:spring通过反射来实现注入
依赖注入:被注入对象依赖IoC容器配置依赖对象
依赖注入:组件之间依赖关系由容器在运行期决定
依赖注入:由容器动态的将某个依赖关系注入到组件之中
目的:提升组件重用的频率、为系统搭建一个灵活、可扩展的平台
IoC中角色:
IoC中一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。
这一点是通过DI(Dependency Injection,依赖注入)来实现的。
总结:A对象需要B对象————→告诉Spring——→获取对象——→spring————→A对象注入到B对象
控制反转内容:获得依赖对象的方式反转了
总结:即让调用类对某一接口实现类的依赖关系由第三方(容器或协作类)注入,以移除调用类对某一接口实现类的依赖。
依赖注入:通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它,也可以说依赖被注入到对象中。
3. AOP:面向切面编程
4. bean:Spring里的对象——java对象
bean对象: 由Spring框架创建和管理的对象的对象 Spring IoC容器实例化、组装和管理的对象 Spring IoC容器管理的对象 bean是对象,一个或者多个不限定 bean由Spring中一个叫IoC的东西管理 我们的应用程序由一个个bean构成 特性:懒加载(@Lazy)、作用域(Scope)、生命周期方法
2、核心jar包
spring-core.jar spring的核心jar包(工具类)
spring-beans.jar 配置文件、创建和管理bean及IOC所需要的类
spring-context.jar 上下文的jar(spring核心扩展包)
spring-aop.jar 使用Spring的AOP特性时所需的类
spring-expression.jar spring表达式语言
commons-login.jar 日志jar包
spring-webmvc.jar Spring MVC框架相关的所有类
3、架构
WEB层:SpringMVC
业务层:Spring的IoC
持久层:Spring的JDBCTemplate(Spring的JDBC模板,ORM模板用于整合其他的持久层框架)
二、重点配置文件
1、添加Maven依赖
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-core</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-beans</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context-support</ artifactId>
< version> 4.3.19.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-web</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> 5.2.2.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-tx</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aop</ artifactId>
< version> 5.2.9.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> 5.2.2.RELEASE</ version>
</ dependency>
2、applicationContext.xml模板
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: context= " http://www.springframework.org/schema/context" xmlns: p= " http://www.springframework.org/schema/p"
xmlns: aop= " http://www.springframework.org/schema/aop" xmlns: tx= " http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd" >
< context: property-placeholder location = " classpath:jdbc.properties" />
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource"
destroy-method = " close" >
< property name = " url" value = " ${spring.datasource.jdbc-url}" />
< property name = " username" value = " ${spring.datasource.username}" />
< property name = " password" value = " ${spring.datasource.password}" />
< property name = " driverClassName" value = " ${spring.datasource.driver-class-name}" />
< property name = " initialSize" value = " 0" />
< property name = " maxActive" value = " 50" />
< property name = " minIdle" value = " 0" />
< property name = " maxWait" value = " 60000" />
< property name = " validationQuery" value = " select 1" />
< property name = " testOnBorrow" value = " false" />
< property name = " testOnReturn" value = " false" />
< property name = " testWhileIdle" value = " true" />
< property name = " timeBetweenEvictionRunsMillis" value = " 6000" />
< property name = " minEvictableIdleTimeMillis" value = " 25200000" />
< property name = " removeAbandoned" value = " true" />
< property name = " removeAbandonedTimeout" value = " 1800" />
< property name = " logAbandoned" value = " true" />
</ bean>
< bean id = " sqlSessionFactory"
class = " com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " mapperLocations" value = " classpath:mapper/*.xml" />
< property name = " configLocation" value = " classpath:mybatis-config.xml" />
< property name = " typeAliasesPackage" value = " com.Customize.UI.entity" />
< property name = " plugins" >
< array>
< bean id = " paginationInterceptor" class = " com.baomidou.mybatisplus.plugins.PaginationInterceptor" >
< property name = " dialectType" value = " mysql" />
</ bean>
</ array>
</ property>
< property name = " globalConfig" ref = " globalConfig" />
</ bean>
< bean id = " globalConfig" class = " com.baomidou.mybatisplus.entity.GlobalConfiguration" >
< property name = " idType" value = " 0" />
< property name = " dbType" value = " mysql" />
< property name = " dbColumnUnderline" value = " true" />
</ bean>
< bean name = " transactionManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< tx: advice id = " transactionAdvice" transaction-manager = " transactionManager" >
< tx: attributes>
< tx: method name = " add*" propagation = " REQUIRED" />
< tx: method name = " append*" propagation = " REQUIRED" />
< tx: method name = " save*" propagation = " REQUIRED" />
< tx: method name = " update*" propagation = " REQUIRED" />
< tx: method name = " modify*" propagation = " REQUIRED" />
< tx: method name = " edit*" propagation = " REQUIRED" />
< tx: method name = " insert*" propagation = " REQUIRED" />
< tx: method name = " delete*" propagation = " REQUIRED" />
< tx: method name = " remove*" propagation = " REQUIRED" />
< tx: method name = " repair" propagation = " REQUIRED" />
< tx: method name = " get*" propagation = " REQUIRED" read-only = " true" />
< tx: method name = " find*" propagation = " REQUIRED" read-only = " true" />
< tx: method name = " load*" propagation = " REQUIRED" read-only = " true" />
< tx: method name = " search*" propagation = " REQUIRED" read-only = " true" />
< tx: method name = " select*" propagation = " REQUIRED" read-only = " true" />
< tx: method name = " *" propagation = " REQUIRED" read-only = " true" />
</ tx: attributes>
</ tx: advice>
< aop: config>
< aop: pointcut id = " transactionPointcut" expression = " execution(* com.Customize.UI.service..*.*(..))" />
< aop: advisor pointcut-ref = " transactionPointcut" advice-ref = " transactionAdvice" />
</ aop: config>
< tx: annotation-driven transaction-manager = " txManager" />
< bean id = " mapperScannerConfigurer" class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.Customize.UI.mapper" />
</ bean>
</ beans>
3、spring-config.xml(applicationContext.xml)
1、先删除:Project Structure —→Facets—→spring (减号—删除)
2、新建配置xml:右击项目 —→Add Frameworks Support —→Spring—→Createempty spring-config.xml
3、移动xml到对应的目录:spring-config.xml移动到对应文件夹
4、创建模板:File | Settings | Editor | File and Code Templates——添加+号
<?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" >
</ beans>
4、spring-beans.xml
三、测试
1、Junit 测试注解:@Test(可以多个)
自动使用junit:
@RunWith ( SpringJUnit4ClassRunner. class )
告诉junit测试bean的配置文件位置:
@ContextConfiguration ( locations = "classpath:applicationContext.xml" )
调用类资源:
@Resource
使用测试方法:
@Test
实例
import com. remli. pojo. Master;
import org. junit. Test;
import org. junit. runner. RunWith;
import org. springframework. test. context. ContextConfiguration;
import org. springframework. test. context. junit4. SpringJUnit4ClassRunner;
import javax. annotation. Resource;
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( "classpath:spring-config.xml" )
public class SpringTest {
@Resource
private Master master;
@Test
public void testMaster ( ) {
System. out. println ( "测试程序启动成功" ) ;
}
}
2、Main 方法测试:main()(只有1个)