浏览 2544 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2013-05-02
最后修改:2013-05-02
一个方法是专门为这些domain类写一个grails的插件plugin,然后各个系统引用这个插件就可以了。这个是最方便的,但是有时候我们并不确定其他的子系统是否一定是grails的WEB工程,或者是即使是grails工程,但不想引用插件,只想用个jar包就可以了,方便多了。 这个时候,就可以用@grails.persistence.Entity标注所有的domain类,然后将它们打包成一个jar包。 接下来,分两种情况使用: 第一方式:如果是普通的groovy或者java工程 增加一个spring的配置文件:applicationContext-gorm.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:gorm="http://grails.org/schema/gorm" xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://grails.org/schema/gorm http://grails.org/schema/gorm/gorm.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:mysql://127.0.0.1:3306/enquiry?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <!--<property name="configClass" value="org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration"/>--> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"/> </bean> <gorm:sessionFactory base-package="com.baoxian" data-source-ref="dataSource" message-source-ref="messageSource"> <!-- config-location="classpath:hibernate.cfg.xml" --> <property name="hibernateProperties"> <util:map> <entry key="hibernate.hbm2ddl.auto" value="update"/> <entry key="hibernate.hibernate.show_sql" value="true"/> <entry key="hibernate.cache.use_second_level_cache" value="true"/> <entry key="hibernate.cache.use_query_cache" value="true"/> <entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider"/> </util:map> </property> <property name="eventListeners"> <map> <entry key="flush"> <bean class="org.codehaus.groovy.grails.orm.hibernate.events.PatchedDefaultFlushEventListener"/> </entry> <entry key="pre-load"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="post-load"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="save"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="save-update"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="post-insert"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="pre-update"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="post-update"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="pre-delete"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> <entry key="post-delete"> <bean class="org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor"/> </entry> </map> </property> </gorm:sessionFactory> <context:annotation-config/> <context:mbean-export/> <tx:annotation-driven/> <context:component-scan base-package="com.baoxian.*"/> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans> 注:最重要的就是上面的那句:<context:component-scan base-package="com.baoxian.*"/>,这个就是你要扫描你的entity的基本路径。 然后启动的时候加载下这个配置文件即可: ClassPathXmlApplicationContext( ["applicationContext-gorm.xml"] as String[]) 接下来就可以直接用了:VehicleEnquiryPojo vehicleEnquiryPojo = VehicleEnquiryPojo.findById(id) //从数据库中获取 第二种方式:在Grails工程里面使用 这个方式的话就稍微复杂一点,需要先写一个gorm的插件,里面只有一个类AnnotationDomainClassConfig: public class AnnotationDomainClassConfig implements InitializingBean { private static Log LOG = LogFactory.getLog(GrailsApplicationFactoryBean.class); private String basePackage="com.baoxian"; private GrailsApplication grailsApplication; private GrailsPluginManager pluginManager; private List<TypeFilter> filters; public void afterPropertiesSet() throws Exception { for(String name:scan(basePackage)){ Class<?> clazz=Class.forName(name, true, Thread.currentThread().getContextClassLoader()) ; ((DefaultGrailsApplication)grailsApplication).addOverridableArtefact(DomainClassArtefactHandler.TYPE, clazz) ; } } private List<String> scan(String basePackage){ String[] basePackages = StringUtils.commaDelimitedListToStringArray(basePackage) ; BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false); scanner.setIncludeAnnotationConfig(false); scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); if(filters!=null&&filters.size()>0){ for(TypeFilter filter:filters){ scanner.addExcludeFilter(filter); } } scanner.scan(basePackages) ; ArrayList<String> result=new ArrayList<String>(); for(String name:registry.getBeanDefinitionNames()){ result.add(registry.getBeanDefinition(name).getBeanClassName()); } return result; } public void setGrailsApplication(GrailsApplication grailsApplication) { this.grailsApplication = grailsApplication; } public void setPluginManager(GrailsPluginManager pluginManager) { this.pluginManager = pluginManager; } public void setBasePackage(String basePackage) { this.basePackage = basePackage; } public void setFilters(List<TypeFilter> filters) { this.filters = filters; } } 然后打成插件:com.baoxian.plugins:gormpatch:1.0.0-SNAPSHOT 接下来,先在BuildConfig.groovy配置文件中加入这个domain类的jar包的依赖和gorm补丁插件的依赖: compile('com.baoxian:baoxian-datatype-base-domain:1.0.0-SNAPSHOT') compile('com.baoxian.plugins:gormpatch:1.0.0-SNAPSHOT') 然后修改/web-app/WEB-INF/applicationContext.xml,加入下面的: <bean id="gormPatch" class="com.baoxian.commons.gorm.AnnotationDomainClassConfig"> <!--包路径,可以用逗号分开--> <property name="basePackage" value="com.baoxian.datatype,com.baoxian.provider.inscar.domain"/> <property name="pluginManager" ref="pluginManager"/> <property name="grailsApplication" ref="grailsApplication"/> </bean> 接下来就可以正常使用grom了:VehicleEnquiryPojo vehicleEnquiryPojo = VehicleEnquiryPojo.findById(id) //从数据库中获取 不过,这里还有个小问题:我有个grails工程,在domain文件夹里面有个domain类A,它依赖于jar包中的domain类B,然后运行的时候,就报错了,说找不到B.findby...方法,我在想是不是加载domain类A的时候还没有将jar包中的domain加载进来。 不知道其他人有没有遇到这种情况。 实在没办法,只有将所有的domain都移到jar里面去了。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |