java - 如何获取所有带有@Entity注解的bean?

标签 java spring spring-data spring-annotations

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

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

我的 map 大小是 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 中有一种方法可以获取所有映射类(类而不是实体实例!!)。

此处讨论了通过某些注释查找所有类的其他方法:Scanning Java annotations at runtime ,在其中一个答案中,提到了一个名为“Google reflections ”的库(后来移至 https://github.com/ronmamo/reflections )。我前段时间使用过这个工具,效果很好。

关于java - 如何获取所有带有@Entity注解的bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26593055/

相关文章:

java - 带有字符串连接的 Spring 属性占位符

java - 为什么第一次调用的响应时间比下面的长

Spring Security 自定义过滤器链代理使用 java 配置进行 mvc 应用程序

java - 在 gradle 中使用 spring 数据时出现 NoClassFoundDefError

java - 了解上下文切换期间的 Java 内存模型

java - GWT RPC 调用在运行时异常后不会回滚事务

java - 在 Oracle 12C jdbc 驱动程序中获取自动递增 key 时出现 NPE

java - Hibernate 一对一单向更新

mysql - 在 Spring Boot 中生成动态查询

hibernate - 使用 Spring Boot 和数据 JPA,尽管有 OpenSesionInViewFilter ,但仍然获得 LazyInitializationException