Posts Tagged ‘Struts’
🚀 Making Spring AOP Work with Struts 2: A Powerful Combination! 🚀
Spring AOP (Aspect-Oriented Programming) and Struts 2 might seem like an unusual pairing, but when configured correctly, they can bring cleaner, more modular, and reusable code to your Struts-based applications.
The Challenge:
- Struts 2 manages its own action instances for each request, while Spring’s AOP relies on proxying beans managed by the Spring container. This means Struts actions are not Spring beans by default, making AOP trickier to apply.
- The Solution: Making Struts 2 Actions Spring-Managed
- To make Spring AOP work with Struts 2, follow these steps:
✅ Step 1: Enable Spring integration with Struts 2
Ensure your `struts.xml` is configured to use Spring:
“`<constant name=”struts.objectFactory” value=”spring”/>“`
This makes Struts retrieve action instances from the Spring context instead of creating them directly.
✅ Step 2: Define Actions as Spring Beans
In your applicationContext.xml or equivalent Spring configuration, define your Struts actions:
“`
<bean id=”myAction” class=”com.example.MyStrutsAction” scope=”prototype”/>
“`
Setting the scope to “prototype” ensures a new instance per request, preserving Struts 2 behavior.
✅ Step 3: Apply AOP with `@Aspect`
Now, you can apply Spring AOP to your Struts actions just like any other Spring-managed bean:
“`
@Aspect
@Component
public class LoggingAspect {
@Before(“execution(* com.example.MyStrutsAction.execute(..))”)
public void logBefore(JoinPoint joinPoint) {
System.out.println(“Executing: ” + joinPoint.getSignature().toShortString());
}
}
“`
This will log method executions before any `execute()` method in your actions runs!
Key Benefits of This Approach
🔹 Separation of Concerns – Keep logging, security, and transaction management outside your action classes.
🔹 Reusability – Apply cross-cutting concerns like security or caching without modifying Struts actions.
🔹 Spring’s Full Power – Leverage dependency injection and other Spring features within your Struts 2 actions.
🔥 By integrating Spring AOP with Struts 2, you get the best of both worlds: Struts’ flexible request handling and Spring’s powerful aspect-oriented capabilities. Ready to make your legacy Struts 2 app cleaner and more maintainable? Let’s discuss!
XAException: JDBC driver does not support XA, hence cannot be a participant in two-phase commit
Error:
MyStrutsAction - Error on uploading: MyPersonnalClass@971720[primaryId=<null>,type=XXX,myDate=20080702,entityId=<null>,optimisticTimestamp=<null>] org.springframework.remoting.RemoteAccessException: Could not access remote service [my-service-bean]; nested exception is javax.transaction.TransactionRolledbackException: BEA1-0F416BB49A8BE6353E83: javax.transaction.xa.XAException: JDBC driver does not support XA, hence cannot be a participant in two-phase commit. To force this participation, set the GlobalTransactionsProtocol attribute to LoggingLastResource (recommended) or EmulateTwoPhaseCommit for the Data Source = myDatasource
Fix:
- Go on WebLogic console
- Then follow the pages: Home > Summary of JDBC Data Sources > myDatasource > Transactions
- Uncheck:
One-Phase Commit
- Check:
Emulate Two-Phase Commit