SSH整合 第四篇 Spring的IoC和AOP
这篇主要是在整合Hibernate后,测试IoC和AOP的应用。
1、工程目录(SRC)

2、IoC
1)、一个Service测试类
/*
* 加入spring容器
*/
private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
public static void main(String[] args) {
new UserServiceTest().testGetUserAndRole();
} public void test1() {
UserService userService= (UserService) applicationContext.getBean("userService");
User user = userService.getById("u1");
System.out.println(user.getId()+"---"+user.getUsername());
}
UserServiceTest
2)、UserServiceImpl
从User user = userService.getById("u1");调用UserServiceImpl。(下文都省略贴接口)。
再看看UserServiceImpl

那么,spring容器应该是怎么样设置的呢?

其他,同理。
3)、组件扫描
对于一个个bean地配置,可能有点麻烦,可以使用组件扫描
如,RoleServiceImpl

xml文件中,则是

3、AOP
这里是事务管理方面的应用。Spring中的设置如下。
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- AOP 切点-->
<aop:config >
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.xzw.ssh.service.impl.*.*(..))" />
</aop:config> <!--事务的传播特性-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<!--默认其他方法都是REQUIRED-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
事务
注意
在使用AOP的时候,之前导入的包还是不够,会有出现“找不到类”的提示,关键字,org/aopalliance/intercept/MethodInterceptor和org/aspectj/weaver,对应加入aopalliance-1.0.jar和aspectjweaver.jar
至此,spring和hibernate的整合基本完成。
SSH整合 第四篇 Spring的IoC和AOP的更多相关文章
- SSH整合 第三篇 Spring的加入
1.思路和想法. 目前理解到的,觉得是的,可能的,应该这样的……………… Spring的两大核心是IoC和AOP Ioc:帮助实例化对象,加载到容器中,在注入到需要用到的地方.这样就可以减少在不同的方 ...
- Spring框架IOC和AOP介绍
说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...
- ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点
Web层用Structs2的action Service层用Spring的IoC和aop以及JdbcTemplate或者Transaction事务(创建对象及维护对象间的关系) Dao层用Hibern ...
- 展开说说,Spring Bean IOC、AOP 循环依赖
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 延迟满足能给你带来什么? 大学有四年时间,但几乎所有人都是临近毕业才发现找一份好工作 ...
- Spring 的IOC和AOP总结
Spring 的IOC和AOP IOC 1.IOC 许多应用都是通过彼此间的相互合作来实现业务逻辑的,如类A要调用类B的方法,以前我们都是在类A中,通过自身new一个类B,然后在调用类B的方法,现在我 ...
- Spring 的IOC 和Aop
Spring 的IOC 和Aop
- # Spring 练习ioc 、aop
Spring 练习 通过学习spring的基础知识,了解了Spring为了降低Java开发的复杂性,采取了以下4种关键策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: ...
- Spring的IOC和AOP之深剖
今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...
- spring的IOC和AOP
spring的IOC和AOP 1.解释spring的ioc? 几种注入依赖的方式?spring的优点? IOC你就认为他是一个生产和管理bean的容器就行了,原来需要在调用类中new的东西,现在都是 ...
随机推荐
- 在Linux下判断系统当前是否开启了超线程
#lscpu Thread(s) per core: 2Core(s) per socket: 6Socket(s): 2 -------------------------------------- ...
- CentOS开机卡在进度条,无法正常开机的排查办法
CentOS开机的时候卡在进度条一直进不去 重启,按f5键进度条/命令行界面方式切换,确认卡问题后处理就好 我这边卡在redis服务,设置为开机启动但是一直服务启动不起来 重启按住"e&qu ...
- iBatis与Hibernate有什么不同?
相同点:屏蔽jdbc api的底层访问细节,使用我们不用与jdbc api打交道,就可以访问数据. jdbc api编程流程固定,还将sql语句与java代码混杂在了一起,经常需要拼凑sql语句,细节 ...
- proxychains 安装
一.安装下载源码: git clone https://github.com/rofl0r/proxychains-ng 编译和安装: cd proxychains-ng ./configure -- ...
- R及Rstuio下载及配置,及基本使用介绍
1.R和Rstudio下载地址 https://cran.rstudio.com/a 2.Rstudio 的配置 外观.代码显示比例配置 选中tools 选中globle options 选中appe ...
- ubuntu下搭建testlink
环境配置: 1. 安装mysql 教程网上找 2. 安装apache sudo apt-get install apache2 重启apache服务 sudo /etc/init.d/apache2 ...
- poj1661 (DP)
题目链接:http://poj.org/problem?id=1661 思路: 把初始位置看成左,右端点均为x0,即长度为0,高度为y0的一个平台,按照平台高度从低到高排序.用dp[i][0],dp[ ...
- [leetcode]257. Binary Tree Paths二叉树路径
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- AIDL--------应用之间的通信接口
在下面例子中04Service中添加aidl包包里定义好接口 接口文件名后缀为.aidl package com.example.aidl; interface IRemoteService{ voi ...