Spring 使用JDBC
来源:互联网 发布:spss筛选数据统计 编辑:程序博客网 时间:2024/06/15 06:58
---恢复内容开始---
注解配置AOP
项目路径:E:\JavaWebSrc\FirstSpringAOP
1:接口代码
接口为 IPerson ,接口不需要写注释
public interface IPerson { public void sayNmae(); public void introduceOneSelf();}
2:实体类代码
student继承了IPerson接口
//将student实例化。装配到spring容器中
@Component("student")public class Student implements IPerson { @Value(value = "1") private int id; @Value(value = "余文辉") private String name; @Value(value = "男") private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; }}
3:切面类
这里使用的注解需要导入(aopaliance.jar和 aspectjweaver.jar 两个jar包)
//切面声明@Aspect//装配到spring容器中@Componentpublic class LoggingAspect {// 声明切入点 声明切入点表达式 @Pointcut("execution(* com.yuwenhui.annotation.Student.* (..))") public void JoinPointExpecssion(){};//前置通知 @Before("JoinPointExpecssion()") public void beforMethod(JoinPoint joinPoint){// 获得方法名 String name = joinPoint.getSignature().getName();// 获得参数列表,再将参数数组转换成List集合 List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("前置通知"); System.out.println("参数:"+args); }//后置通知 @After("JoinPointExpecssion()") public void afterMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("后置通知"); System.out.println("参数:"+args); }// 返回通知,在方法正常结束的时候返回,且 必须要有一个返回值 @AfterReturning(value = "JoinPointExpecssion()",returning = "result") public void afterMethodRetuning(JoinPoint joinPoint,Object result){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("返回通知"+ result); }// 异常通知 ,将在方法抛出异常时触 @AfterThrowing(value = "JoinPointExpecssion()",throwing ="e") public void afterThrowing(JoinPoint joinPoint,Exception e){ String name = joinPoint.getSignature().getName(); System.out.println("返回通知"); System.out.println("异常信息"+e); }}
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描指定包下注释 -->
<context:component-scan base-package="com.yuwenhui.annotation"></context:component-scan>
<!-- 声明Aspect配置 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试类
public class TestAop { @Test public void testAop(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); IPerson student= (IPerson) applicationContext.getBean("student"); student.sayNmae(); student.introduceOneSelf(); }}
XML配置AOP
1:接口配置
不管是XML配置还是annotation配置,接口类都不需要改变
public interface IPerson { public void sayNmae(); public void introduceOneSelf();}
2:实体类配置
public class Student implements IPerson { private int id; private String name; private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}
3:切面类
public class LoggingAspect { public void JoinPointExpecssion(){}; public void beforMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("前置通知"); System.out.println("参数:"+args); } public void afterMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("后置通知"); System.out.println("参数:"+args); } public void afterMethodRetuning(JoinPoint joinPoint,Object result){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("返回通知"+ result); } public void afterThrowing(JoinPoint joinPoint,Exception e){ String name = joinPoint.getSignature().getName(); System.out.println("返回通知"); System.out.println("异常信息"+e); }}
4:测试类
public class TestXml { @Test public void testAop(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-beanxml.xml"); IPerson student= (IPerson) applicationContext.getBean("student"); student.sayNmae(); student.introduceOneSelf(); }}
5:配置文件
需要格外注意
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 需要注意xml头,不是IDEA自动生成的 --> <!-- 配置实体类,并为相关字段初始化 --> <bean id="student" class="com.yuwenhui.xml.Student"> <property name="name" value="余文辉"></property> <property name="id" value="1"></property> <property name="sex" value="男"></property> </bean> <!-- 声明切面类,这里只是声明为普通的bean类 --> <bean id="loggingAspect" class="com.yuwenhui.xml.LoggingAspect"></bean> <!-- 配置aop --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut id="pointcut" expression="execution(* com.yuwenhui.xml.Student.* (..))"/> <!-- 这里才是真正的声明切面 --> <aop:aspect ref="loggingAspect"> <!--前置通知--> <aop:before method="beforMethod" pointcut-ref="pointcut"/> <!--后置通知--> <aop:after method="afterMethod" pointcut-ref="pointcut"/> <!--返回通知--> <aop:after-returning method="afterMethodRetuning" pointcut-ref="pointcut" returning="result"/> <!--异常通知--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/> </aop:aspect> </aop:config></beans>
---恢复内容结束---
阅读全文
0 0
- Spring JDBC-使用Spring JDBC访问数据库
- spring中使用jdbc
- 使用Spring Jdbc (1)
- 使用Spring Jdbc (2)
- 使用Spring Jdbc (3)
- Spring JDBC 的使用
- Spring中使用JDBC
- Spring中使用JDBC
- spring中使用jdbc
- 使用Spring 的JDBC
- 使用Spring 的JDBC
- Spring里使用JDBC
- Spring jdbc 的使用
- Spring+jdbc使用示例
- Spring中使用Jdbc
- SPRING JDBC详细使用
- spring jdbc 使用感想
- spring jdbc的使用
- [剑指offer]python从上到下打印二叉树【面试题9.15】
- Java基础知识——面向对象、数组、常用类
- VIEW
- 关于部署好tomcat的环境下 jsp文件在eclipse中能运行,但是在浏览器打开时显示404错误的个人笔记
- window安装Python环境
- Spring 使用JDBC
- 数据结构-双向链表插入
- PAT_A 1059. Prime Factors (25)
- HDU 6138 AC自动机
- yii2.0 的安装
- 计算结构体的大小
- 【PAT】【Advanced Level】1057. Stack (30)
- OKHTTP3的简单使用,表单提交,json传递
- 阿里云服务器部署个人博客