您的位置:首页 > 编程语言 > Java开发

Spring IOC + AOP 的实现

2015-08-07 10:57 405 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: