In my project all the manager classes implemented like this pattern,
<bean abstract="true"> <property name="transactionManager" ref="transactionManager" /> <property name="proxyTargetClass"><value>true</value></property> <property name="transactionAttributes"> <props> <prop key="create*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean parent="companyManagerTxProxy" scope="prototype"> <property name="target"> <bean> </bean> </property> </bean> can you please explain me why we are using org.springframework.transaction.interceptor.TransactionProxyFactoryBean in creating all the manager's beans. what is the exactly use of it?
2 Answers
This is a broad question because there are conceptual explanation to be done here. In very short: TransactionProxyFactoryBean is part of Spring's Transaction support and allows applications to have a "transactional" behavior in server-independent way. In other words: You may replace EJB Container Managed beans if you configure your services using TransactionProxyFactoryBean.
Please refer to Spring Documentation
You may need to understand the concept of Transaction overall. But a very short explanation is:
Transactions (read or write of information) must be ACID and ensure that we need to give "transactional" behavior the software resources (code) which interact with the database (there may be other transactions).
The above explanation touches a bit on AOP (Aspect Oriented Programming) where in, the behavior you want to give with the transactions is "aspect".
There are usually three things that need to be configured:
transactionManager, target and transactionAttribute.
The transactionAttribute is where you give transactional behavior for the read and writes for your resources. In your example, your companyAdminManager is given trnasactional behavior. The companyAdminManager in turn must be configured with datasource which would have db url, user/pass and other pertinent information.
Here are two good explanations (explains each and every line)
1.) Click this to to read each line of configuration explained
2.) I find the following blog post by Scot to be basic and easy to understand example with explanation. Please read click here
Spring declarative transactions can be IMPERATIVE(PlatformTransactionManager.class) or REACTIVE (ReactiveTransactionManager.class). Under the hood both classes only differ in the TrasactionInterceptor, which is the AOP/AspectJ class that provides advice to transaction methods. You need to understand AOP/AspectJ to understand previous statement.
If you want to programmatically extend Spring transactions instead of implement a whole new TransactionManager you just need to supply the TransactionInterception.
For example:
@Bean public TransactionInterceptor txAdvice() { new TransactionInterceptor() { @Override @Nullable public Object invoke(MethodInvocation invocation) { //......have some fun } }; }