Java 如何获取所有具有@Entity注释的bean?
我正在尝试使用注释获取bean,但它不起作用。我想知道为什么? 以下是我的代码片段: ConfigurableApplicationContext SuggesterContext=createContext(“context.xml”); //查找所有@Entities类 Map beansWithAnnotation=SuggesterContext.getBeansWithAnnotation(Entity.class) 我的地图大小是0 以下是我的context.xml文件:Java 如何获取所有具有@Entity注释的bean?,java,spring,spring-data,spring-annotations,Java,Spring,Spring Data,Spring Annotations,我正在尝试使用注释获取bean,但它不起作用。我想知道为什么? 以下是我的代码片段: ConfigurableApplicationContext SuggesterContext=createContext(“context.xml”); //查找所有@Entities类 Map beansWithAnnotation=SuggesterContext.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)
将只返回Springbean,但通常的实体不是Springbean,因此现在可以工作了
也许在JPA/Hibernate中有一种方法可以获得所有映射的类(类不是实体实例!!)
这里讨论了通过某些注释查找所有类的其他方法:,在其中一个答案中,提到了一个名为“”的库。我以前使用过这个工具,效果很好。也许反射会有所帮助?您可以获取所有注释并检查必要的