<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 设置当前项目内容 -->
    <groupId>com.itheima</groupId>
    <artifactId>day04_eesy_06tx_anno</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <dependencies>
        
        <!-- 在基础IOC功能上提供扩展服务 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!-- spring中对JDBC 的简单封装 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
  
        <!-- 为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!-- 对JUNIT等测试框架的简单封装 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!-- 数据库连接依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        
        <!-- c3p0创建数据源 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- 切入点表达式需要用的包 --> 
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <!-- 测试jar包依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>bean标签
destroy-method:指定类中的某个方法为销毁方法
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:p="http://www.springframework.org/schema/p"     //加入p标签约束
       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"> 
    
    <!-- 指定要扫描的包 -->
    <context:component-scan base-package="com.itheima"/>
    
    <!-- 通过静态共工厂创建 -->
    <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="createAccountService"></bean>
    
    <!-- 通过工厂对象创建 -->
    <bean id="instancFactory" class="com.itheima.factory.InstanceFactory"></bean> 
    <bean id="accountService" factory-bean="instancFactory" factory-method="createAccountService">
    </bean>
    
     <!-- 使用p:propertyName来注入数据,它的本质仍然是调用类中的set方法实现注入功能。-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl4" p:name="test" p:age="21" p:birthday-ref="now"/> 
    
    <!-- 在注入集合数据时,只要结构相同,标签可以互换 --> 
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">  
        <!-- 给数组注入数据 -->
        <property name="myStrs"> 
        <set> 
            <value>AAA</value> 
            <value>BBB</value> 
            <value>CCC</value> 
        </set> 
        </property>
        
        <!-- 注入Map数据 --> 
        <property name="myMap"> 
            <props> 
                <prop key="testA">aaa</prop> 
                <prop key="testB">bbb</prop> 
            </props> 
        </property>
        
        <!-- 注入properties数据 -->
        <property name="myProps"> 
            <map> 
                <entry key="testA" value="aaa"></entry> 
                <entry key="testB"> <value>bbb</value> </entry> 
            </map> 
        </property>
    </bean>
    
    <!-- 构造函数注入 -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="张三"></constructor-arg> 
        <constructor-arg name="age" value="18"></constructor-arg> 
        <constructor-arg name="birthday" ref="now"></constructor-arg> 
    </bean> 
    <bean id="now" class="java.util.Date"></bean>
</beans>环境搭建
<?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: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
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.itheima"></context:component-scan>
</beans>ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);配置步骤
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations= {"classpath:bean.xml"})   //表明配置基于xml
@@ContextConfiguration(classes = SpringConfiguration.class)   //表明基于配置类(两者二选一)
public class AccountServiceTest {    
    @Autowired 
    private IAccountService as ; 
    
    @Test
    void testOne(){
        ....
    }
} 
 应用:在实际开放中,业务层来控制事务的提交与回滚
基于接口的动态代理
通过JDK官方的Proxy类创建代理对象
要求有被代理类实现的接口
 public static void main(String[] args) {
        final Producer producer = new Producer();
        IProducer proxyProducer =
                (IProducer) Proxy.newProxyInstance(
                producer.getClass().getClassLoader(),   //被代理者字节码文件
                producer.getClass().getInterfaces(),    //实现接口
                new InvocationHandler() {               //代理细节
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) 
                        throws Throwable {
                        //提供增强的代码
                        Object returnValue = null;
                        //1.获取方法执行的参数
                        Float money = (Float)args[0];
                        //2.判断当前方法是不是销售
                        if("saleProduct".equals(method.getName())) {
                            returnValue = method.invoke(producer, money*0.8f);
                        }
                        return returnValue;
                    }
                });
        proxyProducer.saleProduct(10000f);
    }基于子类的动态代理
public static void  main(String[] args) {   
    //这可不是被代理类被final修饰了,只是代表成员变量只能赋值一次,且不能更改
    final Actor actor = new Actor();        
    Actor cglibActor = (Actor) Enhancer.create(
        actor.getClass(),            //被代理类的字节码类型
        new MethodInterceptor() {       //具体代理实现
            @Override    
            public Object intercept(
                Object proxy,            //被代理者
                Method method,           //被代理者的方法
                Object[] args,           //被代理者的方法中的参数
                MethodProxy methodProxy  //当前执行方法的代理对象
            ) throws Throwable {     
                String name = method.getName();     
                Float money = (Float) args[0]; 
                Object rtValue = null;     
                if("basicAct".equals(name)){ 
                    if(money > 2000){       
                        rtValue = method.invoke(actor, money/2); 
     
                    }     
                } 
    
                if("dangerAct".equals(name)){ 
                    //危险演出      
                    if(money > 5000){ 
                        rtValue = method.invoke(actor, money/2);      
                    } 
                }     
                return rtValue;    
            } 
        });     
    cglibActor.basicAct(10000); 
    cglibActor.dangerAct(100000);  
} 业务层代理
public class BeanFactory {    
    /** 
    * 创建账户业务层实现类的代理对象   
    * @return   
    */  
    public static IAccountService getAccountService() {
        //1.定义被代理对象   
        final IAccountService accountService = new AccountServiceImpl();   
        //2.创建代理对象   
        IAccountService proxyAccountService = 
            (IAccountService) Proxy.newProxyInstance(
            accountService.getClass().getClassLoader(),
            accountService.getClass().getInterfaces(),
            new InvocationHandler() {    
                //添加事务管理
                @Override        
                public Object invoke(
                    Object proxy, 
                    Method method,     
                    Object[] args
                ) throws Throwable {         
                    Object rtValue = null;         
                    try { 
                        //开启事务          
                        TransactionManager.beginTransaction();         
                        //执行业务层方法          
                        rtValue = method.invoke(accountService, args);        
                        //提交事务          
                        TransactionManager.commit();         
                    }catch(Exception e) {                         
                        //回滚事务          
                        TransactionManager.rollback();         
                        e.printStackTrace();         
                    }finally {         
                        //释放资源          
                        TransactionManager.release();         
                    }         
                    return rtValue;        
                } 
            });   
        return proxyAccountService;  
    } 
} 相关术语
学习spring的开发步骤
<?xml version="1.0" encoding="UTF-8"?> 
<!--加入了aop的约束 -->
<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"        
       xsi:schemaLocation="http://www.springframework.org/schema/beans               
                           http://www.springframework.org/schema/beans/spring-beans.xsd   
                           http://www.springframework.org/schema/aop               
                           http://www.springframework.org/schema/aop/spring-aop.xsd"> 
</beans> <!-- 配置 service --> 
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> 
    <property name="accountDao" ref="accountDao"></property> 
</bean> 
<!-- 配置 dao --> 
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> 
     <property name="dbAssit" ref="dbAssit"></property> 
</bean>   
<!-- 配置数据库操作对象 --> 
<bean id="dbAssit" class="com.itheima.dbassit.DBAssit">  
    <property name="dataSource" ref="dataSource"></property> 
    <!-- 指定 connection 和线程绑定 -->  
    <property name="useCurrentConnection" value="true"></property> 
</bean>   
<!-- 配置数据源 --> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
    <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> 
    <property name="user" value="root"></property>  
    <property name="password" value="1234"></property> 
</bean>     //这个类是用来作为通知的,所以方法一般都是通知,即重复使用的代码
public class TransactionManager {    
    private DBAssit dbAssit ;  
    public void setDbAssit(DBAssit dbAssit) {...} 
     //开启事务 
    public void beginTransaction() {...} 
    //提交事务  
    public void commit() {...}  
    //回滚事务  
    public void rollback() {...}   
    //释放资源  
    public void release() {...} 
}<bean id="txManager" class="com.itheima.utils.TransactionManager">  
    <property name="dbAssit" ref="dbAssit"></property> 
</bean> 
<aop:config>
        <!--配置切入点   id:切入点的唯一标识; expression:用于定义切入点表达式 -->
        <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/> 
        <!--配置切面   id:切面的唯一标识; ref:通知类bean的id -->
        <aop:aspect id="txAdvice" ref="txManager">
            <!-- 配置通知的类型,method:通知类中增强方法名称;  pointcut-ref:指定切入点-->
            <aop:before method="beginTransaction" pointcut-ref="pt1"//>
        </aop:aspect>
</aop:config>对于环绕通知的配置:
问题:
当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了。
分析:
通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有。
解决:
Spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
spring中的环绕通知:
它是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式。
    <!-- 配置通知类 -->
    <bean id="logger" class="com.itheima.utils.Logger"></bean>
    <!--配置AOP-->
    <aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
        <!--配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">           
            <!-- 配置环绕通知 详细的注释请看Logger类中-->
            <aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
        </aop:aspect>
    </aop:config>public class Logger {
    //环绕通知内容
    public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try{
            Object[] args = pjp.getArgs();//得到方法执行所需的参数
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");
            rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");
            return rtValue;
        }catch (Throwable t){
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:context="http://www.springframework.org/schema/context"     
       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/aop         
                           http://www.springframework.org/schema/aop/spring-aop.xsd    
                           http://www.springframework.org/schema/context        
                           http://www.springframework.org/schema/context/spring-context.xsd"> 
    <!-- spring要扫描的包 --> 
    <context:component-scan base-package="com.itheima"></context:component-scan> 
    
    <!-- 开启 spring 对注解 AOP 的支持 --> 
    <aop:aspectj-autoproxy/> @Component("txManager") 
@Aspect         //表明当前类是一个切面类 
public class TransactionManager {   
    //定义一个 DBAssit  
    @Autowired  
    private DBAssit dbAssit ; 
    
    // 切入点表达式注解 
    @Pointcut("execution(* com.itheima.service.impl.*.*(..))") 
    private void pt1() {} 
    
    @Before("execution(* com.itheima.service.impl.*.*(..))")  
    public void beginTransaction() {   
        try {    
            dbAssit.getCurrentConnection().setAutoCommit(false);   
        } catch (SQLException e) {    
            e.printStackTrace();   
        }  
    } 
    @Around("pt1()")    //注意:千万别忘了写括号
    public Object transactionAround(ProceedingJoinPoint pjp) { 
        //定义返回值   
        Object rtValue = null;   
        try { 
            //获取方法执行所需的参数    
            Object[] args = pjp.getArgs(); 
            //前置通知:开启事务    
            beginTransaction(); 
            //执行方法    
            rtValue = pjp.proceed(args); 
            //后置通知:提交事务    
            commit();   
        }catch(Throwable e) { 
            //异常通知:回滚事务    
            rollback();    
            e.printStackTrace();   
        }finally {    
            //最终通知:释放资源    
            release();   
        }   
        return rtValue;  
    }
}不使用xml的配置方式
@Configuration 
@ComponentScan(basePackages="com.liu") 
@EnableAspectJAutoProxy 
public class SpringConfiguration { }public JdbcTemplate()   //通过set方法设置数据源
 
public JdbcTemplate(DataSource dataSource)    //构造函数指定好数据源
 
public JdbcTemplate(DataSource dataSource, boolean lazyInit)   //可以指定是否懒加载<!-- 配置一个数据库的操作模板:JdbcTemplate -->  
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">   
    <property name="dataSource" ref="dataSource"></property>  
</bean> 
<!-- 通过dbcp配置数据源 --> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
    <property name="url" value="jdbc:mysql:// /spring_day02"></property>  
    <property name="username" value="root"></property>  
    <property name="password" value="1234"></property> 
</bean>
<!-- 另外两种数据源配置也是相同,记得要导入相关数据源的坐标 --> 将数据库连接的信息配置到属性文件中
 <!-- 引入外部属性文件: -->
<context:property-placeholder location="classpath:jdbc.properties"/> 
 <!-- 另一种方式 -->
 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      <property name="location" value="classpath:jdbc.properties"/> 
 </bean> 
<!-- 然后通过正则获取 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="${user}"></property>
    <property name="password" value="${password}"></property>
    <property name="driverClass" value="${driverclass}"></property>
    <property name="jdbcUrl" value="${jdbcurl}"></property>
</bean>第一:
    JavaEE 体系进行分层开发,事务处理位于业务层,Spring 提供了分层设计业务层的事务处理解决方案。 
第二:
    spring 框架为我们提供了一组事务控制的接口。这组接口是在 spring-tx-5.0.2.RELEASE.jar 中。 
第三:
    spring 的事务控制都是基于AOP 的,它既可以使用编程的方式实现,也可以使用配置的方式实现。我们学习的重点是使用配置的方式实现Spring中事务控制的 API介绍
基于xml的声明式事物控制(配置方式) 重点
<?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: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/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">
    
    <!-- 配置 spring 创建容器时要扫描的包 -->  
    <context:component-scan base-package="com.liu"></context:component-scan> 
 </beans> <!-- 配置事物管理器 -->
<bean id="transactionManager" 
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <!-- 注入 DataSource -->  
    <property name="dataSource" ref="dataSource"></property> 
</bean> 
<!-- 配置事物的通知引用事物管理器 --> 
<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
    <!-- 配置事物的属性 name:表示业务层里的方法(*是通配符),propagation:表示事务的传播行为,required表示一定需要,supprots一般用于查询 --> 
    <tx:attributes> 
        <tx:method name="*" read-only="false" propagation="REQUIRED"/>  
        <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> 
    </tx:attributes> 
</tx:advice> 
<aop:config> 
 <!-- 配置切入点表达式 -->  
    <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/> 
</aop:config> 配置事物的通知引用事物管理器
配置切入点表达式和事务通知的对应关系
基于注解配置的声明式事物控制(配置方式) 重点
@Service("accountService") 
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS) 
public class AccountServiceImpl implements IAccountService { 
    
    。。。 
    //此操作需要读写
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false)
    @Override
    public void transfer(String sourceName, String targetName, Float money) {
        。。。
    }
}<!-- 开启 spring 对注解事务的支持 --> 
<tx:annotation-driven transaction-manager="transactionManager"/> 不使用xml的配置方式
@Configuration
@ComponentScan("com.liu")
@Import({JdbcConfig.class,TransactionConfig.class})
@PropertySource("jdbcConfig.properties")
@EnableTransactionManagement
public class SpringConfiguration {
}
public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    
    @Bean(name="jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
    @Bean(name="dataSource")
    public DataSource createDataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }
}
public class TransactionConfig {
    @Bean(name="transactionManager")
    public PlatformTransactionManager createTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}还没学
原文:https://www.cnblogs.com/liujiashun/p/12185450.html