Spring IOC + AOP 的实现
Spring思想很不错,尽量减少侵入式编程。现在了解到的Spring提供的功能有,DI,IOC,数据库操作,AOP,MVC。针对DI,AOP写了一些小DEMO
PS:AOP真的很棒
代码参考:《Spring实战》第三版
环境:
win7 64
ide eclipse
jdk 1.8
spring 4.1.7
------------------------------------------------我是代码分割线---------------------------------------------------------
思路:
一个表演者和观众,表演者和观众通过DI注入(XML和注解注入),观众在表演者表演前,时,后的动作通过AOP实现(数据库写了一些配置文件)
表演者Performer.java
package com.gxf.beans; /**
* 表演者
* @author GXF
*
*/
public class Performer { /**
* 表演者表演行为
*/
public void perform(){
System.out.println("表演者开始表演");
}
}
观众Audience.java
/**
* 观众
* @author GXF
*
*/
public class Audience { /**
* 表演前观众开始找座位
*/
public void takeSeats(){
System.out.println("观众开始找座位");
} /**
* 表演前观众关机
*/
public void turnOffCellPhone(){
System.out.println("观众将手机关机");
} /**
* 表演结束观众开始鼓掌
*/
public void applaud(){
System.out.println("观众开始鼓掌");
} /**
* 表演失败,观众要求退钱
*/
public void demandRefund(){
System.out.println("嘘!退票!");
}
}
配置文件config.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="user" class="com.gxf.beans.User">
<property name="name">
<value>zhangsan</value>
</property>
</bean>
<bean id="phone" class="com.gxf.beans.Phone">
<property name="number">
<value>13608196502</value>
</property>
</bean>
<!-- 配置表演者和观众的bean -->
<bean id="performer" class="com.gxf.beans.Performer">
</bean>
<bean id="audience" class="com.gxf.beans.Audience"></bean>
<!-- 配置面向切面 -->
<aop:config>
<aop:aspect ref="audience">
<aop:before pointcut="execution(* com.gxf.beans.Performer.perform(..))" method="takeSeats"/>
<aop:after pointcut="execution(* com.gxf.beans.Performer.perform(..))" method="applaud"/>
</aop:aspect> </aop:config>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>12345</value>
</property>
</bean>
</beans>
测试程序Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gxf.beans.Performer; public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
Performer performer = (Performer) context.getBean("performer");
performer.perform(); } }
中间有些测试DI和没有完成的数据库代码,数据库我觉得还是使用ORM框架好一点,虽然Spring提供了一些模板方法,对数据库进行管理,不过使用起来没有ORM框架方便
源码:http://pan.baidu.com/s/1jGKnKv8
Spring IOC + AOP 的实现的更多相关文章
- spring ioc aop 原理
spring ioc aop 原理 spring ioc aop 的原理 spring的IoC容器是spring的核心,spring AOP是spring框架的重要组成部分. 在传统的程序设计中,当调 ...
- Spring IOC AOP的原理 如果让你自己设计IOC,AOP如何处理(百度)
百度的面试官问,如果让你自己设计一个IOC,和AOP,如何设计, 我把IOC的过程答出来了,但是明显不对, (1) IOC 利用了反射,自己有个id,classtype,hashmap,所有的功能都在 ...
- spring Ioc Aop整合
之前用DWP项目做spring的IOC,xml总是提示有问题,之后改用maven通过. 之后将这一块的内容补充. 仔细考虑一下spring 的IOC是无处不在的,演示Aop也需要依赖spring的IO ...
- spring ioc aop 理解
OC,依赖倒置的意思,所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如果 ...
- Spring IOC/ AOP 笔记
扫描 Bean 以下主要是使用基于注解方式配置 组件扫描(一般用于自己写的类) 添加 @Component 注解,被扫描到后自动作为 Bean 组件 @ComponentScan 扫描配置的位置,将添 ...
- spring IOC DI AOP MVC 事务, mybatis 源码解读
demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...
- spring之初识Ioc&Aop
Spring框架的作用 spring是一个轻量级的企业级框架,提供了ioc容器.Aop实现.dao/orm支持.web集成等功能,目标是使现有的java EE技术更易用,并促进良好的编程习惯. Spr ...
- Spring学习笔记(二)Spring基础AOP、IOC
Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...
- 【转】spring - ioc和aop
[转]spring - ioc和aop 1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对 ...
随机推荐
- 【bzoj3122】: [Sdoi2013]随机数生成器 数论-BSGS
[bzoj3122]: [Sdoi2013]随机数生成器 当a>=2 化简得 然后 BSGS 求解 其他的特判 : 当 x=t n=1 当 a=1 当 a=0 判断b==t /* http: ...
- 洛谷P3604 美好的每一天(莫队)
传送门 由乃的题还是一如既往的可怕…… 先放上原题解 标解: 一个区间可以重排成为回文串,即区间中最多有一个字母出现奇数次,其他的都出现偶数次 发现这个和 类似 这样如果一个区间的 和为 或者 ...
- cenos php执行pdf2swf 配置环境
1.第一步:安装xpdf语言包 1.mkdir –p /usr/share/xpdf 2.cd /usr/share/xpdf/ 3.下载中文支持及字体库wget ftp://ftp.foolabs. ...
- Java foreach remove问题分析
原文链接:http://www.cnblogs.com/chrischennx/p/9610853.html 都说ArrayList在用foreach循环的时候,不能add元素,也不能remove元素 ...
- springboot junit单元测试报错
1.测试类中如下方框为主函数 2.application.yml注意如下2个地方 3.主函数
- CompletionService详解
public interface CompletionService<V> { Future<V> submit(Callable<V> task); Future ...
- Jenkins 相关
手动下载Jenkins plugin 的地址, 下载后的是zip 文件,然后再手动修改为.hpi 文件,然后再手动上传 https://plugins.jenkins.io/
- 整合feign过程中出现问题:
一:编译器报错 这个地方是由于没有被spring管理,编译器报错,可以选择加上@comoponent这个注解 也可以选择不加,启动程序是不会报错的. 二 报错为空: org.springframewo ...
- ubuntu下vnc部署安装
ubuntu下vnc部署安装,参考如下博客:https://www.cnblogs.com/xuliangxing/p/7642650.html https://jingyan.baidu.com/a ...
- fsockopen函数被禁用的解决方法
判断fsockopen 是否可用:function_exists('fsockopen');如果没有开启 一.开启fsockopen函数 修改php.ini,将 disable_functions = ...