spring:IoC,DI,装配Bean(xml方式,注解方式),AOP,声明式事务管理
装配Bean
装配Bean,xml方式:
1. 实例化
默认构造<baen id="" class="">
静态工厂 <bean id="" class="工厂全限定类名" factory-method="静态方法">
实例工厂 <bean id="" factory-bean="工厂实例的id" factory-method="普通方法">
2. 作用域:singleton,prototype,request,session
3. 生命周期:构造方法--》初始化方法--》setter方法注入--》使用--》销毁方法
<bean id="" class="" init-method="初始化方法名称" destory-method="销毁方法名称">
另外,若类 implements BeanPostProcessor 接口后,可以在初始化方法的前后执行override方法,可以生成代理对象返回。
4. 属性依赖注入(DI)
构造方法注入 <construtor-arg index="0" type="java.lang.String" value="hello"/> 或 <construtor-arg name="uname" value="zhangsan"/>
setter方法注入 <property name="" value=""/> 或注入对象 <property name="" ref=""/>
集合注入:
-----------------------------
装配Bean,注解方式:
首先在applicationContext.xml添加命名空间context, 其次添加组件扫描(<context:component-scan base-package="com.htj.crm">)
1. @Component取代<bean class="">
@Component("id") 取代 <bean id="" class="">
2.web开发,提供3个@Component注解衍生注解(功能一样)取代<bean class="">
@Repository :dao层
@Service:service层
@Controller:web层
3.依赖注入 ,给私有字段设置,也可以给setter方法设置
普通值:@Value("")
引用值:
方式1:按照【类型】注入
@Autowired
方式2:按照【名称】注入1
@Autowired
@Qualifier("名称")
方式3:按照【名称】注入2
@Resource("名称")
4.生命周期
初始化:@PostConstruct
销毁:@PreDestroy
5.作用域
@Scope("prototype") 多例
自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean
AOP(面向切面编程)
AOP的含义:在不修改目标类的情况下,对目标类的方法进行增强,像之前调用一个方法(前置通知),之后调用一个方法(后置通知)。解耦合、代码复用、易扩展(横向抽取)。
应用:权限控制、事务管理、性能监控。
AOP原理:使用代理机制进行实现。
- 接口 + 实现类 :spring采用 jdk 的动态代理Proxy。
- 实现类:spring 采用 cglib字节码增强。
AOP术语
- target:目标类,需要被代理的类。例如:UserService
- Joinpoint(连接点):所谓连接点是指那些可能被拦截到的方法。例如:所有的方法
- PointCut 切入点:已经被增强的连接点。例如:addUser()
- advice 通知/增强,增强代码。例如:after、before
- Weaving(织入):是指把增强advice应用到目标对象target来创建新的代理对象proxy的过程.
- proxy 代理类
- Aspect(切面): 是切入点pointcut和通知advice的结合
一个线是一个特殊的面。
一个切入点和一个通知,组成成一个特殊的面。
半自动AOP编程
使用AOP联盟规范、spring-aop实现
在切面类 implements MethodInterceptor ,实现“环绕通知”。
配置ApplicationContext.xml
<bean id="proxyServiceId" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="com.htj.service.UserService"></property>
<property name="target" ref="userServiceId"></property>
<property name="interceptorNames" value="myAspectId"></property>
</bean>
使用时,从spring容器中获得bean,UserService userService = (UserService) applicationContext.getBean(“proxyServiceId”);
全自动AOP编程(使用AspectJ)
<!-- 3 aop编程
3.1 导入命名空间
3.2 使用 <aop:config>进行配置
proxy-target-class="true" 声明时使用cglib代理
<aop:pointcut> 切入点 ,从目标对象获得具体方法
<aop:advisor> 特殊的切面,只有一个通知 和 一个切入点
advice-ref 通知引用
pointcut-ref 切入点引用
3.3 切入点表达式
execution(* com.itheima.c_spring_aop.*.*(..))
选择方法 返回值任意 包 类名任意 方法名任意 参数任意
-->
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* com.itheima.c_spring_aop.*.*(..))" id="myPointCut"/>
<aop:advisor advice-ref="myAspectId" pointcut-ref="myPointCut"/>
</aop:config>
事务管理
编程式事务
基于TransactionTemplate的编程式事务,像execute(new TransactionCallback(){匿名内部类})
//需要spring注入模板
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
@Override
public void transfer(final String outer,final String inner,final Integer money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
accountDao.out(outer, money);
//断电
// int i = 1/0;
accountDao.in(inner, money);
}
});
}
spring配置
<!-- 3 service -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
<!-- 4 创建模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>
<!-- 5 配置事务管理器 ,管理器需要事务,事务从Connection获得,连接从连接池DataSource获得 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
声明式事务(重要)
xml方式
<tx:advice id="aaa" class=""> 配置事务的通知,子标签<tx:attributes>
<aop:config> 配置aop,通知与切入点
<aop:advisor advice-ref="aaa" point-ref="bbb"/>
<aop:point-cut id="bbb" expression="execution(* cn.htj.sercice..*.*(..))"/>
例子:
<!-- 4 事务管理 -->
<!-- 4.1 事务管理器
DataSourceTransactionManager ,jdbc开发时事务管理器,采用JdbcTemplate
HibernateTransactionManager,hibernate开发时事务管理器,整合hibernate
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.2 事务详情(事务通知) , 在aop筛选基础上,对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
<tx:attributes> 用于配置事务详情(属性属性)
<tx:method name=""/> 详情具体配置
propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的;默认是REQUIRED
isolation 隔离级别 默认是DEFAULT(使用数据库默认级别)
read-only 是否只读 默认是false
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true">
<!-- 所有方法 -->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!-- 4.3 AOP编程,目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service..*.*(..))"/>
</aop:config>
注解方式
首先spring配置,在applicationContext.xml 中配置
- 事务管理器
… - 将配置事务管理器交予spring
<tx:annotation-driven transaction-manager="....">
然后,在目标类上加 @Transactional
,也同时在方法上加,会覆盖类上的
例子:
<!-- 4 事务管理 -->
<!-- 4.1 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.2 将管理器交予spring
* transaction-manager 配置事务管理器
* proxy-target-class
true : 底层强制使用cglib 代理
-->
<tx:annotation-driven transaction-manager="txManager"/>
@Transactional
public class AccountServiceImpl implements AccountService {
或者加参数
@Transactional(propagation=Propagation.REQUIRED , isolation = Isolation.DEFAULT)
public class AccountServiceImpl implements AccountService {
参数有: