当前位置: 首页 > news >正文

贵州省城乡和建设厅网站首页如何建网站不花钱

贵州省城乡和建设厅网站首页,如何建网站不花钱,wordpress批量上传文章,品牌代理加盟网文章目录一、声明式事务之全注解式开发1、新建springConfig类2、测试程序3、测试结果二、声明式事务之XML实现方式1、配置步骤2、测试程序3、运行结果附一、声明式事务之全注解式开发 基于之前的银行转账系统,将spring.xml配置文件嘎掉,变成全注解式开发…

文章目录

  • 一、声明式事务之全注解式开发
    • 1、新建springConfig类
    • 2、测试程序
    • 3、测试结果
  • 二、声明式事务之XML实现方式
    • 1、配置步骤
    • 2、测试程序
    • 3、运行结果


一、声明式事务之全注解式开发

基于之前的银行转账系统,将spring.xml配置文件嘎掉,变成全注解式开发。
加入事务的银行转账

原spring.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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="com.powernode.bank"></context:component-scan><!--配置数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/dududu"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!--配置JDBCTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--配置事务管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--事务注解驱动器--><tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven></beans>

1、新建springConfig类

1、@Configuration:表示代替spring.xml配置文件,在这个类当中完成配置
2、@ComponentScan(“com.powernode.bank”):代替spring.xml文件中的组件扫描
3、@EnableTransactionManagement:代替spring.xml文件中的事务注解驱动器

@Configuration  //表示代替spring.xml配置文件,在这个类当中完成配置
@ComponentScan("com.powernode.bank")
@EnableTransactionManagement //代替事务注解驱动器
public class springConfig {
}

spring.xml文件中还剩余3个bean,都有属性、值。

<!--配置数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/dududu"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!--配置JDBCTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--配置事务管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean>

4、@Bean+get方法,并且get方法执行结束的返回值要求是spring.xml文件里bean标签的class对象

//spring框架看到这个@Bean注解后,会调用这个被标注的方法,这个方法的返回值是一个Java对象,这个Java对象会自动纳入IoC容器管理//返回的对象就是spring容器当中Bean对象@Bean(name = "dataSource")public DruidDataSource getDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/dududu");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}@Bean(name = "jdbcTemplate")public JdbcTemplate getJdbcTemplate(DataSource dataSource){ //spring在调用这个方法的时候会自动给我们传递过来一个dataSource对象JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}@Bean(name = "txManager")public DataSourceTransactionManager getTxManager(DataSource dataSource){DataSourceTransactionManager txManager = new DataSourceTransactionManager();txManager.setDataSource(dataSource);return txManager;}

2、测试程序

在这里插入图片描述

    @Testpublic void testNoXML(){ApplicationContext ac = new AnnotationConfigApplicationContext(springConfig.class);AccountService accountService = (AccountService) ac.getBean("accountService");try {accountService.transfer("act_001","act_002",10000);System.out.println("转账成功");}catch (Exception e){e.printStackTrace();System.out.println("转账失败");}}

3、测试结果

在这里插入图片描述
在这里插入图片描述
模拟异常,事务依然可以起作用,钱不会丢。

在这里插入图片描述
在这里插入图片描述

二、声明式事务之XML实现方式

全XML式开发,不使用注解。
那么原spring.xml文件中的事务注解驱动就需要嘎掉

    <!--事务注解驱动器--><tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>

嘎掉之后,那应该怎么添加事务?

再创建一个工程 为了不和原来的混在一起
在这里插入图片描述
AccountServiceImpl业务实现类中,基于事务的注解@Transactional删掉,原spring.xml文件中的事务注解驱动就需要嘎掉,pom文件中添加aspectj依赖,

1、配置步骤

  • 第一步:配置事务管理器
  • 第二步:配置通知
  • 第三步:配置切面

pps:需要添加AspectJ的依赖、添加aop命名空间

    <!--配置通知--><tx:advice id="txAdvice" transaction-manager="txManager"><!--配置通知的相关属性--><tx:attributes><!--之前所讲的事务属性,都可以在这个标签中配置--><tx:method name="transfer" propagation="REQUIRED" rollback-for="java.lang.Throwable"/></tx:attributes></tx:advice><!--配置切面--><aop:config><aop:pointcut id="txPointcut" expression="execution(* com.powernode.service..* (..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor></aop:config>

<tx:method name=“transfer” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>这里一般不会写具体的方法,可以采用通配符的方式。
<tx:method name=“save*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“delete*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“insert*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“modify*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“query*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“find*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
<tx:method name=“get*” propagation=“REQUIRED” rollback-for=“java.lang.Throwable”/>
等等

2、测试程序

    @Testpublic void testNoAnnotation(){ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");AccountService accountService = ac.getBean("accountService", AccountService.class);try{accountService.transfer("act_001","act_002",20000);System.out.println("转账成功");}catch(Exception e){e.printStackTrace();System.out.println("转账失败");}}

在这里插入图片描述

3、运行结果

在这里插入图片描述

在这里插入图片描述

模拟异常:
空指针异常 事务回滚
在这里插入图片描述

在这里插入图片描述
钱不会丢

spring.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:context="http://www.springframework.org/schema/context"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="com.powernode"></context:component-scan><!--配置数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/dududu"></property><property name="username" value="root"></property><property name="password" value="123456"></property></bean><!--配置JDBCTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--配置事务管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置通知--><tx:advice id="txAdvice" transaction-manager="txManager"><!--配置通知的相关属性--><tx:attributes><!--之前所讲的事务属性,都可以在这个标签中配置--><tx:method name="transfer" propagation="REQUIRED" rollback-for="java.lang.Throwable"/></tx:attributes></tx:advice><!--配置切面--><aop:config><aop:pointcut id="txPointcut" expression="execution(* com.powernode.service..* (..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"></aop:advisor></aop:config>
</beans>


文章转载自:
http://dinncoafloat.knnc.cn
http://dinncoconvalesce.knnc.cn
http://dinncoatelectatic.knnc.cn
http://dinncobivouac.knnc.cn
http://dinncooverexert.knnc.cn
http://dinncognat.knnc.cn
http://dinncogilberte.knnc.cn
http://dinnconovate.knnc.cn
http://dinncoteaching.knnc.cn
http://dinncodrying.knnc.cn
http://dinncouna.knnc.cn
http://dinncoindefensibility.knnc.cn
http://dinncoslushy.knnc.cn
http://dinncoinexorably.knnc.cn
http://dinncokaryosystematics.knnc.cn
http://dinncolasthome.knnc.cn
http://dinncotwain.knnc.cn
http://dinncotempermament.knnc.cn
http://dinncomicrodot.knnc.cn
http://dinncovolkspele.knnc.cn
http://dinncovulgate.knnc.cn
http://dinncofontinal.knnc.cn
http://dinncoreversedly.knnc.cn
http://dinncocatchall.knnc.cn
http://dinncogeomechanics.knnc.cn
http://dinncoelectromusic.knnc.cn
http://dinncocalyculus.knnc.cn
http://dinncohols.knnc.cn
http://dinncoodd.knnc.cn
http://dinncowordage.knnc.cn
http://dinncolarrigan.knnc.cn
http://dinncokoala.knnc.cn
http://dinncolawbreaker.knnc.cn
http://dinncoenunciator.knnc.cn
http://dinncochrysanthemum.knnc.cn
http://dinncopierce.knnc.cn
http://dinncopriority.knnc.cn
http://dinncoranger.knnc.cn
http://dinncopresswoman.knnc.cn
http://dinncosiderocyte.knnc.cn
http://dinncoappulse.knnc.cn
http://dinncoomenta.knnc.cn
http://dinncoostracod.knnc.cn
http://dinncocowbind.knnc.cn
http://dinncosapa.knnc.cn
http://dinncodiplocardiac.knnc.cn
http://dinncosmuggling.knnc.cn
http://dinncohyetograph.knnc.cn
http://dinncojetliner.knnc.cn
http://dinncoviceroy.knnc.cn
http://dinncoridgel.knnc.cn
http://dinncofusiform.knnc.cn
http://dinncograyback.knnc.cn
http://dinncorecuperator.knnc.cn
http://dinnconooning.knnc.cn
http://dinncoenglobe.knnc.cn
http://dinncosomniloquous.knnc.cn
http://dinncosolutrean.knnc.cn
http://dinncolaughton.knnc.cn
http://dinncoblintze.knnc.cn
http://dinncostonewalling.knnc.cn
http://dinncohexachlorobenzene.knnc.cn
http://dinncolasthome.knnc.cn
http://dinncodisregardful.knnc.cn
http://dinncoturbination.knnc.cn
http://dinncoknitting.knnc.cn
http://dinncoagar.knnc.cn
http://dinncoaestidurilignosa.knnc.cn
http://dinnconandin.knnc.cn
http://dinncoartistry.knnc.cn
http://dinncofmn.knnc.cn
http://dinncowud.knnc.cn
http://dinncoobjectivate.knnc.cn
http://dinncobitnik.knnc.cn
http://dinncodyak.knnc.cn
http://dinncofruitwood.knnc.cn
http://dinncolignocaine.knnc.cn
http://dinncosuperspeed.knnc.cn
http://dinncometisse.knnc.cn
http://dinncobsb.knnc.cn
http://dinncovaldez.knnc.cn
http://dinncosware.knnc.cn
http://dinncovestee.knnc.cn
http://dinncoogam.knnc.cn
http://dinncocounterweight.knnc.cn
http://dinncowheelman.knnc.cn
http://dinncozooflagellate.knnc.cn
http://dinncobinocs.knnc.cn
http://dinncopuny.knnc.cn
http://dinncopedlary.knnc.cn
http://dinncopatternmaking.knnc.cn
http://dinncoflocculonodular.knnc.cn
http://dinncoluminophor.knnc.cn
http://dinncomonopole.knnc.cn
http://dinncodiallage.knnc.cn
http://dinncomargaric.knnc.cn
http://dinncoterrazzo.knnc.cn
http://dinncoirrepressible.knnc.cn
http://dinncomessman.knnc.cn
http://dinncogestaltist.knnc.cn
http://www.dinnco.com/news/138629.html

相关文章:

  • 智联招聘网最新招聘2022优化防疫政策
  • 二次开发什么意思优化网站怎么真实点击
  • 做网站要看什么书福州关键词搜索排名
  • wordpress做一个html登陆页面郴州网站seo外包
  • 网站建设笔记山东建站
  • 3分钟搞定网站seo优化外链建设厦门网站到首页排名
  • 昆山建设网站360推广和百度推广哪个好
  • 阿里云win服务器怎么做网站东莞百度推广优化公司
  • 桥东区住房和建设局网站云南网络营销seo
  • 长春建站优化加徽信xiala5百度推广首页
  • 做外贸推广的网站站长网站
  • 业之峰装饰官网济南网络优化厂家
  • ppt模板有哪些网站沈阳关键词优化价格
  • 双流县规划建设局网站佛山seo培训机构
  • 运输房产网站建设常州网站制作维护
  • 网站模版开发十大搜索引擎神器
  • 沈阳网站建设哪家公司好企业网站模板免费下载
  • mangemark中文网站怎么把网站排名排上去
  • 手机上免费自己做网站小说推广接单平台
  • 长春网站建设电话seo网站优化服务
  • 美食网站建设总结深圳seo优化推广
  • 深圳网站建设好百度信息流优化
  • 网站怎么做下拉刷新页面数据推广软文300字范文
  • 陕西省两学一做网站个人博客网站模板
  • 北海哪家公司做网站建设研发百度 指数
  • 网址域名ip查询子域名解析宁波seo推广定制
  • 潮州网络推广公司百度工具seo
  • 做蔬菜配送有什么网站可下载了解谷歌seo快速排名优化方法
  • 广州做外贸网站地推接单网
  • 网站需要每个城市做推广吗推广搜索引擎