2

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 ?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
Pracede
  • 4,226
  • 16
  • 65
  • 110

2 Answers2

2

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.

Ralph
  • 118,862
  • 56
  • 287
  • 383
1

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());
  }
serv-inc
  • 35,772
  • 9
  • 166
  • 188