简体   繁体   中英

Spring boot with transactional configuration

I am new to Spring Boot. I was trying to use Spring Boot with hibernate and mysql DB. I was trying to search for how to use spring's transactional configuration using spring boot. In normal spring application where you have xml files you define transaction using aop as below

<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!--the transactional advice (what 'happens'; see the
<aop:advisor/>bean below)-->
<tx:advice id="txAdvice" transaction-manager="txManager">
     <!--the transactional semantics...-->
     <tx:attributes>
          <!--all methods starting with 'get' are read-only-->
          <tx:method name="get*" read-only="true"/>
          <!--other methods use the default transaction settings (see below)-->
          <tx:method name="*"/>
     </tx:attributes>
</tx:advice>
<!--ensure that the above transactional advice runs for any execution
of an operation defined by the FooService interface-->
<aop:config>
     <aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
     <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config>
<!--don't forget the DataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
     <property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
     <property name="username" value="scott"/>
     <property name="password" value="tiger"/>
</bean>
<!--similarly, don't forget the PlatformTransactionManager-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
</bean>

Using above config you can ask spring to attach a read-only transaction to only get* method and default transaction to all other methods.

How do you achieve this(defining transaction aop on methods using wildcard) using Spring Boot?

Tried searching this on google but couldn't find anything. :(

Please guide me to the solution or any preexisting link.

Thanks

From the reference doc, You can do this

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

In you case you can disable the configuration altogether.

Link here.

http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html

As M. Deinum commented that if you can not skip the xml configuration then you can use it using @ImportResource annotation and provide your xml file name. The xml should be available on the classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM