Spring与 Junit4 整合
@RunWith(SpringJUnit4ClassRunner.class)
让测试运行于Spring测试环境
@ContextConfiguration Spring整合JUnit4测试时,使用注解引入多个配置文件
单个文件
@ContextConfiguration(locations="../applicationContext.xml")
@ContextConfiguration(classes =SimpleConfiguration.class)
多个文件时,可用{}
@ContextConfiguration(locations = {"classpath*:/spring1.xml", "classpath*:/spring2.xml" })
Xml中注解配置
<context:annotation-config>:注解扫描是针对已经在Spring容器里注册过的Bean
<context:component-scan>:不仅具备<context:annotation-config>的所有功能,还可以在指定的package下面扫描对应的bean
<context:component-scanbase-package="com.test"/>
和<context:component-scan>同时存在的时候,前者会被忽略。
即使注册Bean,同时开启<context:annotation-config />扫描,@autowire,@resource等注入注解只会被注入一次,也即只加载一次
Aop
1、创建切面类
2、@Aspect
xml配置扫描<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3、切面类添加(切入点和增强)
前置增强
@Before("execution(* com.igeekhome.anli.UserDaoImpl.updateUser(..))") publicvoid updateUser() { System.out.println("权限验证"); } |
后置增强
//公共切入点 @Pointcut("execution(* com.igeekhome.anli.UserDaoImpl.*(..))") privatevoid myPointcut() {}
@AfterReturning(pointcut="myPointcut()", returning="ret") publicvoid deleteUser(Object ret) { System.out.println("删除成功:......+ return:"+ret); } |
环绕增强:
@Around("myPointcut()") public Object transactional (ProceedingJoinPoint jp) throws Throwable { System.out.println("开启事物"); Object object=jp.proceed(); System.out.println("提交事务"); returnobject; } |
异常增强
@AfterThrowing(pointcut="myPointcut()", throwing="ret") public Object execption(Object ret) {
System.out.println("异常增强"); returnret; } |
最终增强
@After("execution(* com.igeekhome.anli.UserDaoImpl.saveUser(..))") publicvoid finall() {
System.out.println("最终增强");
} |
Jdbc
1、Dao类(写sq语句的类)继成JdbcDaoSupport 类
<!-- 加载属性配置文件 --> <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))!