简体   繁体   中英

How to get all beans that have @Entity annotation?

I am trying to get bean with annotation but it's not working. 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 !!!!

Here is my context.xml file :

<?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

What wrong please ?

It does not work, because spring's getBeansWithAnnotation(Entity.class); will only return spring beans, but usual entities are not Spring beans, therefore this does now work.


Maybe there is a way in JPA/Hibernate to obtain all Mapped classes (classes not entity instances!!).

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. I used this tool some times ago and it worked well.

To add the code that @Ralph pointed to here:

  ClassPathScanningCandidateComponentProvider scanner =
        new ClassPathScanningCandidateComponentProvider(false);

  scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));

  for (BeanDefinition bd : scanner.findCandidateComponents("com.example.changeme"))
  {
     log.error(bd.getBeanClassName());
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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