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. Java与C++比较

    本文仅从片面的角度比较Java与C++的一些特性,如有错误的地方,请指正. 语言特性上的一些差异: 1.Java没有无符号整数,C++/C#都有. 2.Java中不存在指针.Java的引用是功能弱化的 ...

  2. 项目管理之码云和git

    目录 学习链接 1 码云 1 第一步,注册 2 第二部,登录 2 创建项目 2 git管理 4 如何生成公钥 5 clone项目 5 提交项目 6 1.本地初始化一个项目 6 2.开始第一次上传你的项 ...

  3. Miller-Rabin素数测试

    Miller-Rabin素数测试 给出一个小于1e18的数,问它是否为质数?不超过50组询问.hihocoder 我是真的菜,为了不误导他人,本篇仅供个人使用. 首先,一个1e18的数,朴素\(O(\ ...

  4. 扫描线-小Z的桌子

    大概题意:在一个01矩阵中找到一个周长最大的全0矩形. 这道题用的是扫描线,O(n^2),求最大面积的思路完全可以放在这里.下面说说思路. 首先,一个最大周长子矩形(最大周长全0矩形),左右两侧的列上 ...

  5. Java foreach remove问题分析

    原文链接:http://www.cnblogs.com/chrischennx/p/9610853.html 都说ArrayList在用foreach循环的时候,不能add元素,也不能remove元素 ...

  6. P5212 SubString LCT+SAM

    $ \color{#0066ff}{ 题目描述 }$ 给定一个字符串init,要求支持两个操作 在当前字符串的后面插入一个字符串 询问字符串ss在当前字符串中出现了几次?(作为连续子串) 强制在线. ...

  7. socket 释放全过程

    1.close()函数:立即返回到进程 int close(int sockfd);     //返回成功为0,出错为-1. close 一个套接字的默认行为是把套接字标记为已关闭,然后立即返回到调用 ...

  8. jq 使用手册

    翻译整理:Young.J官方网站:http://jquery.com jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写js变得更加方便!如果你不是 ...

  9. PHP命名空间 namespace 及导入 use 的用法

    命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种情况下只要避免命名重复就可以解决,最常见的一种做法是约定一个前缀. 在PHP中,出现 ...

  10. freemarker常用标签解释二

    1 assign 指令 使用这个指令你可以创建一个新的变量,或者替换一个已经存在的变量.注意仅仅顶级变量可以被创建/替换(也就是说你不能创建/替换some_hash.subvar,除了some_has ...