1 、spring boot

@EnableJpaRepositories(
repositoryBaseClass = BaseRepositoryImpl.class,
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = BaseRepository.class),
excludeFilters = @ComponentScan.Filter(NoRepositoryBean.class),
repositoryFactoryBeanClass = BaseRepositoryFactoryBean.class
)

从EnableJpaRepositories的源代码里面可以看到相关的默认值

 1 @EnableJpaRepositories(
2 basePackages = {},
3 basePackageClasses = {},
4 includeFilters = {},
5 excludeFilters = {},
6 repositoryImplementationPostfix = "Impl",
7 namedQueriesLocation = "",//META-INF/jpa-named-queries.properties
8 queryLookupStrategy=QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND, //QueryLookupStrategy.Key.x
9 repositoryFactoryBeanClass=JpaRepositoryFactoryBean.class, //class
10 entityManagerFactoryRef="entityManagerFactory",
11 transactionManagerRef="transactionManager",
12 considerNestedRepositories=false,
13 enableDefaultTransactions=true
14 )

2、 FilterType 说明

public enum FilterType {

    /**
* Filter candidates marked with a given annotation.
* @see org.springframework.core.type.filter.AnnotationTypeFilter
*/
ANNOTATION, /**
* Filter candidates assignable to a given type.
* @see org.springframework.core.type.filter.AssignableTypeFilter
*/
ASSIGNABLE_TYPE, /**
* Filter candidates matching a given AspectJ type pattern expression.
* @see org.springframework.core.type.filter.AspectJTypeFilter
*/
ASPECTJ, /**
* Filter candidates matching a given regex pattern.
* @see org.springframework.core.type.filter.RegexPatternTypeFilter
*/
REGEX, /** Filter candidates using a given custom
* {@link org.springframework.core.type.filter.TypeFilter} implementation.
*/
CUSTOM }

3、  JpaRepositoryFactoryBean

FactoryBean 是一个可以在 IOC而容器中被管理的一个 bean,  是对各种处理过程和资源使用的抽象,  FactoryBean 在需要

时产生另一个对象,而不返回 FactoryBean本身,   我们可以把它看成是一个抽象工厂,对它的调用返回的是工厂生产的产

品,  如对代理对象的处理,对事务性代理的处理等

public class JpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
TransactionalRepositoryFactoryBeanSupport<T, S, ID> { private EntityManager entityManager; /**
* The {@link EntityManager} to be used.
*
* @param entityManager the entityManager to set
*/
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
} @Override
public void setMappingContext(MappingContext<?, ?> mappingContext) {
super.setMappingContext(mappingContext);
}

/*
   * 看这里
   */
@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
return createRepositoryFactory(entityManager);
} protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
return new JpaRepositoryFactory(entityManager);
} @Override
public void afterPropertiesSet() { Assert.notNull(entityManager, "EntityManager must not be null!");
super.afterPropertiesSet();
}
}

4、  JpaRepositoryFactory

    /*
* Create a repository instance as backing for the query proxy.
   * 实例化自定义RepositoryImpl时,会注入EntityManager    
*/
@Override
protected Object getTargetRepository(RepositoryInformation information) { SimpleJpaRepository<?, ?> repository = getTargetRepository(information, entityManager);
repository.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata()); return repository;
} /**
* Callback to create a {@link JpaRepository} instance with the given {@link EntityManager}
   *
*/
protected <T, ID extends Serializable> SimpleJpaRepository<?, ?> getTargetRepository(RepositoryInformation information, EntityManager entityManager) { JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType()); return getTargetRepositoryViaReflection(information, entityInformation, entityManager);
} /**
* Returns the base class backing the actual repository instance. Make sure
* {@link #getTargetRepository(RepositoryMetadata)} returns an instance of this class.
*/
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { if (isQueryDslExecutor(metadata.getRepositoryInterface())) {
return QueryDslJpaRepository.class;
} else {
return SimpleJpaRepository.class;
}
}

5、 自定义的RepositoryImpl时,注入自身项目的SessionContext(或者是其他依赖), 如何处理

其实Repository不应该注入SessionContext

解答:

重写JpaRepositoryFactory 的 getTargetRepository(RepositoryInformation information)方法

public class BaseRepositoryFactoryBean<R extends JpaRepository<T, ID>, T, ID extends Serializable> extends JpaRepositoryFactoryBean<R, T, ID> {

    @Autowired
private ISessionContext sessionContext; @Override
protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) { return new BaseRepositoryFactory(entityManager);
} private class BaseRepositoryFactory<T extends BaseEntity, I extends Serializable>
extends JpaRepositoryFactory { private final EntityManager entityManager; public BaseRepositoryFactory(EntityManager entityManager) {
super(entityManager);
this.entityManager = entityManager;
} @Override
protected Object getTargetRepository(RepositoryInformation information) {
Class<T> clazz = (Class<T>) information.getDomainType();
JpaEntityInformation<T, ID> entityInformation = getEntityInformation(clazz);
return new BaseRepositoryImpl<>(entityInformation, entityManager, sessionContext);
} @Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return BaseRepositoryImpl.class;
}
} }

从EnableJpaRepositories说开去的更多相关文章

  1. [Objective-C] 从NSInteger说开去

    在iOS开发过程中,我一直习惯于使用C语法里的基本类型,而很少用(除非必须使用)Foundation的数据类型.最近看了一些资料,发现自己这样写可能有风险,虽然目前没遇到过相关的问题,但这是非常需要注 ...

  2. 从Linux内核升级的必要性说开去

    Linux内核更新超级频繁,可是有必要时刻升级吗?个人感觉没有必要,可是你要时刻关注新特性列表,然后把自己的内核升级到离最新版本号差一两个月公布的版本号而不是最新版本号.以保证稳定性,由于一两个月的时 ...

  3. (转)2019年 React 新手学习指南 – 从 React 学习线路图说开去

    原文:https://www.html.cn/archives/10111 注:本文根据 React 开发者学习线路图(2018) 结构编写了很多新手如何学习 React 的建议.2019 年有标题党 ...

  4. 由SOAP说开去 - - 谈谈WebServices、RMI、RPC、SOA、REST、XML、JSON

    引子: 关于SOAP其实我一直模模糊糊不太理解,这种模模糊糊的感觉表述起来是这样: 在使用web服务时(功能接口),本来我就可以通过安卓中固有的http类(使用http协议),来发送http请求,并且 ...

  5. Java多线程总结之由synchronized说开去

    更新完毕,结贴,以后有新的想法再开新帖 这几天不断添加新内容,给个大概的提纲吧,方面朋友们阅读,各部分是用分割线隔开了的: synchronized与wait()/notify() JMM与synch ...

  6. 从谷歌 GFS 架构设计聊开去

    伟人说:“人多力量大.” 尼古拉斯赵四说:“没有什么事,是一顿饭解决不了的!!!如果有,那就两顿.” 研发说:“需求太多,人手不够.” 专家说:“人手不够,那就协调资源,攒人头.” 释义:一人拾柴火不 ...

  7. 从《BLAME!》说开去——新一代生产级卡通真实感混合的渲染方案

    <BLAME!>是Polygon Pictures Inc.(以下简称PPI)创业33周年以来制作的第一部CG剧场电影,故事来自于贰瓶勉的同名漫画作品(中文译名为<探索者>或者 ...

  8. 由底层和逻辑说开去——c++之类与对象的深入剖析

    类是什么,对象是什么,  这两个问题在各个c++书里面都以一种抽象的描述方式,给了我们近乎完美的答案,然后我好像就知道什么是类什么是对象了,但是当扪心自问,类在哪儿,对象在哪儿,成员方法在哪儿,成员变 ...

  9. 从一个Bug说开去--解决问题的思路,Linked Server, Bulk Insert, DataTable 作为参数传递

    声名— 部分内容为杜撰,如有雷同,不胜荣幸! 版权所有,如要引用,请标明出处! 如果打赏,请自便! 1       背景介绍 最近一周在忙一个SQL Server 的Bug,一个简单的Bug,更新两张 ...

随机推荐

  1. Go指南_切片的长度与容量

    源地址 https://tour.go-zh.org/moretypes/11 一.描述 切片拥有 长度 和 容量. 切片的长度就是它所包含的元素个数. 切片的容量是从它的第一个元素开始数,到其底层数 ...

  2. python -u 启动python文件的作用,PYTHONUNBUFFERED环境变量的作用

    python -u 启动python文件的作用是不缓存,直接把输出重定向到文件,比如nohup启动什么的,如果不使用-u启动,那么程序中的print和日志什么的,可能不会非常及时的重定向到out文件, ...

  3. [OpenCV] Samples 16: Decompose and Analyse RGB channels

    物体的颜色特征决定了灰度处理不是万能,对RGB分别处理具有相当的意义. #include <iostream> #include <stdio.h> #include &quo ...

  4. 为何谷歌围棋AI AlphaGo可能会把李世石击溃

    /* 版权声明:可以随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 谷歌DeepMind开发的人工智能围棋程序AlphaGo以5:0的压倒性优势击败了欧洲围棋冠军.专业二 ...

  5. akka pubsub example

    测了一个小时的 Pubsub 模式,发现这个模式和自己预期的不太一样,具体表现在: 1. 当 subscriber 订阅了某个 topic 并附带 groupName 时,如果 publish 发布的 ...

  6. yum安装VirtualBox

    参考官方文档: https://www.virtualbox.org/wiki/Linux_Downloads 配置yum源: vim /etc/yum.repos.d/virtualbox.repo ...

  7. IDEA自动编译设置

    ctrl+alt+s: ctrl+shift+alt+/:

  8. mysql格式化小数保留小数点后两位(小数点格式化)

    格式化浮点数的问题,用format(col,2)保留两位小数点,出现一个问题,例如下面的语句,后面我们给出解决方法 SELECT FORMAT(12562.6655,2); 结果:12,562.67 ...

  9. nginx 自启动脚本

    nginx 自启动脚本 创建脚本 cd /etc/init.d vi nginx 脚本如下: #! /bin/bash # chkconfig: 35 85 15 # description: Ngi ...

  10. 6.25python线程问题

    #encoding=utf-8 #2018-6-25 20:34:48 #解决耦合的问题#用队列解决这种问题,起到了缓冲的作用 import threading import time # #pyth ...