Spring基础(二)

配置文件加载数据库配置文件获得连接信息步骤
  1. 引入context命名空间和约束路径

命名空间:

xmlns:context="http://www.springframework.org/schema/context"

约束路径

http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
Spring基于注解的开发
  1. Spring原始注解:原始注解主要用于替代bean的配置

使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的bean需要进行扫描一边识别使用注解配置的类,字段和方法,如果没配置扫描路径就进行测试,会报noSuchBean异常

<context:component-scan base-package="com.no1"/>

@Autowired //按照数据类型从Spring容器中进行匹配的
@Qualifier(“userDao”) //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
@Resource(name=“userDao”) //@Resource相当于@Qualifier+@Autowired

Spring新注解

关于componentScan的几点说明:context:component-scan节点,属性base-package告诉spring要扫描的包,use-default-filters=”false”表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含@Service,@Component,@Repository,@Controller注解修饰的类,use-default-filters属性的默认值为true,这就意味着会扫描指定包下标有@Service,@Component,@Repository,@Controller的注解的全部类,并注册成bean。
所以如果仅仅是在配置文件中写<context:component-scan base-package=“com.sparta.trans”/> Use-default-filter此时为true时,那么会对base-package包或者子包下所有的java类进行扫描,并把匹配的java类注册成bean。

Spring集成Junit步骤
  1. 在pom.xml中集成Junit的坐标
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
  1. 使用@Runwith注解替换原来的运行期
  2. 使用@Configuration指定配置文件或配置类
  3. 使用@Autowired注入需要测试的对象
  4. 创建测试方法进行测试
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml")
@ContextConfiguration(classes = {SpringCofiguration.class})
public class SpringJunitTest {

    @Autowired
    private UserService userService;

    @Autowired
    private DataSource dataSource;

    @Test
    public void test1() throws SQLException {
        userService.save();
        System.out.println(dataSource.getConnection());
    }

}
Spring的AOP简介
  1. 作用和优势
    在程序运行期间,可以在不修改源码的情况下对方法进行增强,可以起到减少代码重复,提高开发效率,便于维护的作用。

  2. AOP的底层实现
    实际上,AOP的底层是通过Spring提供的动态代理机制技术实现的。在运行期间,Spring通过动态代理技术生成对象,代理对象方法执行时进行增强功能的介入,在调用目标对象的方法,从而完成功能的增强。

  3. AOP的相关概念
    在这里插入图片描述

  4. AOP使用流程
    (1)在配置文件中引入命名空间和约束路径
    命名空间:

xmlns:aop="http://www.springframework.org/schema/aop"

约束路径:

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

(2)导入AOP相关坐标

<!--aspectj是AOP技术的实现-->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.13</version>
    <scope>compile</scope>
</dependency>

(3)创建目标接口和目标类

//目标类
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running.....");
        //int i = 1/0;
    }
}
//目标接口
public interface TargetInterface {

    public void save();

}

(4)创建切面类

public class MyAspect {

    public void before(){
        System.out.println("前置增强..........");
    }

    public void afterReturning(){
        System.out.println("后置增强..........");
    }

    //Proceeding JoinPoint:  正在执行的连接点===切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强....");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强....");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强..........");
    }

    public void after(){
        System.out.println("最终增强..........");
    }

}

(5)将2.3交由Spring管理

(6)在配置文件中配置织入关系

<aop:config>
    <!--声明切面-->
    <aop:aspect ref="myAspect">
        <!--抽取切点表达式-->
        <aop:pointcut id="myPointcut" expression="execution(* com.no1.aop.*.*(..))"></aop:pointcut>
        <!--切面:切点+通知-->
        <!--<aop:before method="before" pointcut="execution(public void com.no1.aop.Target.save())"/>-->
        <!--<aop:before method="before" pointcut="execution(* com.no1.aop.*.*(..))"/>
        <aop:after-returning method="afterReturning" pointcut="execution(* com.no1.aop.*.*(..))"/>-->
        <!--<aop:around method="around" pointcut="execution(* com.no1.aop.*.*(..))"/>
        <aop:after-throwing method="afterThrowing" pointcut="execution(* com.no1.aop.*.*(..))"/>
        <aop:after method="after" pointcut="execution(* com.no1.aop.*.*(..))"/>-->
        <aop:around method="around" pointcut-ref="myPointcut"/>
        <aop:after method="after" pointcut-ref="myPointcut"/>
    </aop:aspect>
</aop:config>

(7)测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

    @Autowired
    private TargetInterface target;

    @Autowired
    private TargetInterface test;

    @Test
    public void test1(){
        target.save();
    }

    @Test
    public void test3(){
        test.save();
    }
}
Spring 事务控制
  1. 事务隔离级别(设置隔离级别,可以解决事务并发产生的问题,如脏读,不可重复读,虚读)
    ISOLATION-DEFAULT
    ISOLATION-READ-UNCOMMITTED 解决不了任何问题
    ISOLATION-READ-COMMITTED 解决脏读
    ISOLATION-REPEATABLE-READ 解决不可重复读
    ISOLATION-SERIALIZABLE 解决所有

  2. 事务传播行为
    required: 如果当前没有事务,就新建一个事务。如果当前存在事务,则加入到事务当中。
    supports: 支持当前事务,如果当前没有事务,则以非事务的方式进行
    mandatory: 使用当前事务,如果没有事务,就报异常
    requires_new: 新建事务,如果当前在事务中,就把当前事务挂起
    not_supported: 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
    never: 不支持事务,如果有事务,则报异常

  3. 什么是声明式事务控制
    简而言之就是在配置文件中声明,用在spring配置文件中声明式的处理事务来代替代码式的事务控制

  4. Spring 事务配置基本流程(基于xml)
    导入相应坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

在配置文件中导入约束路径和命名空间

xmlns:tx="http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

在配置文件中配置数据库





在配置文件中配置平台事务管理器

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

在配置文件中配置事务

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--设置事务的属性信息的-->
    <tx:attributes>
        <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
        <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
        <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
    <!--以update开头的所有方法-->
        <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>
<!--配置事务的aop织入-->
<aop:config>
    <aop:pointcut id="txPointcut" expression="execution(* com.no1.service.impl.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值