当前位置: 首页 > 工具软件 > jBeanBox > 使用案例 >

单文件实现IOC/AOP功能,jBeanBox项目第一版发布

汪深
2023-12-01
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.
 类似资料: