问题描述
我有一个程序包(例如packagesToScan
),其中包含一些我希望保留以@Entity
注释的类.
I've a package (say packagesToScan
) containing Classes that I wish to persist annotated with @Entity
.
在定义ApplicationContext
配置时,我已完成以下操作.
While defining ApplicationContext
configuration, I've done as follows.
@Configuration
@EnableJpaRepositories("packagesToScan")
@EnableTransactionManagement
@PropertySource("server/jdbc.properties")
@ComponentScan("packagesToScan")
public class JpaContext {
public class JpaContext {
...// Other configurations ....
...// Other configurations ....
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(this.dataSource());
emf.setJpaVendorAdapter(this.jpaVendorAdapter());
emf.setPackagesToScan("packagesToScan");
emf.setJpaProperties(this.hibernateProperties());
return emf;
}
在开发时,我在packagesToScan
中有一些不能满足持久性要求的类(例如没有主键等),因此,由于ApplicationContext
设置失败,我不允许运行测试.
While developing, I've some classes within packagesToScan
which doesn't satisfy requirements for persistence (like no primary keys etc) and due to this I'm not allowed to run test because of ApplicationContext
setup failure.
现在,有什么方法可以扫描packagesToScan
中的某些选定类别或忽略某些类别?
Now,Is there any way that I can scan just some selected classes or ignore some classes within packagesToScan
?
推荐答案
我一直在尝试解决相同的问题,最终得到如下解决方案:
I have been trying to solve the same problem and finally got a solution as below:
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan" value="com.mycompany.bean"/>
<property name="entityTypeFilters" ref="packagesToScanIncludeRegexPattern">
</property>
<property name="hibernateProperties">
// ...
</property>
</bean>
<bean id="packagesToScanIncludeRegexPattern" class="org.springframework.core.type.filter.RegexPatternTypeFilter" >
<constructor-arg index="0" value="^(?!com.mycompany.bean.Test).*"/>
</bean>
我意识到有一个,可用于过滤要包括的类.在此示例中,我使用了 RegexPatternTypeFilter ,但是有其他类型的过滤器.
I realized that there is a setEntityTypeFilters
function on the LocalSessionFactoryBean
class which can be used to filter which classes to be included. In this example I used RegexPatternTypeFilter but there are other types of filters as well.
还请注意,筛选器可用于包含语义.为了进行转换以排除语义,我不得不在正则表达式中使用否定前瞻.
Also note that the filters work with include semantics. In order to convert to exclude semantics I had to use negative lookahead in the regex.
此示例显示了xml配置,但转换为基于Java的配置应该很简单.
This example shows the xml configuration but it should be trivial to convert to java based configuration.
这篇关于扫描PackagesToScan时忽略一些类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!