jBeanBox is a micro scale(1 file) IOC & AOP tool, it has no XML files(Use "BeanBox" java classes as configurations), no annotations. 1 single Java file ~650 lines source code do all the IOC/AOP job like Spring framework IOC/AOP core did.
jBeanBox follows BSD-3 license. Current version is jBeanBox2.0.
Author: Yong Zhu
Since: 2016-2-13
Email: Yong9981@gmail.com
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: write annotation in Java classes is an invasion, IOC/AOP tool should better not change Java files.
Advantage of jBeanBox:
1) Simple, only 1 java file do all the IOC/AOP job. No XML files, no annotations, use pure Java classes as configurations, these Java classes are extended from "BeanBox" class (like "Box" class in another project "jWebBox" 1 single Java file do all jobs Apache-Tiles did; seems "Box-Oriented Programming" can often make design simple).
2) Java configurations can be organized in packages; IDE support class name spelling check and refactor.
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 mobile application or small project.
How to use jBeanBox?
jBeanBox is released as Java source code(only 1 file), to use it need copy "BeanBox.java" into your project src folder, and put below 4 jars in lib folder:
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
Key Features of jBeanBox2.0:
SingleTon/Prototype support
SingleTon Cache
Value injection, Bean injection, Constructor injection, Static factory injection, Bean factory injection
Java regular expression point-cut
AOP & AspectJ Advice support
A basic introduction of using jBeanBox IOC/AOP tool:
class CompanyBox1 extends BeanBox {//CompanyBox1 is Configuration ID
{
setClassOrValue(Company.class); //set Bean class, use new CompanyBox1().getBean() will create a Company instance
setProperty("name", "Littel Dog"); //inject property with a value
}
}
class CompanyBox extends CompanyBox1 {//Configuration Extends from another configuration
{
setProperty("name", "Big Dog"); //Property override
}
}
public class OrderBox extends BeanBox {
{
setClassOrValue(Order.class);
setProperty("orderNO", "PO#20160214");// Value Injection
setProperty("company", new CompanyBox());// Bean Injection
setProperty("orderItem1", new BeanBox(OrderItem.class, new Object[] { "Dog1", 77.99 }));// Constructor Injection
setStaticFactory("orderItem2", OrderItemFactory.class, "createOrderItem", new Object[] { "Dog2", 88.99 });// Static Factory Injection
BeanBox factory = new BeanBox(OrderItemFactory.class, new Object[] { new CompanyBox(), false });
setBeanFactory("orderItem3", factory, "createOrderItem2", new Object[] { new CompanyBox(), "Dog3", 99.99 });// Bean factory Injection
}
static {// AOP setting
BeanBox.setAOPAround("examples.example1_allusuages.\\w*", "print\\w*", new BeanBox(AOPLogAdvice.class).setProperty("name", "Demo"), "doAround");
}
}
public class Tester {
public static void main(String[] args) {
Order order = new OrderBox().getBean(); //here get the bean from container
order.printALlItems();
}
}
Below is an 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".
package examples.example3_transaction;
import java.util.Properties;
import net.sf.jbeanbox.BeanBox;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import com.mchange.v2.c3p0.ComboPooledDataSource;
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");
setProperty("maxPoolSize", 10);
}
}
class JdbcTmpBox extends BeanBox {
{
setConstructor(JdbcTemplate.class, new Object[] { new DSPoolBeanBox() });
}
}
class TxManagerBox extends BeanBox {
{
setClassOrValue(DataSourceTransactionManager.class);
setProperty("dataSource", new DSPoolBeanBox());
}
}
class TxInterceptorBox extends BeanBox {// Advice
{
Properties props = new Properties();
props.put("insert*", "PROPAGATION_REQUIRED");
setConstructor(TransactionInterceptor.class, new Object[] { new TxManagerBox(), props });
}
}
public class SpringTxTest {
static {// AOP setting
BeanBox.setAOPAround("examples.example3_transaction.SpringTx\\w*", "insert\\w*", new TxInterceptorBox(), "invoke");
}
public void insertUser() {
JdbcTemplate dao = new JdbcTmpBox().getBean();
dao.execute("insert into users values ('User1')");
int i = 1 / 0; //To cause a roll back of insertUser method
dao.execute("insert into users values ('User2')");
}
public static void main(String[] args) {
SpringTxTest tester = new BeanBox(SpringTxTest.class).getBean();
tester.insertUser();
}
}
To run above example, you need copy "BeanBox.java" file into your project src folder, and put below extra jars in lib folder:
http://central.maven.org/maven2/c3p0/c3p0/0.9.1.2/c3p0-0.9.1.2.jar
http://central.maven.org/maven2/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.5/mysql-connector-java-5.1.5.jar
http://central.maven.org/maven2/org/springframework/spring-aop/3.2.16.RELEASE/spring-aop-3.2.16.RELEASE.jar
http://central.maven.org/maven2/org/springframework/spring-beans/3.2.16.RELEASE/spring-beans-3.2.16.RELEASE.jar
http://central.maven.org/maven2/org/springframework/spring-core/3.2.16.RELEASE/spring-core-3.2.16.RELEASE.jar
http://central.maven.org/maven2/org/springframework/spring-jdbc/3.2.16.RELEASE/spring-jdbc-3.2.16.RELEASE.jar
http://central.maven.org/maven2/org/springframework/spring-tx/3.2.16.RELEASE/spring-tx-3.2.16.RELEASE.jar
A "pom.xml" file can be found on jBeanBox project's web site for all above jars.
There is a file "Demo_jBeanBox2.0.war" include all above examples and jars can be found on jBeanBox project site.