Spring整合Juint类和aop技术还有Jdbc模板

=====================整个juint========================

//帮我们创建容器

 

@RunWith(SpringJUnit4ClassRunner.class)
//指定创建容器使用的是那个配置文件
@ContextConfiguration("classpath:applicationContext.xml")
public class td {


@Resource(name="aaa")
private aaa a;

@Test
public void fun1() {
System.out.println(a);

 

}

 

 

=========================Aop技术====================================



<!-- joinpoint(连接点):目标对象中,所有可以增强的方法 -->
<!-- pointcut(切入点):目标对象,已经增强的方法 -->
<!-- Advice(通知/增强):增强的代码 -->
<!-- Target(目标对象):被代理对象 -->
<!-- weaving(织入):将通知应用到切入点的过程 -->
<!-- proxy(代理):将通知织入到目标对象之后,形成代理对象 -->
<!-- aspect(切面):切入点+通知 -->







<!-- 准备工作,导入aop命名空间 -->
<!-- 配置目标对象 -->
<bean name="aaa" class="Spring_a.aaa"></bean>
<!-- 配置通知对象 -->
<bean name="MyAdvice" class="Spring_a.MyAdvice"></bean>
<!-- 配置将通知植入到目标对象 -->
<aop:config>
<!-- 配置切入点 
public void Spring_a.aaa.fun1()
void Spring_a.aaa.fun1()可以省略public
* Spring_a.aaa.fun1()void返回值有点局限,可以用*表示,取代所有的返回值
* Spring_a.aaa.*(..)这个方法名字,也可以用*表示,点点是任意参数
-->
<aop:pointcut expression="execution(* Spring_a.aaa.*(..))" id="pc"/>
<aop:aspect ref="MyAdvice">
<!-- 指定名为before的方法作为前置通知 -->
<aop:before method="before" pointcut-ref="pc"/>
<aop:after-returning method="after" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
<aop:after-throwing method="afterEx" pointcut-ref="pc"/>
<aop:after method="afterq" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>

 

 

======================JDBC模板(XML)=====================

 

<!-- 配置事物通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 方法名字,隔离级别,那个是无脑配置,只读 -->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 配置织入 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* dao.*(..))" id="txpc"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc"/>
</aop:config>

<!-- 读取外面的数据库配置文件 -->
<context:property-placeholder location="classpath:*.properties"/>
<!-- 将连接池注入到spring容器 -->
<bean name="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///hibernate_test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="12345678"></property>
</bean>
<!-- 将JDBCTemplate放入到spring容器中 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"></property>
</bean>

 

 



======================JDBC模板(注解)=====================

 

在方法上面加上注解


@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)

======================JDBC模板(XML)=====================

如果你喜欢,或者有帮助到你的地方可以关注我的微信公众号,田园折耳,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值