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&amp;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 的实现的更多相关文章

  1. spring ioc aop 原理

    spring ioc aop 原理 spring ioc aop 的原理 spring的IoC容器是spring的核心,spring AOP是spring框架的重要组成部分. 在传统的程序设计中,当调 ...

  2. Spring IOC AOP的原理 如果让你自己设计IOC,AOP如何处理(百度)

    百度的面试官问,如果让你自己设计一个IOC,和AOP,如何设计, 我把IOC的过程答出来了,但是明显不对, (1) IOC 利用了反射,自己有个id,classtype,hashmap,所有的功能都在 ...

  3. spring Ioc Aop整合

    之前用DWP项目做spring的IOC,xml总是提示有问题,之后改用maven通过. 之后将这一块的内容补充. 仔细考虑一下spring 的IOC是无处不在的,演示Aop也需要依赖spring的IO ...

  4. spring ioc aop 理解

    OC,依赖倒置的意思,所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B,反正A要用到B,则A依赖于B.所谓倒置,你必须理解如果不倒置,会怎么着,因为A必须要有B,才可以调用B,如果 ...

  5. Spring IOC/ AOP 笔记

    扫描 Bean 以下主要是使用基于注解方式配置 组件扫描(一般用于自己写的类) 添加 @Component 注解,被扫描到后自动作为 Bean 组件 @ComponentScan 扫描配置的位置,将添 ...

  6. spring IOC DI AOP MVC 事务, mybatis 源码解读

    demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...

  7. spring之初识Ioc&Aop

    Spring框架的作用 spring是一个轻量级的企业级框架,提供了ioc容器.Aop实现.dao/orm支持.web集成等功能,目标是使现有的java EE技术更易用,并促进良好的编程习惯. Spr ...

  8. Spring学习笔记(二)Spring基础AOP、IOC

    Spring AOP 1. 代理模式 1.1. 静态代理 程序中经常需要为某些动作或事件作下记录,以便在事后检测或作为排错的依据,先看一个简单的例子: import java.util.logging ...

  9. 【转】spring - ioc和aop

    [转]spring - ioc和aop 1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对 ...

随机推荐

  1. 乱序优化与GCC的bug

      以下内容来自搜狗实验室技术交流文档,搜狐公司研发中心版权所有,仅供技术交流   摘要 --------- 乱序优化是现代编译器非常重要的特性,本文介绍了什么是乱序优化,以及由此引发的一个bug,希 ...

  2. LCA【洛谷P2971】 [USACO10HOL]牛的政治Cow Politics

    P2971 [USACO10HOL]牛的政治Cow Politics 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向 ...

  3. Oracle 11G R2在用EXP 导出时,空表不能导出解决办法

    11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法: 1.insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segmen ...

  4. chafen

    //f(u)>=f(v)+w //求最大值 跑最短路 v->u -w //求最小值 跑最长路 u->v w

  5. Rabbitmq相关学习网址

    1.安装文档: http://www.cnblogs.com/shuzhenyu/p/9823324.html 2.RabbitMq的整理 exchange.route.queue关系 https:/ ...

  6. 增加tomcat多实例

    第一步:解压 第二步:修改端口 /data/service/tomcat1/conf <Server port="8006" shutdown="SHUTDOWN& ...

  7. Spring实现AOP

    转载: https://blog.csdn.net/tolcf/article/details/49133119 [框架][Spring]XML配置实现AOP拦截-切点:JdkRegexpMethod ...

  8. Jenkins未授权访问脚本执行漏洞

    Jenkins未授权访问脚本执行漏洞 步骤 首先找一个站点挂上一个反弹shell脚本,然后在脚本执行框里执行脚本进行下载到tmp目录: println "wget http://47.95. ...

  9. set与map

    set /*set里面没有相同的元素 所以可以用于数组去重*/ //内部去重用的是=== 对象不等 但是 NaN等 //其本身是构造函数 let s=new Set([1,2,{},{},3,4,Na ...

  10. java——最大堆 MaxHeap

    使用数组来实现最大堆 堆是平衡二叉树 import Date_pacage.Array; public class MaxHeap<E extends Comparable <E>& ...