jBeanBox第一版终于完成了,已更新到项目目录https://sourceforge.net/projects/jbeanbox/ 下,有兴趣的可以看一看。
这个项目的主要特点:
1)简单,只用一个Java文件实现了IOC/AOP功能,源码不到350行,由此可见实现IOC/AOP实际上很简单。
2) 用Java类代替XML作为配置文件,Java作为配置文件的主要优点是IDE支持类名检查、重构。其实这也是这个项目如此小的原因,因为不用考虑读取和翻译XML文件的问题。
3) 支持所有注入方式如构造器注入、工厂注入等; AOP功能简化,只支持Around方法和切点的正则表达式,但是已能应付大多场合。
4)本项目以功能单一、源码精简为目的,功能仅限于IOC/AOP。 事务支持、DAO支持等业务功能不考虑扩充进来。
目前项目已完成,进入排错阶段,欢迎大家来挑错和试用,谢谢!(Email: Yong9981@gmail.com ) 因为发布在sourceForge,是英文介绍,就对付着看吧。
本贴的附件和网站上的是一样的,打了一个war包,可以直接导入到Eclipse,其中用到了asm-x.x.x.jar和cglib-x.x.jar两个第三方库也打进包了。
jBeanBox1.0.1 is a micro scale(1 file) but full functional IOC & AOP tool, it has no XML files(Use "BeanBox" java classes as configuration), no annotations. 1 single Java class ~350 lines source code do all the IOC/AOP job like Spring framework IOC/AOP core did.
jBeanBox is an open source project follows BSD license, project address: https://sourceforge.net/projects/jbeanbox/
(Note: 2 jar files asm-x.x.x.jar and cglib-x.x.jar are still needed for this project, it's used for create proxy beans similar like Spring did.)
Why jBeanBox?
Some other IOC/AOP frameworks problem:
1) Spring, HiveMind and other IOC/AOP tools use XML as configuration file: XML does not support class name spelling check & IDE refactoring, hard to create/change configuration in run-time.
(I noticed from Spring3.0, it started to use a kind Java class to replace XML, but Java mixed up with annotations, looks weird, and it's hard to create/change configuration at run time.)
About Spring, here are some pros & cons: http://www.aksindiblog.com/spring-framework-advantages-disadvantages.html. For my opinion, as an IOC/AOP tool, it’s too fat(more than 700 classes of core functions).
2) Guice and other IOC/AOP tools depend on annotation: write annotation in Java classes is invasion, IOC/AOP tool should better not change Java files.
jBeanBox is a micro IOC/AOP tool, only focus on IOC/AOP. Its source code are very short and easy to understand. It's ideal to mobile device, small projects, and of cause can be used anywhere where a tiny IOC/AOP tool needed.
Key features of jBeanBox:
1) Simple, only 1 short java file do all the IOC/AOP job. No XML files, no annotations, use pure Java classes as configurations, these Java classes be called "BeanBox" (like "Box" class in another project "jWebBox", in that project 1 single Java file did all jobs Tiles did; It seems using "Box-Oriented Programming” can often make project very short). It's easy to learn and to debug, any problem you can solve by yourself after look into the 350 lines source codes.
2) Compare to XML, Java configurations is not easy to read (but still some programmer like Java, otherwise why Spring invented Java configurations?), but the advantage is it can be organized in packages, and UML tool can used to draw class relationship, and most of all, IDE support class name spelling check and refactoring.
3) Bean configurations (BeanBoxes) can be created dynamically, it's easy to modify or create configurations at run-time, testing become simple and quick, testing configurations(if organized in packages) can be reused in production environment.
4) Small size, ideal for immigrate to mobile device.
How to use it:
For convenient, this project be packaged as .war file, you can throw it into Tomcat or Jboss webapps folder, or import into Eclipse as a web project to see the source code, and run "MainTest.java" to see the test result. There are 2 jar files "asm-3.x.x.jar" and "cglib-2.x.jar" packed in the war package but you can replace these 2 jars by downloading from their websites. There are some test classes in this war package, but for a new project, only 1 java file "BeanBox.java" and 2 libs (asm-3.x.x.jar and cglib-2.x.jar) are needed.
Typically a project using jBeanBox, need prepare below files:
1. Your bean or interface classes, advoice classes(AOP), for example:
public interface Iitem {
public void doPrint();
}
public class ItemImpl implements Iitem {
public void doPrint() {
System.out.println("Do print");
};
}
public class Tester {
private Iitem item;
//gette & setter
public void doPrintItem() {
item.doPrint();
}
}
public class LogAdvicor {//This is an advicor sample
private String name;
public Object doAround(Object[] args) {
System.out.println("=====logger " + name + ", Before method: " + args[2] + "=====");
Object o = BeanBox.invoke(args);
System.out.println("=====logger " + name + ", After method: " + args[2] + "=====");
return o;
}
//getter&setter
}
2. Configuration classes (The substitute of XML):
class TesterBox extends BeanBox {
{
this.setClassOrValue(Tester.class);
this.setProperty("item", new BeanBox(ItemImpl.class));
}
}
3. At last, add point-cut and get bean by call getBean() method:
public static void main(String[] args) {
BeanBox.addAdvicor("com.test.\\w*.\\w*", "doPrint\\w*", new BeanBox(LogAdvicor.class).setProperty("name", "Demo"), "doAround");
Tester t = (Tester) new TesterBox().getBean();
t.doPrintItem();
}
The result:
=====logger Demo, Before method: public void com.test.anotherTest.Tester.doPrintItem()=====
=====logger Demo, Before method: public void com.test.anotherTest.ItemImpl.doPrint()=====
Do print
=====logger Demo, After method: public void com.test.anotherTest.ItemImpl.doPrint()=====
=====logger Demo, After method: public void com.test.anotherTest.Tester.doPrintItem()=====
More usage like Construction injection, Static Factory injection & Bean Factory injection can see test demo in project test folder.
分享到:
相关推荐
jBeanBox项目的定位:需要一个功能较全的IOC/AOP工具,但是又不想引入臃肿的Spring。 其它IOC/AOP工具的问题: Spring: 源码臃肿,Java方式的配置不灵活, 非单例模式时性能差。 Guice: 源码臃肿(200多个类),手工...
Spring中IOC/AOP的说明和例子
jBeanBox项目的定位:需要一个功能较全的IOC/AOP工具,但是又不想引入臃肿的Spring。 其它IOC/AOP工具的问题: Spring: 源码臃肿,Java方式的配置不灵活, 非单例模式时性能差。 Guice: 源码臃肿(200多个类),手工...
基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)...
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
C#实现的IOC和AOP框架,供学习
手写简单实现ioc、aop事务demo,实现事务回滚,使用到了,工厂模式,动态代理
力求获得一种简洁实用的方法实现IOC和AOP相结合的使用方式。 查阅了多个技术资料。经过多次测试,基本达到目的。 IOC使用微软的 Microsoft.Practices.Unity,AOP 使用微软企业库的 Microsoft.Practices.Enterprise...
介绍下spring的ioc和aop的定义
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
unity ioc 和 aop 简单示例,适合初学者,包括XML的配置!
Spring_IOC_AOP总结,有代码详细讲解了springIOC和AOP的功能,是你学习三大框架的必备
SpringIOC和AOP实现机制模拟,来自与网络。
希望能给有需要的人带来帮助,其中演示了配置下的IOC、AOP、属性注入、构造函数注入、通知过滤器、以及不使用配置直接代码硬编的AOP动态代码过程,另外还增加了Castle实现IOC及AOP的示例。 使用VS2015或VS2017打开,...
包括spring的ioc和aop,ioc是用比较流行的set注入方式,aop是用比较流行的xml配置方式。由于是maven项目,所以用普通java项目的话,需要把配置文件放到和程序一个目录下(src下),还要自己导入spring依赖的包。
实现spring框架中两大思想: 1.ioc 2.aop 分析实现原理自己实现ioc和aop。
手写实现IoC和AOP开源架构源码2021.pdf
Java进阶之手写实现IoC和AOP共16页.pdf.zip
Spring IOC AOP MVC 简单例子
springIOC与AOP配置