1. 程式人生 > >Spring boot中Session Factory的使用

Spring boot中Session Factory的使用

@Aspect
@Configuration
public class TxAdviceInterceptor {
	
	// 超時時間 單位秒
	private static final int TX_METHOD_TIMEOUT = 5;
    private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.hwj..service.*.*(..))";
 
    @Autowired
    private PlatformTransactionManager transactionManager;
 
    @Bean
    public TransactionInterceptor txAdvice() {
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
         
        // 只讀事務,不做更新操作
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED );
        // 當前存在事務就使用當前事務,當前不存在事務就建立一個新的事務
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
        requiredTx.setRollbackRules(
            Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        requiredTx.setTimeout(TX_METHOD_TIMEOUT);
        Map<String, TransactionAttribute> txMap = new HashMap<>();
        txMap.put("add*", requiredTx);
        txMap.put("save*", requiredTx);
        txMap.put("insert*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("delete*", requiredTx);
        txMap.put("get*", readOnlyTx);
        txMap.put("query*", readOnlyTx);
        source.setNameMap( txMap );
        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
        return txAdvice;
    }
 
    @Bean
    public Advisor txAdviceAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice());
    }
    
    @Bean    //一定要注入這個(劃重點)
    public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){    
        return hemf.getSessionFactory();    
    }     

}

相關推薦

Spring bootSession Factory的使用

@Aspect @Configuration public class TxAdviceInterceptor { // 超時時間 單位秒 private static final int TX_METHOD_TIMEOUT = 5; private static final String A

spring boot 使用redis session

錯誤 dap isa name art res 5.0 fir sync   spring boot 默認的httpsession是存在內存中。這種默認方式有幾個缺點:1、當分布式部署時,存在session不一致的問題;2、當服務重啟時session就會丟失,這時候用戶就需

Spring Boot使用Spring Session解決分散式會話共享問題

如果你正在使用Java開發Web應用,想必你對HttpSession非常熟悉,但我們知道HpptSession預設使用記憶體來管理Session,如果將應用橫向擴充套件將會出現Session共享問題。 Spring Session提供了一套建立和管理Servlet Ht

spring boot 訪問 REST 接口

pri clas rest pla oot bject res .get obj RestTemplate restTemplate = new RestTemplate(); Object result = restTemplate.getForObject("http

3.Spring Boot使用Swagger2構建強大的RESTful API文檔

pack 效果 type 現象 業務邏輯 blank depend imp any 原文:http://www.jianshu.com/p/8033ef83a8ed 由於Spring Boot能夠快速開發、便捷部署等特性,相信有很大一部分Spring Boot的用戶會用來構

spring-boot實戰【07】【轉】:Spring BootWeb應用的統一異常處理

http integer private fin ima lex clas 友好 ref 我們在做Web應用的時候,請求處理過程中發生錯誤是非常常見的情況。Spring Boot提供了一個默認的映射:/error,當處理中拋出異常之後,會轉到該請求中處理,並且該請求有一個全

spring-boot實戰【06】【轉】:Spring Boot使用Swagger2

des values 產生 service sof div 解決 整合 data 由於Spring Boot能夠快速開發、便捷部署等特性,相信有很大一部分Spring Boot的用戶會用來構建RESTful API。而我們構建RESTful API的目的通常都是由於多終端的

spring-boot實戰【12】:Spring Boot使用JavaMailSender發送郵件

nts fun rop oci 單元測試 prop 快速入門 cat tid 相信使用過Spring的眾多開發者都知道Spring提供了非常好用的JavaMailSender接口實現郵件發送。在Spring Boot的Starter模塊中也為此提供了自動化配置。下面通過實例

Spring Boot 配置定時任務,實現多線程操作

pre log pri http code china 部分 多線程操作 .net 參考的代碼部分 https://git.oschina.net/jokerForTao/spring_boot_schedule 一目了然!Spring Boot 中配置定時任務,實現

spring boot實現響應圖片的方法以及改進

spring-bootController響應,噴出圖片,是一個很常見的功能,代碼如下@RequestMapping(value = { "/img/{filename:.+}" }, method = RequestMethod.GET, produces = { MediaType.I

關於Spring boot讀取屬性配置文件出現中文亂碼的問題

led Coding uri oot serve http 添加 message 程序 1.再配置文件(application.properties)中添加編碼字符集 #返回頁面、數據中文亂碼問題spring.http.encoding.force=truespring.h

Spring Boot的initializers的作用分析

ack array init pes rtl ble set bsp warn 在SpringApplication的實例屬性中有一個初始器的屬性:List<ApplicationContextInitializer<?>> initializers

Thymeleaf 模板 在spring boot 的引用和應用

end text www. bean template har ica ngs sta Thymeleaf是一個java類庫,他是一個xml/xhtml/html5的模板引擎和Struts框架的freemarker模板類似,可以作為mvc的web應用的view層。 Thy

spring-boot application.properties的各種配置

ecif creat ati .sh drive fig nag ide cte ###########################################################datasource connect mysql#############

Spring Boot實現logback多環境日誌配置

cati feature gprof 配置 color app config 現在 ng- 在Spring Boot中,可以在logback.xml中的springProfile標簽中定義多個環境logback.xml: <springProfile name=

Spring bootRedis的使用

分布 random code sleep fault Language 文件 keyword attr spring boot對常用的數據庫支持外,對nosql 數據庫也進行了封裝自動化。 redis介紹 Redis是目前業界使用最廣泛的內存數據存儲。相比memcached

spring boot 通過session 取數據

get ets attribute ini () fig spring boot word //先把數據存入session 中  LoginInfo loginInfo = userService.login(userName, password); reque

Spring Boot的自定義start pom

sin string cond aps 標註 bind rip ges 由於 start pom是springboot中提供的簡化企業級開發絕大多數場景的一個工具,利用好strat pom就可以消除相關技術的配置得到自動配置好的Bean。 舉個例子,在一般使用中,我們使用基

spring boot Spring data jpa數據庫表字段命名策略

_id -s ber data 駝峰命名 org body strategy 命名 spring boot 中Spring data jpa命名策略 數據庫,表字段命名是駝峰命名法(UserID),Spring data jpa 自動更新之後是 user_id, 表字段不對

springboot(三):Spring bootRedis的使用

red implement getc factory pro acc 系統 val fault spring boot對常用的數據庫支持外,對nosql 數據庫也進行了封裝自動化。 redis介紹 Redis是目前業界使用最廣泛的內存數據存儲。相比memcached,Red