请教关于entity:repository标签什么含义?

本文解析了Spring框架中context.xml文件内的非bean定义部分,重点介绍了<entity:repository>标签的使用方法及其属性,包括如何指定接口、实现类及属性配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在spring的context.xml里面下面的非bean定义是什么意思?

<entity:repository id="projectRepository"
interfaces="com.........ProjectRepository"
class="com...........impl.ProjectRepositoryImpl">
<property name="type" value="com......Project" />
</entity:repository>
和这种方式比呐 package com.kucun.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.core.ResolvableType; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.Map; @Service public class DynamicRepositoryService { @Autowired private ApplicationContext applicationContext; private final Map<String, JpaRepository<?, ?>> repositoryMap = new HashMap<>(); private final Map<Class<?>, JpaRepository<?, ?>> classRepositoryMap = new HashMap<>(); public Map<Class<?>, JpaRepository<?, ?>> getClassRepositoryMap() { return classRepositoryMap; } @PostConstruct public void init() { Map<String, JpaRepository> repositories = applicationContext.getBeansOfType(JpaRepository.class); repositories.forEach((beanName, repo) -> { Class<?> entityClass = resolveEntityClass(repo); if (entityClass != null) { repositoryMap.put(entityClass.getSimpleName().toLowerCase(), repo); classRepositoryMap.put(entityClass, repo); } }); } /** * 使用 ResolvableType 正确解析代理对象的泛型参数 */ private Class<?> resolveEntityClass(JpaRepository<?, ?> repository) { // 创建 ResolvableType 从 Repository 实例 ResolvableType repoType = ResolvableType.forClass(repository.getClass()); // 沿着继承链向上查找 JpaRepository 接口 ResolvableType jpaRepositoryType = repoType.as(JpaRepository.class); // 获取第一个泛型参数(实体类型) ResolvableType entityType = jpaRepositoryType.getGeneric(0); // 解析为 Class 对象 return entityType.resolve(); } public JpaRepository<?, ?> getJpaRepository(String entityName) { JpaRepository<?, ?> repo = repositoryMap.get(entityName.toLowerCase()); if (repo == null) { throw new IllegalArgumentException("未找到实体 " + entityName + " 对应的Repository"); } return repo; } public JpaRepository<?, ?> getJpaRepository(Class<?> entityClass) { JpaRepository<?, ?> repo = classRepositoryMap.get(entityClass); if (repo == null) { throw new IllegalArgumentException("未找到实体类 " + entityClass.getName() + " 对应的Repository"); } return repo; } public Map<String, JpaRepository<?, ?>> getRepositoryNameMap() { return repositoryMap; } }
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值