我正在尝试使用注释获取bean,但它不起作用。我想知道为什么 ? 这是我的代码片段:
ConfigurableApplicationContext suggestorContext = createContext(“context.xml”); //找到所有的@Entities课程 Map 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
请问有什么不对?
答案 0 :(得分:1)
它不起作用,因为spring的getBeansWithAnnotation(Entity.class);
只返回spring bean,但通常的实体不是Spring bean,因此现在可以正常工作。
也许在JPA / Hibernate中有一种方法可以获取所有Mapped类(类不是实体实例!!)。
这里讨论了通过某些注释查找所有类的其他方法:Scanning Java annotations at runtime,在其中一个答案中,提到了一个名为“Google reflections”的库。我之前使用过这个工具,效果很好。
答案 1 :(得分:0)
在此处添加@Ralph 指向的代码:
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
for (BeanDefinition bd : scanner.findCandidateComponents("com.example.changeme"))
{
log.error(bd.getBeanClassName());
}