JBoss EAPSpring Framework交易企業系統升級

JBoss EAP 09: understand Spring XML and verify Java configuration behavior

··6 min read
Back to the JBoss EAP learning path

Replacing a long applicationContext.xml with @Bean methods can make the configuration shorter without preserving transaction behavior. Identify object ownership and proxy crossings first. This lesson runs one service through XML configuration, Java configuration, and Java importing XML, all using the same behavioral test body.

Download lesson 09: XML/Java configuration lab

The lab fixes Java 21, Spring 6.2.19, and H2. Boot 3.5.16 supplies build/dependency management only; there is no SpringApplication or web server. All configurations run on the same new framework, not legacy Spring 3/4, so this establishes configuration behavior rather than enterprise migration compatibility.

Three rows at the top. XML mode: tx:advice plus aop:advisor in context.xml applies to create*. Java mode: EnableTransactionManagement in JavaConfig (proxyBeanMethods=false) enables @Transactional. Java importing XML: ImportedXmlConfig loads the same context.xml, so the rules come from XML. All three point to one TicketService and one shared test body. Two call paths below: TicketEntry calls createThenFail through the injected proxy, a transaction is observed and rolls back, 0 rows remain; selfInvokeFailure calls this.createThenFail without a new proxy crossing, no transaction is observed, 1 row remains
Where each loading mode gets its transaction rules, and the result of two call paths. Class names and row counts come from this lesson's download and local tests. Diagram drawn by KeepOnFirst.

Trace objects through bean, ref, and @Bean

xml
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
  <constructor-arg ref="dataSource"/>
</bean>
<bean id="ticketService" class="com.keeponfirst.xml.TicketService">
  <constructor-arg ref="jdbc"/>
</bean>

In this excerpt, id names a bean, ref references another bean, and constructor-arg supplies the constructor dependency. Follow ticketService → jdbc → dataSource to locate the resource. Here XML creates an H2 data source; it is not an EAP JNDI lookup.

java
@Bean
public TicketService ticketService(JdbcTemplate jdbc) {
    return new TicketService(jdbc);
}

The Java form receives the managed JdbcTemplate as a method parameter and constructs the same service. JavaConfig uses proxyBeanMethods=false, so it relies on parameter injection rather than direct calls between @Bean methods. Distinguish container-managed dependencies from manually constructed objects that may lack proxying and lifecycle processing.

Official example: with proxyBeanMethods=false, do not call @Bean methods directly

The Spring 6.2 @Configuration javadoc says proxyBeanMethods defaults to true. In that mode, @Bean methods are proxied. A direct call to a @Bean method in user code still returns the shared singleton.

With false, @Bean methods are processed individually, which the javadoc calls “@Bean Lite Mode”. It says this is behaviorally equivalent to removing the @Configuration stereotype. The basic-concepts page adds that a direct call is not intercepted by the container. Each such call creates a new instance. Lite-mode @Bean methods should therefore not declare inter-bean dependencies. They receive collaborators as method arguments instead.

java
// JavaConfig.java in the lab (excerpt)
@Bean public DataSource dataSource() {
    var ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:xml-java-" + UUID.randomUUID() + ";DB_CLOSE_DELAY=-1");
    return ds;
}
@Bean public JdbcTemplate jdbc(DataSource ds) { return new JdbcTemplate(ds); }
@Bean public DataSourceTransactionManager transactionManager(DataSource ds) {
    return new DataSourceTransactionManager(ds);
}

// Variant NOT in the lab and NOT executed:
// @Bean public JdbcTemplate jdbc() { return new JdbcTemplate(dataSource()); }

The lab's JavaConfig follows that advice. Both jdbc and transactionManager take the parameter ds, so they share one DataSource. Each run of dataSource() builds an H2 URL from a new random UUID.

This is an inference from the official text above. If jdbc called dataSource() directly, the JdbcTemplate would connect to a second in-memory database. The transaction manager governs the container's DataSource, so its rollback would not reach the JdbcTemplate writes. This variant was not executed and is not in the download.

Component scanning can coexist with XML

Both configurations scan only com.keeponfirst.xml.entry for the @Component TicketEntry and declare other beans explicitly. Expanding scanning to the whole root package can load another configuration or service definition. Broader scanning is not a substitute for understanding a missing dependency.

java
@Configuration(proxyBeanMethods = false)
@ImportResource("classpath:context.xml")
public class ImportedXmlConfig { }

The third mode imports the same XML without also loading JavaConfig or a second annotation-driven transaction configuration. This supports retaining understood XML during gradual migration. ImportResource is a loading mechanism, not a repair for removed classes, custom namespace handlers, or incompatible dependencies.

Align transaction declarations with the same methods

xml
<tx:advice id="ticketAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="createThenCheckedRollback" rollback-for="java.lang.Exception"/>
    <tx:method name="create*"/>
  </tx:attributes>
</tx:advice>

The XML advisor separately applies this advice to public create* methods; declaring advice does not attach it everywhere. Java configuration enables the annotations through EnableTransactionManagement. XML mode has no annotation-driven transaction setup, so its rules come from XML despite the shared service carrying annotations. This avoids stacking two transaction configurations.

This lab has no global rollback override: runtime exceptions roll back, while checked exceptions do not by default. Its explicit checked-exception method therefore declares rollback rules in both forms. A real migration also needs to align propagation, isolation, timeout, readOnly, manager selection, and exception policies.

Official example: unset <tx:method/> attributes use defaults

In context.xml, <tx:method name="create*"/> sets only the name. The Spring 6.2 <tx:advice/> settings page lists the defaults for unset attributes; the table below collects them. The name attribute accepts the * wildcard, with get*, handle*, and on*Event as the official examples.

AttributeOfficial defaultThis lab's create*
propagationREQUIREDUnset; default applies
isolationDEFAULTUnset; default applies
read-onlyfalse (read-write)Unset; default applies
timeout-1 (underlying system default)Unset; default applies
rollback-forNone; RuntimeException rolls back, checked Exception does notOnly createThenCheckedRollback sets java.lang.Exception
Defaults from the Spring 6.2 <tx:advice/> settings page; the last column comes from context.xml in the download.

The docs note that isolation, timeout, and read-only apply only when propagation is REQUIRED or REQUIRES_NEW. Here create* uses the default REQUIRED, so all three rows apply. The official XML example has the same shape. tx:advice defines the rules, and the pointcut and advisor in aop:config choose the methods. This lab has no separate aop:pointcut. It puts the pointcut directly in the pointcut attribute of aop:advisor. This lab's pointcut is execution(public * com.keeponfirst.xml.TicketService.create*(..)). When converting to @Transactional, check this table row by row. A change in any cell can change transaction behavior.

Predict the row counts, then run the tests

bash
mvn test
mvn package

Each case starts with an empty table, and the test itself owns no transaction. Three configurations execute the same test body, plus one manual-construction control: four JUnit invocations. The table describes six observations within each configuration, not 18 separate JUnit tests.

CallRows remainingTransaction observed
Normal create1Yes
Runtime exception0Yes
Checked exception, default rule1Yes
Explicit checked rollback0Yes
Self-invocation of failing method1No
Entry calls proxied service0Yes
Observed locally after return or exception.

Why self-invocation is a different route

text
Caller → Spring proxy → createThenFail → rollback

Caller → selfInvokeFailure → this.createThenFail
                           (no new proxy crossing)

TicketEntry → injected Service proxy → createThenFail → rollback

selfInvokeFailure has no surrounding transaction and calls its own method without re-entering the proxy. The local test observed no active transaction and one row. TicketEntry crosses the injected proxy and rolls back. An already transactional outer method or AspectJ weaving changes the conditions; self-invocation does not universally mean no transaction exists.

Preserve the transaction contract before choosing its notation. Changing checked-exception rollback while converting to annotations is a behavioral change. For this migration, the useful outcome is an explainable object/proxy path and a validating case for each relevant XML section.

Next: MDB, DB, and redelivery boundaries

Sources