Download Latest Version jbeanbox-core-2.4.jar (1.7 kB)
Email in envelope

Get an email when there's a new version of jBeanBox

Home / jBeanBox2.2
Name Modified Size InfoDownloads / Week
Parent folder
BeanBox.java 2016-07-25 39.1 kB
Demo_jBeanBox2.2.war 2016-07-20 4.5 MB
README.txt 2016-07-20 9.6 kB
jbeanbox-2.2-src.jar 2016-07-20 29.0 kB
jbeanbox-2.2.jar 2016-07-20 21.0 kB
Totals: 5 Items   4.6 MB 0
jBeanBox is a micro scale(only 1 file ~1000 line source code) IOC & AOP tool, it has no XML files(Use "BeanBox" java classes as configurations), 1 single Java file do all the IOC/AOP jobs like Spring framework IOC/AOP core did. 
jBeanBox follows BSD-3 license,  current version is jBeanBox2.2.
(jBeanBox is still in developing, looking for contributors to improve this project, if have interest please post issue on jBeanBox github project page or Email to me Yong9981@gmail.com, tks).

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 based configuration to replace XML, but Java mixed up with annotations, complicated and does not support configuration extending, 
    hard to change configuration at runtime, and if use method name as Bean ID, Bean ID does not support IDE refactor.) 
    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 big and too complicated.
2) Guice and other IOC/AOP tools depend on annotations: Annotations is only a pull type injection, write annotation in Java classes is an invasion, IOC/AOP tool should not change Java file too much.

Advantage of jBeanBox:
1) Simple, only 1 single java file do all the IOC/AOP job, No XML, only 1 Annotation. Easy to learn, use and modify source code.
2) Use Java to replace XML, the implementation is simpler than Spring or Guice's Java configuration.
3) jBeanBox is a full function IOC/AOP tool aim to replace Spring IOC/AOP core in small project, it has below key features:
* Box-oriented Java Configuration to replace XML, (it called "BeanBox"), BeanBox can be created/modified at runtime.
* Bean instance lazy initialization (Similar like Guice)
* SingleTon/Prototype support, default all beans are SingltTon (Similar like Spring)
* SingleTon Cache
* AOP & AspectJ interface support
* Multiple injection mechanisms: 
     Push type: Value injection, Bean injection, Constructor injection, Static factory injection, Bean factory injection
     Pull type: @InjectBox Annotation 
* multiple  contexts support (Besides its default global context, it support multiple contexts, simliar like create many "ApplicationContext"s in Spring)
* Bean life cycle support(postConstruction & preDestory call back) 
 
How to use jBeanBox?
Download jbeanbox-x.x.jar or jbeanbox-x.x-src.jar from jBeanBox project site, and put below 4 jars in lib folder(Other version jars may also work but did not tested):
http://central.maven.org/maven2/aopalliance/aopalliance/1.0/aopalliance-1.0.jar
http://central.maven.org/maven2/asm/asm/3.3.1/asm-3.3.1.jar
http://central.maven.org/maven2/org/aspectj/aspectjrt/1.8.9/aspectjrt-1.8.9.jar
http://central.maven.org/maven2/cglib/cglib/2.2.2/cglib-2.2.2.jar


A basic introduction of how to use jBeanBox:

Example 1 - Basic usages:
public class OrderBox extends BeanBox {//Java Configuration class, play the same role like XML in Spring
	{   //setClassOrValue(Order.class);   //if called by getBean(), no need set class
		setProperty("orderNO", "PO#20160214");// Inject value
		setProperty("company", CompanyBox.class);// Inject a bean
		setProperty("orderItem1", new BeanBox(OrderItem.class, "Dog1", 77.99));// inject constructor parameters
		setStaticFactory("orderItem2", OrderItemFactory.class, "createOrderItem", "Dog2", 88.99);// static factory
		BeanBox factory = new BeanBox(OrderItemFactory.class, new CompanyBox(), false);
		setBeanFactory("orderItem3", factory, "createOrderItem2", new CompanyBox(), "Dog3", 99.99);// bean factory
	}

	public static class CompanyBox1 extends BeanBox {
		{
			setClassOrValue(Company.class);
			setProperty("name", "Company1");
		}
	}

	public static class CompanyBox extends CompanyBox1 {// Extends from another Bean configuration
		{
			setProperty("name", "Company2");//property override
		}
	}
}

public class Tester {
	public static void main(String[] args) {
		Order order = BeanBox.getBean(Order.class);
		order.printALlItems();
		System.out.println("Order bean is a SingleTon? " + (order == new OrderBox().getBean()));//true
	}
}
("Company", "Order", "OrderItem", "OrderItemFactory"  Java classes can see in "Demo_jBeanBox2.1.war" file)


Example 2 - AOP & Aspectj demo:
public class Tester {
	private Iitem item;

	public void setItem(Iitem item) {
		this.item = item;
	}

	public void doPrintItem() {
		item.doPrint();
	}

	public static void main(String[] args) {
		BeanBox advice = new BeanBox(AOPLogAdvice.class).setProperty("name", "AOP Logger");
		BeanBox.defaultContext.setAOPAround("examples.example2_aop.\\w*", "doPrint\\w*", advice, "doAround");
		
		BeanBox advice2 = new BeanBox(AspectjLogAdvice.class).setProperty("name", "AspectJ Logger");
		BeanBox.defaultContext.setAspectjAfterReturning("examples.example2_aop.\\w*", "doPrint\\w*", advice2, "doAfterReturning");

		Tester t = new BeanBox(Tester.class) {
			{ this.setProperty("item", ItemImpl.class);
			}
		}.getBean();
		t.doPrintItem();
	}
}


Example 3 - InjectBox Annotation &  Context:
//1 to 7 are "pull" type injection, 8&9 are "push" type injection, from this example we can see Annotation make source code concise, but hard to understand and maintenance.
public class Tester {
	@InjectBox(A.StrBox.class)
	String s1;// Use StrBox.class

	@InjectBox(A.class)
	String s2;// Use A.StringBox.class (or A.StringBox2.class, 2 to 8 depends context setting)

	@InjectBox(B.class)
	String s3;// Use B$S3Box.class

	@InjectBox
	C c4;// Use CBox.class

	@InjectBox
	String s5;// Use TesterBox$StringBox.class

	@InjectBox(required = false)
	D d6;// Use Config$DBox.class (or Config2$DBox2)

	@InjectBox(required = false)
	E e7;// Use Config$E7Box.class (or Config2$E7Box2)

	private String s8; // injected by field, not suitable for Proxy bean

	private String s9; // injected by setter method

	public void setS9(String s9) {
		this.s9 = s9;
	}

	public void print() {
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		System.out.println((c4 == null) ? null : c4.value);
		System.out.println(s5);
		System.out.println((d6 == null) ? null : d6.value);
		System.out.println((e7 == null) ? null : e7.value);
		System.out.println(s8);
		System.out.println(s9);
		System.out.println(this);
	}

	public static void main(String[] args) {
		Tester t = BeanBox.getBean(Tester.class);
		t.print();

		BeanBoxContext ctx = new BeanBoxContext(Config2.class).setBoxIdentity("Box2");
		Tester t3 = ctx.getBean(Tester.class);
		t3.print();
	}
}

 
Example 4 - PostConstructor and PreDestory 
public class Tester {
	private String name;

	public void init() {
		name = "Sam";
	}

	public void destory() {
		System.out.println("Bye " + name);
	}

	public static class TesterBox extends BeanBox {
		{
			setPostConstructor("init");
			setPreDestory("destory");
		}
	}

	public static void main(String[] args) {
		BeanBox.getBean(Tester.class);
		BeanBox.defaultContext.close();// print Bye Sam
	}
}
 
Example 5 - This example shows how to use jBeanBox to replace Spring's core to achieve "Declarative Transaction".
This example integrated "C3P0" + "JDBCTemplate" + "Spring Declarative Transaction Service"  but did not use Spring's IOC/AOP core, 
a weird combination but target is clear: "To eliminate XML".

public class TesterBox extends BeanBox {
	static {
		BeanBox.defaultContext.setAOPAround("examples.example5_transaction.Test\\w*", "insert\\w*", new TxInterceptorBox(), "invoke");
	}

	static class DSPoolBeanBox extends BeanBox {
		{
			setClassOrValue(ComboPooledDataSource.class);
			setProperty("jdbcUrl", "jdbc:mysql://127.0.0.1:3306/test?user=root&password=yourPWD&useUnicode=true&characterEncoding=UTF-8");
			setProperty("driverClass", "com.mysql.jdbc.Driver");// your jdbc driver name
			setProperty("maxPoolSize", 10);
		}
	}

	static class TxManagerBox extends BeanBox {
		{
			setClassOrValue(DataSourceTransactionManager.class);
			setProperty("dataSource", new DSPoolBeanBox());
		}
	}

	static class TxInterceptorBox extends BeanBox {// Advice
		{
			Properties props = new Properties();
			props.put("insert*", "PROPAGATION_REQUIRED");
			setConstructor(TransactionInterceptor.class, new TxManagerBox(), props);
		}
	}

	public static class JdbcTemplateBox extends BeanBox {
		{
			setConstructor(JdbcTemplate.class, new DSPoolBeanBox());
		}
	}
}

public class Tester {
	@InjectBox
	private JdbcTemplate dao;

	public void insertUser() {
		dao.execute("insert into users values ('User1')");
		int i = 1 / 0; //Throw a runtime Exception to roll back transaction
		dao.execute("insert into users values ('User2')");
	}

	public static void main(String[] args) {
		Tester tester = BeanBox.getBean(Tester.class);
		tester.insertUser();
	}
}


To run example5, below extra jars are needed, you can download these jars in Maven repository:
c3p0-0.9.1.2.jar
commons-logging-api-1.0.4.jar
mysql-connector-java-5.1.5.jar
spring-aop-3.2.16.RELEASE.jar
spring-beans-3.2.16.RELEASE.jar
spring-core-3.2.16.RELEASE.jar
spring-jdbc-3.2.16.RELEASE.jar
spring-tx-3.2.16.RELEASE.jar

There is a war package "Demo_jBeanBox2.1.war" include all above examples and jars can be found on jBeanBox project site. 
Source: README.txt, updated 2016-07-20