Spring简介
一、引入
1.spring核心依赖
<dependencies>
<!-- 依赖分类
mybatis
druid连接池
mysql驱动包
spring核心包
spring-jdbc包
spring-tx包
springmybatis整合包
springtext包
aspectJ包
junit包 -->
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<!-- druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.13</version>
</dependency>
<!-- MyBatis与Spring的整合包,该包可以让Spring创建MyBatis的对象 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.13</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<!-- junit,如果Spring5整合junit,则junit版本至少在4.12以上 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring整合测试模块 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.13</version>
<scope>test</scope>
</dependency>
</dependencies>
2.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
3.Spring整合MyBatis_搭建环境
- 添加包扫描对象,将包内对象自动注册到容器中
- 添加配置文件引用
- 创建链接池对象(对配置文件进行引用${})
- 创建SqlSessionFactoryBean对象,其中引入连接池以及Mybatis配置文件
- 创建自动代理对象,将Dao映射文件自动生成
4.具体applicationContext文件内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="com.zbzzs"></context:component-scan>
<!-- 读取配置文件 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 创建连接池数据源对象 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 创建Spring封装过的SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
</bean>
<!-- 该对象可以自动扫描持久层接口,并为接口创建代理对象 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置扫描的接口包 -->
<property name="basePackage" value="com.zbzzs.dao"></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 注册事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 进行事务相关配置 -->
<tx:advice id="txAdvice">
<tx:attributes>
<!-- 暂时所有方法都使用默认配置 -->
<tx:method name="*"/>
<!-- 查询方法是只读事务 -->
<tx:method name="find*" read-only="true" isolation="READ_UNCOMMITTED"></tx:method>
</tx:attributes>
</tx:advice>
<!-- Aop配置 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut id="pointcut" s="execution(* com.zbzzs.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>
</beans>
5.aop
<!-- 通知对象 -->
<bean id="myAspectJAdvice" class="com.zbzzs.advice.MyAspectAdvice"></bean>
<bean id="myAspectJAdvice2" class="com.zbzzs.advice.MyAspectAdvice2"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="myAspectJAdvice">
<!-- 配置切点 -->
<aop:pointcut id="myPointcut" expression="execution(* *..*.*(..))"/>
<!-- 后置通知 -->
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut"/>
</aop:aspect>
<aop:aspect ref="myAspectJAdvice2">
<aop:pointcut id="myPointcut2" expression="execution(* com.zbzzs.dao.UserDao.*(..))"/>
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut2"/>
</aop:aspect>
</aop:config>
SpringIOC总结
-
Spring框架根据不同的功能被划分成了多个模块,这些模块可以满足一切企业级应用开发的需求,在开发过程中可以根据需求有选择性地使用所需要的模块
-
IOC:控制反转;AOP:面向切面编程
-
模块简介
Core Container Spring核心模块,任何功能都离不开该模块,是其他模块建立的基础 Data Access / Integration 该模块提供了数据持久化的相应功能 Web 该模块提供了web开发的相应功能 AOP 提供了面向切面编程实现 Aspects 提供与AspectJ框架的集成,该框架是一个面向切面编程框架 Instrumentation: 提供了类工具的支持和类加载器的实现,可以在特定的应用服务器中使用 Messaging 为Spring框架集成一些基础的报文传送应用 Test 提供与测试框架的集成 -
Spring创建方式包括使用空参构造方法,使用工厂,静态共产方法
-
SpringBean对象创建策略:singleton、prototype,其中singleton包括懒加载属性lazy-init=“true”>
-
SpringBean对象的生命周期方法
init-method="init" destroymethod="destory"
-
获取Bean对象的方式:id、name、类.class,(id,类.class)
DI注入
-
DI注入,SpringIOC的具体实现,框架为对象的属性赋值
-
Setter注入,需要对象有setter方法,Bean对象中内含Propery属性
-
构造方法注入,被注入类编写有参的构造方法
<constructor-arg name="studentDao"ref="studentDao"></constructor-arg>
-
自动注入
<bean id="studentService"class="**" autowire="byName"></bean>
-
注入基本数据类型
<property name="name" value="***">
-
注入List、set集合 propery->list {value || bean}
-
注入Map集合 propery->map ->entry(key,value)
-
注入properties 对象 propery->props->prop(value)
-
注解实现IOC:@Component、@Repository、@Service、@Controller
-
注解实现IOC:@Scope设置创建策略,@Scope(“singleton”)
-
注解实现IOC:@Autowired,自动注入,容器中有多个对象匹配类型时,会找beanId等于属性名的对象,找不到会报错。
-
注解实现IOC:@Qualifier必须和@Autowired一起使用,指定用哪个类的beanid来注册
-
注解实现IOC:@Value
@Value("1") private int count; @Value("${jdbc.username}")//首先配置文件已经被读取 private String username;
-
注解实现IOC:@Configuration,设置配置类(不推荐)
-
注解实现IOC:@ComponentScan,设置扫描包(不推荐)
-
注解实现IOC:@PropertySource,设置配置文件(不推荐)
-
注解实现IOC:@Bean,将第三方对象放置容器
-
注解实现IOC:@Import,导入其他配置类
SpringAOP总结
-
AOP面向切面编程,在不修改源码的基础上,对已有方法进行增强。
-
AOP实现原理:动态代理技术
-
AOP包括:切面(切点、通知)
-
推荐使用AspectJ框架
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency>
-
配置文件需添加
<bean id="myAspectJAdvice"class="****"></bean> <!-- 配置AOP --> <aop:config> <!-- 配置切面 --> <aop:aspect ref="myAspectJAdvice"> <!-- 配置切点 --> <aop:pointcut id="myPointcut" expression="execution(* com.zbzzs.dao.UserDao.*(..))"/> <!-- 配置通知 --> <aop:after-returning method="onr" pointcut-ref="myPointcut"/> </aop:aspect> </aop:config
-
SpringAOP_切点表达式:访问修饰符 返回值 包名.类名.方法名(参数列表)
-
SpringAOP_多切面配置:创建两个通知对象,设置两个切面(ref不同)
-
AOP注解:Spring可以使用注解代替配置文件配置切面
<!-- 开启注解配置Aop --> <aop:aspectj-autoproxy> </aop:aspectjautoproxy>
-
AOP注解:@Aspect,通知类上方设置
-
AOP注解:@Before/@AfterReturning/@AfterThrowing/@After/@Around,通知方法上加入
-
AOP注解:为一个类下的所有方法统一配置切点
@Pointcut("execution(* com.zbzs.dao.UserDao.*(..))") public void pointCut(){} @Before("pointCut()") public void myBefore(JoinPoint joinPoint) { System.out.println("前置通知..."); }
-
AOP注解:@EnableAspectJAutoProxy,代替xml中AOP注解支持
事务
-
添加依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.13</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency>
-
配置文件
<!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 进行事务相关配置 --> <tx:advice id = "txAdvice"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置切面 --> <aop:config> <!-- 配置切点 --> <aop:pointcut id="pointcut" expression="execution(* *..*.*(..))"/> <!-- 配置通知 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor> </aop:config>
-
事务的传播行为:REQUIRED 默认,一般无需更改
-
事务的隔离级别:REPEATABLE_READ(重复读),一般无需更改
-
注解配置事务:
<!-- 注册事务注解驱动 --> <tx:annotation-driven transactionmanager="transactionManager"> </tx:annotation-driven>
-
在需要事务支持的方法或类上加@Transactional (不推荐)
-
配置类代替xml中的注解事务支持:在配置类上方写 @EnableTranscationManagement