[英]How to get all beans that have @Entity annotation?
I am trying to get bean with annotation but it's not working.我正在尝试使用注释获取 bean,但它不起作用。 I wonder why ?我想知道为什么 ? Here is my code snippet :这是我的代码片段:
ConfigurableApplicationContext suggestorContext = createContext("context.xml");
// Find all @Entities classes
Map<String, Object> beansWithAnnotation = suggestorContext.getBeansWithAnnotation(Entity.class);
my map size is 0 !!!!我的地图大小是 0 !!!!
Here is my context.xml 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:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="ismDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="user"/>
<property name="password" value="password"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
</bean>
<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="ismDS"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="generateDdl" value="false"/>
</bean>
</property>
<property name="packagesToScan" value="com.myproject.entities.entity"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<jpa:repositories base-package="com.mypackage.dao"
entity-manager-factory-ref="myEmf"/>
</beans>
All my entities are in the following package : com.myproject.entities.entity
我所有的实体都在以下包中: com.myproject.entities.entity
What wrong please ?请问有什么问题?
It does not work, because spring's getBeansWithAnnotation(Entity.class);
它不起作用,因为 spring 的getBeansWithAnnotation(Entity.class);
will only return spring beans, but usual entities are not Spring beans, therefore this does now work.只会返回 spring bean,但通常的实体不是 Spring bean,因此现在可以工作了。
Maybe there is a way in JPA/Hibernate to obtain all Mapped classes (classes not entity instances!!).也许在 JPA/Hibernate 中有一种方法可以获取所有映射的类(类不是实体实例!!)。
An other ways to find all classes by some annotations are discussed here: Scanning Java annotations at runtime , in one of the answers, a lib called " Google reflections " (later moved to https://github.com/ronmamo/reflections ) is mentioned.此处讨论了通过某些注释查找所有类的另一种方法: 在运行时扫描 Java 注释,在其中一个答案中,一个名为“ Google 反射”的库(后来移至https://github.com/ronmamo/reflections )是提及。 I used this tool some times ago and it worked well.我以前用过这个工具,效果很好。
To add the code that @Ralph pointed to here:添加@Ralph 指向此处的代码:
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
for (BeanDefinition bd : scanner.findCandidateComponents("com.example.changeme"))
{
log.error(bd.getBeanClassName());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.