Spring and junit4 integration

Spring and Junit4 integration

@RunWith(SpringJUnit4ClassRunner.class)

Let the tests run in the Spring test environment

 

@ContextConfiguration When Spring integrates JUnit4 tests, use annotations to introduce multiple configuration files

 

single file

@ContextConfiguration(locations="../applicationContext.xml") 

 

@ContextConfiguration(classes =SimpleConfiguration.class)

 

When there are multiple files, {} can be used

 

@ContextConfiguration(locations = {"classpath*:/spring1.xml", "classpath*:/spring2.xml" })

 

Annotation configuration in Xml

<context:annotation-config>: Annotation scanning is for beans that have been registered in the Spring container

<context:component-scan>: not only has all the functions of <context:annotation-config>, but also can scan the corresponding beans under the specified package

<context:component-scanbase-package="com.test"/>

When present with <context:component-scan>, the former will be ignored.

Even if the bean is registered and the <context:annotation-config /> scan is enabled at the same time, the injection annotations such as @autowire and @resource will only be injected once, that is, only loaded once.

 

Aop

1. Create a facet class

2、@Aspect

xml configuration scan <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3. Aspect class added (entry point and enhancement)

 

 

pre-enhancement

@Before("execution(* com.igeekhome.anli.UserDaoImpl.updateUser(..))")

   publicvoid updateUser() {

      System.out .println ( " Authorization Verification " );

   }

 

rear boost

// public entry point

   @Pointcut("execution(* com.igeekhome.anli.UserDaoImpl.*(..))")

   privatevoid myPointcut() {}

  

   @AfterReturning(pointcut="myPointcut()", returning="ret")

   publicvoid deleteUser(Object ret) {

      System.out .println ( " Delete successful: ......+ return:" + ret );

   }

 

Surround Enhancement:

@Around("myPointcut()")

   public Object transactional (ProceedingJoinPoint jp) throws Throwable {

System.out       .println ( " Open the thing " );

      Object object=jp.proceed();

      System.out.println ( " Commit transaction " );

      returnobject;

   }

abnormal enhancement

@AfterThrowing(pointcut="myPointcut()", throwing="ret")

   publicObject execption(Object ret) {

     

      System.out.println ( " Exception Enhancement " );

      returnret;

   }

final enhancement

@After("execution(* com.igeekhome.anli.UserDaoImpl.saveUser(..))")

   public void finall () {

     

      System.out.println ( " Final Enhancement " ) ;

     

   }

 

Jdbc

1. The Dao class (the class that writes the sq statement) is succeeded by the JdbcDaoSupport class

<!-- Load property configuration file -->

<context:property-placeholder location="classpath:db.properties"/>

<!--配置数据源-->

<bean id="dataSource" class=" com.alibaba.druid.pool.DruidDataSource">

    <property name="driverClassName" value="${jdbc.className}"></property>

    <property name="url" value="${jdbc.url}"></property>

    <property name="username" value="${jdbc.username}"></property>

     <property name="password" value="${jdbc.password}"></property>

    <property name="initialSize" value="${jdbc.initialSize}"></property>

    <property name="maxActive" value="${jdbc.maxActive}"></property>

    <property name="minIdle" value="${jdbc.minIdle}"></property>

</bean>

 

<!-- Dao实现层继承jdbcdaosupper不需要配置jdbcTemplate模板工具类-->

<!--配置jdbcTemplate模板工具类 

 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

    <property name="dataSource" ref="dataSource"></property>

</bean>-->

<!-- Dao实现层依赖dataSource必须写-->

 <bean id="accountDAO" class="com.igeekhome.spring.AccountImpl">

          <property name="dataSource" ref="dataSource"></property>

    </bean>

2、 

3、Xml配置db.properties文件和数据源

 

<!-- 加载属性配置文件 -->

   <context:property-placeholder location="classpath:db.properties"/>

<!--配置数据源-->

   <bean id="dataSource" class=" com.alibaba.druid.pool.DruidDataSource">

      <property name="driverClassName" value="${jdbc.className}"></property>

      <property name="url" value="${jdbc.url}"></property>

      <property name="username" value="${jdbc.username}"></property>

      <!--  <property name="password" value="${jdbc.password}"></property>-->

      <property name="initialSize" value="${jdbc.initialSize}"></property>

      <property name="maxActive" value="${jdbc.maxActive}"></property>

      <property name="minIdle" value="${jdbc.minIdle}"></property>

   </bean>

事务

1、xml内容

    <!-- 配置事务管理器 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource" ref="dataSource"></property>

    </bean>

 

    <!-- 开启事务注解的支持 -->

    <tx:annotation-driven transaction-manager="transactionManager"/>

3、在需要事物的类或方法上写上注解

如果 @Transactional 标注在 Class 上面,那么将会对这个 Class 里面所有的 public 方法都包装事务方法。等同于该类的每个公有方法都放上了@Transactional。

如果某方法需要单独的事务定义,则需要在方法上加@Transactional来覆盖类上的标注声明。记住:方法级别的事务覆盖类级别的事务,一般我们在企业开发的时候,类上面定义的事务为只读属性(@Transactional(readOnly=true)),增删改的方法上定义的事务为可写属性(@Transactional(readOnly=false))!查找的方法上定义的事务为可写属性(@Transactional(readOnly=true))!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325520087&siteId=291194637