首页 > 数据库 >spring context.xmL配置数据库事务以及aop

spring context.xmL配置数据库事务以及aop

时间:2023-03-20 20:02:08浏览次数:55  
标签:xmL www http spring springframework context aop org schema


[color=red][b]xml配置方式[/b][/color]

!-- from the file context.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 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>

<!-- other <bean/> definitions here -->
</beans>




[color=red][b]注解方式[/b][/color]


// the service class that we want to make transactional
@Transactional
public class DefaultFfooService implements FooService {
Foo getFoo(String fooName);
Foo getFoo(String fooName, String barName);
void insertFoo(Foo foo);
void updateFoo(Foo foo);
}


以下是配置方式, 要么配置service, 要么配置扫描service bean注解也行


<!-- from the file context.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- this is the service object that we want to make transactional -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- a PlatformTransactionManager is still required -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- (this dependency is defined somewhere else) -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- other <bean/> definitions here -->
</beans>

标签:xmL,www,http,spring,springframework,context,aop,org,schema
From: https://blog.51cto.com/u_3871599/6138554

相关文章

  • Spring整合velocity
    版本要1.4,1.6版本出现问题:FailedtoloadDirective:org.apache.velocity.tools.generic.directive.Ifnull,不知道怎么解决.[url]http://yixia......
  • springmvc中获得HttpServletRequest request方法
    在web。xml中配置一个监听<listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>......
  • Spring容器管理的配置Bean转换对象为json字符串时StackOverflowError问题
    背景项目中某配置类XxxConfig定义了很多配置参数,通过Spring的@Value注解与配置中心的项目yml里的配置项关联。@Slf4j@Getter@Setter@RefreshScope@Configurationpub......
  • 用xml来定义imagebutton的状态
    res/drawable/[color=red]drawable_x[/color].xml<?xmlversion="1.0"encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/ap......
  • Android xml文件里读取string array
    example:XMLfilesavedatres/values/strings.xml:<?xmlversion="1.0"encoding="utf-8"?><resources><string-arrayname="plan......
  • Spring MVC3: Controller接受Form数据
    SpringMVC3:Controller接受Form数据<formaction="./saveIntoDatabase.do"method="post"name="saveIntoDatabase"><inputname="name"t......
  • spring-servlet.xml
    WEB-INF目录下面建一个ascweb-servlet.xml文件,其实这个文件的命名就是Web.xml中servlet-name的名字加-servlet.xml.其文件内容如下:<?xmlversion="......
  • Spring容器加载完之后执行特定任务
    有两个服务器类,需要SpringContext在InitAfterSpringContextService之前初始化:1、SpringContextspring容器的上线文环境packagecom.cdelabcare.pubservice;importorg.sp......
  • Spring MVC防重复提交
    如何在SpringMVC里面解决此问题(其它框架也一样,逻辑一样,思想一样,和具体框架没什么关系)。要解决重复提交,有很多办法,比如说在提交完成后redirect一下,也可以用本文提到的使用to......
  • Springboot项目密码加密器jasypt
    最新版依赖<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.5</version></dependenc......