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. [51nod1222] 最小公倍数计数(莫比乌斯反演)

    题面 传送门 题解 我此生可能注定要和反演过不去了--死都看不出来为啥它会突然繁衍反演起来啊-- 设\(f(n)=\sum_{i=1}^n\sum_{j=1}^n[{ij\over\gcd(i,j)} ...

  2. P3768 简单的数学题 杜教筛+推式子

    \(\color{#0066ff}{ 题目描述 }\) 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数n和一个整数p,你需要求出(\(\sum_{i=1}^n\sum_{j=1}^n ij ...

  3. C++基础学习9:构造函数和析构函数

    1.  构造函数用来对类对象进行初始化,它完成对内存空间的申请.赋初值等工作.  2.  析构函数主要是用来做清理工作的. 补充:函数名或变量名前面有"::"但是没有类名,说明这是 ...

  4. MVC与三层的区别

    闲来无事,想了想MVC与三层的区别,根据自己的经验,上图 由此来看,其实这两种框架(结构)的分层方式其实没什么联系,也没有什么可比性,但他们的目的都一样的:解耦 --Ones

  5. CF796C Bank Hacking 思维

    Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search f ...

  6. COM编程快速入门

    COM编程快速入门 COM编程快速入门 http://www.vckbase.com/index.php/wv/1642   COM是一种跨应用和语言共享二进制代码的方法.与C++不同,它提倡源代码重 ...

  7. spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页

    controller: /**  * 分页查询用户  * @param request  * @param response  * @return  * @throws Exception  */ @ ...

  8. Jury Jeopardy (这是一道单纯的模拟题)

    Description What would a programming contest be without a problem featuring an ASCII-maze? Do not de ...

  9. Experimental Educational Round: VolBIT Formulas Blitz F

    Description One company of IT City decided to create a group of innovative developments consisting f ...

  10. node js linux / OS 安装

    rm -rf 删除文件夹名字rm -rf 软连接名称 1.安装taryum install -y tar 3. 下载node https://nodejs.org/en/download/ 4. 拷贝 ...