Announcement Announcement Module

JavaConfig forum decommissioned in favor of Core Container

As described at

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at
See more
See less
How to use TransactionProxyFactoryBean in JavaConfig? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use TransactionProxyFactoryBean in JavaConfig?


    I'm new to JavaConfig and have one question. In our development we use spring's transaction management because it's so powerful and convenient, thanks to the AOP! ^_^ Usually we'll define one base bean like this:

    <bean id="baseProxy" lazy-init="true" abstract="true" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionAttributes">
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>


    then we can define our transactional service as :

    <bean id="fooService" parent="baseProxy">
    <property name="target">
    <bean class="example.FooServiceImpo">
    <property name="fooDao"><ref bean="fooDao"/></property>

    now our "fooService" can use the above transaction attribute defined in its parent bean, but how to implement this configuration when we are using JavaConfig? I didn't find any hint in JavaConfig's reference document.

    Thanks again!

  • #2
    Here is a comprehensive working example using JavaConfig (uses standalone HSQL server with a simple foo table). This java configuration can be further simplified if you use annotation driven transactions (not included in this sample).

    import org.springframework.transaction.interceptor.TransactionProxyFactoryBean;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
    import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
    import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.dao.EmptyResultDataAccessException;
    import org.springframework.orm.ObjectRetrievalFailureException;
    import org.junit.Test;
    import javax.sql.DataSource;
    import java.util.Properties;
    import java.sql.SQLException;
    import java.sql.ResultSet;
    import static junit.framework.Assert.assertEquals;
    public class TestTransactionConfig {
        public void testFooService() throws Exception {
            JavaConfigApplicationContext context = new JavaConfigApplicationContext(TransactionConfig.class);
            DefaultFooService fooService = context.getBean(DefaultFooService.class);
            Foo myFoo = new Foo();
            assertEquals("myFoo", fooService.getFoo(0).getName());
        static class TransactionConfig {
            public TransactionProxyFactoryBean baseProxy() {
                TransactionProxyFactoryBean transactionProxyFactoryBean = new TransactionProxyFactoryBean();
                Properties transactionAttributes = new Properties();
                transactionAttributes.setProperty("save", "PROPAGATION_REQUIRED");
                transactionAttributes.setProperty("update", "PROPAGATION_REQUIRED");
                transactionAttributes.setProperty("delete", "PROPAGATION_REQUIRED");
                return transactionProxyFactoryBean;
            public DefaultFooService fooService() {
                DefaultFooService fooService = new DefaultFooService();
                return fooService;
            public DefaultFooDao fooDao() {
                DefaultFooDao fooDao = new DefaultFooDao();
                return fooDao;
            public PlatformTransactionManager transactionManager() {
                DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
                return transactionManager;
            public DataSource dataSource() {
                DriverManagerDataSource dataSource = new DriverManagerDataSource();
                return dataSource;
        static class Foo {
            private Integer id;
            private String name;
            public Integer getId() {
                return id;
            public void setId(Integer id) {
       = id;
            public String getName() {
                return name;
            public void setName(String name) {
       = name;
        public interface FooService {
            Foo getFoo(int id);
            void insertFoo(Foo foo);
            void updateFoo(Foo foo);
            void deleteFoo(int id);
        public interface FooDao {
            void create(Foo foo);
            Foo read(int id);
            void update(Foo foo);
            void delete(int id);
        static class DefaultFooDao implements FooDao {
            public DefaultFooDao() {
            private SimpleJdbcTemplate simpleJdbcTemplate;
            private SimpleJdbcInsert insertFoo;
            private DataSource dataSource;
            public void setDataSource(DataSource dataSource) {
                this.dataSource = dataSource;
                this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
                this.insertFoo = new SimpleJdbcInsert(dataSource)
            public void create(Foo foo) {
                Number newKey = this.insertFoo.executeAndReturnKey(
            public Foo read(int id) {
                Foo foo;
                try {
                    foo = this.simpleJdbcTemplate.queryForObject(
                            "SELECT id, name FROM foo WHERE id=?",
                            new JdbcPetRowMapper(),
                catch (EmptyResultDataAccessException ex) {
                    throw new ObjectRetrievalFailureException(Foo.class, id);
                return foo;
            public void update(Foo foo) {
                throw new UnsupportedOperationException();
            public void delete(int id) {
                throw new UnsupportedOperationException();
            private MapSqlParameterSource createFooParameterSource(Foo foo) {
                return new MapSqlParameterSource()
                        .addValue("id", foo.getId())
                        .addValue("name", foo.getName());
            private class JdbcPetRowMapper implements ParameterizedRowMapper<Foo> {
                public Foo mapRow(ResultSet rs, int rownum) throws SQLException {
                    Foo foo = new Foo();
                    return foo;
        static class DefaultFooService implements FooService {
            public DefaultFooService() {
            private FooDao fooDao;
            public void setFooDao(FooDao fooDao) {
                this.fooDao = fooDao;
            public Foo getFoo(int id) {
            public void insertFoo(Foo foo) {
            public void updateFoo(Foo foo) {
            public void deleteFoo(int id) {

    Hope this helps.

    Last edited by aruld; Oct 11th, 2008, 08:32 PM. Reason: fixed a typo


    • #3
      Thanks very much, I got it.
      But I think this approach confused me a little.When I use an XML configuration, I can understand it clearly that the fooService was returned from the ProxyFactoryBean and also I know it was aspected with one interceptor.

      Further I think if the code was refactored like this it would be easier to understand (based on XML)

          public TransactionProxyFactoryBean baseProxy() { }
          public FooService fooService() {
                  DefaultFooService fooService = new DefaultFooService();
                  return fooService;
      then if I need another barService, it can be
          public BarService barService() {
                  DefaultBarService barService = new DefaultBarService ();
                  return barService;
      but it failed! Do I need config another TransactionProxyFactoryBean again? I found that Spring will actually create one bean named "baseProxy", but I want it to be abstract( like "abstract=true" in the XML ), then it can be reused by barService .....

      another problem , I printed the fooService returned by the SpringContext and found it's not been proxied(I removed DAO and datasource, just return one empty fooService ), then I commented the configuration for baseProxy, it printed the same result, seems that the configuration for baseProxy is useless.I know if I do configuration in XML, the fooService will be a reference to one object returned by ProxyFactoryBean.getObject() (though it's done when spring initialize the IOC context), but I can't see that from the above code.

      Anyway I got some idea, thanks again.
      Last edited by javeer; Oct 12th, 2008, 11:14 AM.


      • #4
        I am not sure how the bean inheritance is supported in JavaConfig. May be someone in this forum can chime in. I prefer to use Spring 2+ Transaction configuration as they are less verbose and recommended style and easier to configure using JavaConfig.