繁体   English   中英

如何获取所有具有@Entity 注释的bean?

[英]How to get all beans that have @Entity annotation?

我正在尝试使用注释获取 bean,但它不起作用。 我想知道为什么 ? 这是我的代码片段:

ConfigurableApplicationContext suggestorContext = createContext("context.xml");
// Find all @Entities classes
Map<String, Object> beansWithAnnotation = suggestorContext.getBeansWithAnnotation(Entity.class);

我的地图大小是 0 !!!!

这是我的 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>

我所有的实体都在以下包中: com.myproject.entities.entity

请问有什么问题?

它不起作用,因为 spring 的getBeansWithAnnotation(Entity.class); 只会返回 spring bean,但通常的实体不是 Spring bean,因此现在可以工作了。


也许在 JPA/Hibernate 中有一种方法可以获取所有映射的类(类不是实体实例!!)。

此处讨论了通过某些注释查找所有类的另一种方法: 在运行时扫描 Java 注释,在其中一个答案中,一个名为“ Google 反射”的库(后来移至https://github.com/ronmamo/reflections )是提及。 我以前用过这个工具,效果很好。

添加@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM