1、spring框架的优点
开源,因为核心是IOC和Aop 所以拥有两者的优点。包容性强可以兼容其他优秀的框架。
2、spring是开源的轻量级分层式开源框架以IOC和AOP为内核。
3、IOC 又叫做控制反转
降低程序之间的耦合。
依赖管理,以后需要的对象由spring创建,我们只需要在配置文件中说明。
4、spring dl
(依赖注入)意思是自身对象中的内置对象是通过注入的方式进行创建。
依赖注入 :有两种 实现方式:Setter方式(传值方式)和构造器方式(引用方式)。
5、注解
@component 包含三个注解@controller @service @Repository
@Autowired 自动注入将对象注入 不用在new Mapper() 只需要 声明就行
@QualiFier 选择注入的对象(id)
@Resource(name = "car") 相当于上面的两个注解
@configuration使该类最先加载
@conponentScan 扫描该目录使其可以用注解
@PropertySource 导入外部文件
@Bean//把当前方法的返回值放在Spring容器中,相当于<bean id = "" class = "">
6、AOP 面向切面编程
底层原理是代理模式,代理模式和修饰器模式并列,都是方法增强,代理模式又分为静态代理和动态代理,静态代理需要实现接口中的所有方法,没有针对性,所以用的更多的是动态代理。
7、写通知,里面是增强的
Aop 配置,aop 切点 (未增强方法)
Aop切面(增强方法), 里面有个通知,需要dI他并且注入到Aop中
添加方法
8、aop事务,两件事务需要连续进行,如果一个出错,都不执行。如:转账,前者出错,那么双方都不执行。
9、idea 配置事务
引入外部文件
配置数据库
配置事务管理平台
配置通知事件
配置切面
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 引入数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="url" value="${db.url}"></property>
<property name="driverClassName" value="${db.driverClassName}"></property>
</bean>
<!-- 事务平台管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addAnd" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<!-- <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>-->
</tx:attributes>
</tx:advice>
<!-- 配置事务切面 -->
<aop:config proxy-target-class="false" expose-proxy="false">
<aop:pointcut id="txPc" expression="execution(* service.impl.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"></aop:advisor>
</aop:config>
<bean id="userMapperImpl" class="mapper.impl.UserMapperImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="userServiceImpl" class="service.impl.UserServiceImpl">
<property name="userMapperImpl" ref="userMapperImpl"></property>
</bean>
</beans>