2010-10-20 90 views
6

我有一个包含注释的实体类,我导入到我的Web项目的包。 当tomcat部署该项目时,jar中的实体类不会被扫描注解。有没有办法告诉spring在jar文件中搜索带注释的类?即:休眠 - 弹簧注释的实体不从罐内扫描

<context:component-scan base-package="{path to jar or something}"/> 
+0

我忘了补充 - 我使用maven创建罐子 – 2010-10-20 17:28:10

回答

6

如果你的意思@Entity注释类,<context:component-scan>无关他们。 @Entity类是由Hibernate发现的,所以你需要配置Hibernate,而不是Spring。

如果使用通过JPA休眠(即你有persistence.xml),你需要添加下面一行到persistence.xml以便扫描/WEB-INF/lib/yourFileWithEntities.jar为实体类:

<jar-file>lib/yourFileWithEntities.jar</jar-file> 
+0

我不得不添加 WEB-INF/lib中/我的包-0.0.1-SNAPSHOT.jar,以便它的工作.. – Niko 2012-06-25 16:39:36

8

查找: http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html

第二个例子:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="packagesToScan" value="test.package"/> 
</bean>

放置两个通配符以扫描所有子包。 (例如:“test.package。**”)

+0

对于Hibernate 4:org.springframework.orm.hibernate4.LocalSessionFactoryBean完成这项工作:) – Tobias 2014-08-03 15:45:31

1

使用spring3.1或使用<class>com.***.***<class>标记在persistence.xml中添加实体类。