java Spring整合JDBC Spring使用存注解进行CRUD操作 使用Spring改造项目 Spring-Aop面向切面编程

Spring整合JDBC
    Spring框架中提供了一个可以操作数据库的对象,这个对象封装了对JDBC实现的细节,提供了一套模板,
这个模板类是JDBCTemplate,该类在Spring-jdbc.jar包中
    演示JdbcTemplate模板类对数据库的操作
    开发步骤:
        ○ 导包:Spring核心包,+Spring-jdbc+数据库驱动jar包+连接池jar包(C3P0)+spring-test测试包+Junit
        ○ 书写dao层接口 准备数据库user表
        ○ 书写dao层接口的 实现类
        ○ 书写CRUD增删改查方法
        ○ 使用JUnit进行单元测试
    properties配置jdbc连接信息
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql:///db_spring?characterEncoding=UTF-8
    jdbc.user=root
    jdbc.password=root
    <!--使用context:property-->
    <!--引入db.properties文件到Spring容器中-->
    <context:property-placeholder location="classpath:db.properties"/>



Spring使用纯注解进行CRUD操作
    ? @Configuration
    作用:就是用来代替spring的xml文件
    被该注解标记的类,是Spring的一个主配置类,当容器创建的时候会从该类上加载注解
    ? @ComponentScan
    作用:就是用来组件扫描的
    等价于:<context:component-scan base-package="com.zhiyou100"/>
    被该注解标记的类可以进行扫描,使用value属性来识别包名
    ? @PropertySource()
    作用:就是用来导入db.properties数据源文件到Spring容器中
    等价于:<context:property-placeholder location="classpath:db.properties"/>
    ? @Bean
    作用:用来在spring中构建对象
    等价于:<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    ? @Import
    作用:就是用来引入其他配置文件
    等价于<import resoure = "classpath:"/>


使用Spring改造项目
Spring-Aop面向切面编程
基于动态代理技术实现的,底层依赖于反射技术,对应用当中的一些公共代码作横向抽取,放到切面中(通知中)
专业术语:
    ? 连接点:JoinPoint  [目标对象]中[可以增强的][方法]称之为连接点
    ? 目标对象:Target 需要被增强的类对象
    ? 切入点:Pointcut目标对象中要增强的方法称之为切入点(必须)
    ? 通知/增强:Advice在[目标对象]中[要增强的方法]中[添加的增强的内容]称之为通知
    ? 织入:Weaving  将[通知][应用到连接点]的[过程]我们称之为织入
    ? 代理:Proxy 生成的代理对象 如:由Spring创建的UserService类对象通过动态代理技术生成的代理对象为代理
    ? 切面:切入点+通知=切面

    

AOP快速入门

代理模式

开发步骤:
    ? 准备Userservice接口
    ? 准备UserService接口的实现类
    ? 把UserService实现类交给Spring管理
        ○ 准备Spring的相关jar包
            § 除了Spring配置的常规jar包,还需要配置:
                □ aopalliance
                □ aspectweaver
                □ spring-aspects
        ○ 准备Spring的配置文件 xml
        ○ 把UserServiceImpl类通过<bean>方式配置到Spring的核心文件中aoolicationContext.xml
    ? 配置通知(目标对象中需要增强的内容)  
    交给Spring管理
    ? 配置切入点
    在Spring的主配置文件中配置切入点
    ? 通过切入点和通知配置切面  切面 = 切入点 + 通知
    借助于<aop:aspect>
    ? 测试采用Spring整合JUnit进行测试
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: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/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">

    <!--把UserServiceIml类对象配置到Spring容器中-->
    <!--<bean id="userService" class="com.zhiyou100.service.impl.UserServiceImpl"/>-->

    <!--组件扫描-->
    <context:component-scan base-package="com.zhiyou100"/>

    <!--把通知MyAdvice类对象配置到Spring容器中-->
    <bean id="myAdvice" class="com.zhiyou100.advice.MyAdvice"/>

    <!--配置AOP-->
    <aop:config>
        <!--配置切入点-->
        <!--要增强public void addUser() 方法名=包名+类名+方法名-->
        <aop:pointcut id="pct"
                      expression="execution(* com.zhiyou100.service..*.*(..))"/>
        <!--<aop:pointcut id="pct01"
                      expression="execution(public void com.zhiyou100.service.impl.UserServiceImpl.updateObject())"/>-->

        <!--
            expression 切入点表达式的改造过程;目的是为了下面的通知通配所有的切入点
            public void com.zhiyou100.service.impl.UserServiceImpl.addUser()
            //扩展UserServiceImpl其他的切入点方法
            //..代表统配方法中的所有参数
            //*代表该类中的所有方法
            public void com.zhiyou100.service.impl.UserServiceImpl.*(..)
            //扩展业务包下的其他类
            public void com.zhiyou100.service.impl.*.*(..)
            //扩展业务包下面的类,以及子包中的类 ..代表service当前包下面的类以及service包的子包的类
            public void com.zhiyou100.service..*.*(..) 切入点表达式到此为止 * com.zhiyou100.service..*.*(..)


            //
            public void com.zhiyou100..*.*(..)
            //
            public void *..*.*(..)
            //第一个*匹配方法中的所有返回值
            public * *..*.*(..)
            //权限修饰符可以在表达式中省略不写
            * *..*.*(..)
        -->




        <!--配置切面 切入点 + 通知-->
        <aop:aspect ref="myAdvice">
            <!--
                配置通知的目标方法 关联哪一个切入点
                实质上就是把before方法中的通知内容和addUser()切入点方法做一个绑定  形成切面
            -->
            <!--前置通知-->
            <aop:before method="before" pointcut-ref="pct"/>

            <!--后置通知-->
            <aop:after-returning method="afterReturning" pointcut-ref="pct"/>

            <!--异常拦截通知-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="pct"/>

            <!--环绕通知-->
            <!--<aop:around method="around" pointcut-ref="pct"/>-->

            <!--最终通知-->
            <aop:after method="after" pointcut-ref="pct"/>
        </aop:aspect>
    </aop:config>
</beans>

基于注解的AOP配置
开发步骤:
    ? 让UserServiceImpl目标对象被Spring管理到
    ? 让MyAdvice通知类也被Spring管理到
    ? 配置切面
        ○ 配置切入点
        ○ 要把切入点和通知进行绑定--->形成切面
        ○ 让当前的MyAdvice通知类
        ○ 开启Soring支持AOP注解
相关注解
//让当前的通知类具备有配置切面的条件
    @Aspect
//配置切入点
    @Pointcut("execution(* com.zhiyou100.service..*.*(..))")
//前置通知
    @Before("pointCut()")
 //后置通知
    @AfterReturning("pointCut()")
//异常拦截通知
    @AfterThrowing("pointCut()")
//环绕通知
    @Around("pointCut()")
 //最终通知
    @After("pointCut()")
前置通知  before()
后置通知  afterReturning() 如果切入带你有异常信息,则会中断
异常拦截通知  afterThrowing() 如果切入点中有异常信息,则会植入该方法中定义的增强内容
环绕通知  around()  前置通知+后置通知+异常拦截通知
最终通知  after()  他不管切入点中是否有异常信息,都会执行该方法定义的增强内容



Spring-TX事务->声明式事务


 
posted @ 2021-03-05 19:53  shangjinshuai  阅读(124)  评论(0)    收藏  举报