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
观众Audience.java
配置文件config.xml
测试程序Test.java
中间有些测试DI和没有完成的数据库代码,数据库我觉得还是使用ORM框架好一点,虽然Spring提供了一些模板方法,对数据库进行管理,不过使用起来没有ORM框架方便
源码:http://pan.baidu.com/s/1jGKnKv8
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
相关文章推荐
- java开发webservice的几种方式[待总结:cxf、xfire、axis2]
- How to change context root of a dynamic web project in Eclipse
- eclipse中Android项目架构
- JAVA bean与XML互转的利器---XStream
- java垃圾回收
- Thinking in Java -- 类型信息RTTI
- 封装SpringJdbcTemplate
- spring默认为单例模式
- Java String类型数据的字节长度
- Java中关于String学习总结
- eclipse快捷键
- java线程研究---(1)建立Thread和产生线程对象
- Spring3.1+Hibernate3+Struts2的最新整合所需要的jar包
- Spring学习笔记-类型转换器
- java动态代理(JDK和cglib)
- 从两个平方算法到分治算法-java
- Spring @Autowired,@Resource,@Required注解的用法和作用
- 使用Java代码将IP地址转换为int类型的方法
- springmvc对restful风格的实现
- eclipse启动闪退问题解决方法