Replacing Spring Core with jBeanBox to implement declarative transactions

The main problem of Spring is that it is too complicated and there are too many configuration methods. XML configuration does not support runtime modification, and does not support IDE class name refactoring and method prompts. jBeanBox is an IOC/AOP micro-project with a single-file source code of only 650 lines. It is characterized by using JAVA instead of XML, which is simple and easy to understand. The current version is updated to version 2.0, adding AOP Alliance and AspectJ interface support for compatibility with other AOP tools. Declarative transactions are a typical application of AOP. The basic principle is to use thread local variables to manage connections. I wanted to write a simplified version myself, but the level is limited. Secondly, the feature of AOP is that services and kernels are pluggable designs. , the kernel and services can be used separately. Other business support provided in Spring, such as ORM, JTA, JMS, etc., can theoretically be extracted and used on other IOC/AOP tools. If it cannot be extracted, it means that it is tied to the Spring core, which is consistent with its design concept. is inconsistent. Based on the principle of not reinventing the wheel, we now try to extract the declarative transaction service in Spring and integrate it with jBeanBox, which means that this integration only uses Spring's transaction service, not its IOC/AOP. Kernel, weird combination, but a clear purpose: do away with XML configuration.
The following is an example of jBeanBox integrating c3p0 data pool + JDBCTemplate + Spring declarative transaction, and the test passed (only a single file).


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");//your jdbc driver name
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; // throw RuntimeException
dao.execute("insert into users values ​​('User2')");
}

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

You can see that the error transaction in the insertUser method will be rolled back as a whole, and Spring's declaration service will take effect . XML disappears completely, and the configuration can be written in Java and used at any time, which is very convenient.
By the way, since Spring 3.0 comes a JAVA-based configuration, but it needs to be used in conjunction with annotations, which is complicated and difficult to use, and has the following problems: the configuration cannot be inherited, and the configuration cannot be changed during runtime, and the method name is used instead of the class. The name is used as the default ID of the bean. When the target class is refactored, the method name cannot be changed automatically, but must be changed manually, which cancels the meaning of refactoring.
To run this example, please modify the actual database connection and download the 12 jars mentioned below. Or download the packaged "Demo_jBeanBox2.0.war" file from the jBeanBox project website, which already includes this example and all libraries.
Required packages for jBeanBox2.0:
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 Required for
this example:
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
above package To download with Maven, "pom.xml" can be found on the project homepage.
jBeanBox can be downloaded from the github jBeanBox project homepage, just one file:
https://github.com/drinkjava2/jBeanBox/blob/master/jBeanBox2.0/BeanBox.java

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326857297&siteId=291194637