grails学习笔记-5、conf下的配置文件

配置文件结构如下图:

大体说说每个文件的作用:

1、resources.groovy:Spring的bean在这里便配置。

// Place your Spring DSL code here
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean
import org.springframework.cache.ehcache.EhCacheFactoryBean
beans = {
	//----------------------EhCache配置---------------------------begin
	//EhCacheManagerFactoryBean创建的cacheManager是单例模式的
	cacheManager (EhCacheManagerFactoryBean){bean->
		shared=true
		configLocation="/WEB-INF/ehcache.xml"
	}//EhCacheManagerFactoryBean是一个FactoryBean,用来产生CacheManager
	advertismentCache(EhCacheFactoryBean){//EhCacheFactoryBean是一个FactoryBean,用来产生Cache
		cacheManager = ref("cacheManager")
		cacheName = "advertisementCache"
	}
	clientVersionCache(EhCacheFactoryBean){//EhCacheFactoryBean是一个FactoryBean,用来产生Cache
		cacheManager = ref("cacheManager")
		cacheName = "clientVersionCache"
	}
	//---------------------EhCache配置---------------------------end
}

上边是我一个项目中的。以前要在applicationContext.xml中配置的bean你可以挪到这里来。凡是这里定义的bean,你都可以直接注入到controller和service中使用。

例如这样

class BookController {    def reportCache//在resource文件中有定义

    def getElement(){

        Element element=reportCache.get(xx)

    }

}

service中也是一样。

2、ApplicationResources.groovy:这个在项目中基本没用到。不熟悉。

3、BootStrap.groovy。如果你要在程序启动后/程序结束时自动做一些工作,就在这里。

import com.eternal.photomanage.system.SysRole
import com.eternal.photomanage.system.SysUser
import com.eternal.photomanage.bussiness.Advertisement
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element
import org.apache.shiro.crypto.hash.Sha256Hash

class BootStrap {
	def advertisementService
	def clientVersionService
	Cache advertismentCache//缓存发布的广告
    def init = { servletContext ->
		//建立超级管理员用户
		//Create the admin role
	   def adminRole = SysRole.findByName('ROLE_ADMIN') ?:
			   new SysRole(name: 'ROLE_ADMIN').save(flush: true, failOnError: true)
	   // Create an admin SysUser
	   def adminUser = SysUser.findByUsername('admin') ?:
			   new SysUser(username: "admin",
			   passwordHash: new Sha256Hash('password').toHex(),
			   type:SysUser.SysUserType.SysUser.value)
			   .save(flush: true, failOnError: true)
	   // Add roles to the admin SysUser
	   assert adminUser.addToRoles(adminRole)
	   .save(flush: true, failOnError: true)
	   //Add permission to the role ROLE_ADMIN
	   assert adminRole.addToPermissions("*:*")
	   .save(flush: true, failOnError: true)
	   //查询发布的广告,将广告推送到缓存
	  advertisementService.pushAdvertisments()
	  // 查询客户端版本信息,将发布的版本信息放到缓存中
	   clientVersionService.pushNewVersion()
    }
    def destroy = {
    }
}
上边是我一个项目中的。我在程序启动后往缓存中推送了数据。注意:advertisementService和clientVersionService,这是我定义的两个service,可以直接注入到BootStrap.groovy中使用。两个服务类分别是AdvertisementService.groovy和ClientVersionService.groovy,注入时只要将类名的第一个字母小写就行了。destory一般不用,因为你只有通过命令(比如tomcat:catalina stop)或者停止服务等正常方式来停止服务器,才能调用destory。我们不能保证destory被调用。
4、BuildConfig.groovy。项目build是用到的信息。

grails.servlet.version = "2.5" // Change depending on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"

grails.project.dependency.resolution = {
    // inherit Grails' default dependencies
    inherits("global") {
        // specify dependency exclusions here; for example, uncomment this to disable ehcache:
        // excludes 'ehcache'
    }
    log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
    checksums true // Whether to verify checksums on resolve

    repositories {
        inherits true // Whether to inherit repository definitions from plugins

        grailsPlugins()
        grailsHome()
        grailsCentral()

        mavenLocal()
        mavenCentral()

        // uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
        //mavenRepo "http://snapshots.repository.codehaus.org"
        //mavenRepo "http://repository.codehaus.org"
        //mavenRepo "http://download.java.net/maven/2/"
        //mavenRepo "http://repository.jboss.com/maven2/"
    }
    dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.

        // runtime 'mysql:mysql-connector-java:5.1.20'
		runtime  'postgresql:postgresql:9.1-902.jdbc4'
    }

    plugins {
        runtime ":hibernate:$grailsVersion"
        runtime ":jquery:1.8.0"
        runtime ":resources:1.1.6"

        // Uncomment these (or add new ones) to enable additional resources capabilities
        //runtime ":zipped-resources:1.0"
        //runtime ":cached-resources:1.0"
        //runtime ":yui-minify-resources:0.1.4"

        build ":tomcat:$grailsVersion"

        runtime ":database-migration:1.1"

        compile ':cache:1.0.0'
		compile(':shiro:1.1.4'){//加上这句话后,安装插件和打包时也就不再引入shiro1.1.4所依赖的quartz-1.6.2.jar
			excludes "quartz"
		}
    }
}
grails.war.resources = { stagingDir, args ->
	copy(todir: "${stagingDir}/WEB-INF/classes"){
		fileset(dir:"grails-app/conf",includes:"log4jConf.groovy")
	}
	copy(todir: "${stagingDir}/WEB-INF/classes"){
		fileset(dir:"grails-app/conf",includes:"QuartzConfig.groovy")
	}
}

上边是我一个项目中的。详细说明看官方文档。我就不说了。我说要我曾遇到的问题。

(1)数据库驱动jar。我在lib中引入jar包,add to build path,refresh dependencies。开发中没遇到问题,编译也正常。单在运行时报"数据库驱动包找不着"。在上边的文件的dependencies中添加 runtime 'postgresql:postgresql:9.1-902.jdbc4'后解决。

(2)插件安装时遇到的问题。我在一个项目中引入了两个插件shiro和quartz2。shiro依赖quartz-1.6.2.jar,而quartz2依赖quartz-2.0.0。两者冲突导致quartz2插件无法运行。卸载插件shiro,在上边文件的plugins中添加:

compile(':shiro:1.1.4'){//加上这句话后,安装插件和打包时也就不再引入shiro1.1.4所依赖的quartz-1.6.2.jar
            excludes "quartz"

}

重新安装shiro,解决问题。

可以看到,dependencies中你可以添加对jar包的依赖。grails会自动通过maven在定义好的库(repositories {}中定义)中找到并下载下来使用。插件对jar包依赖则定义在plugins中。

(3)我要在打包时往war包中添加一些文件。可以通过

grails.war.resources = { stagingDir, args ->
	copy(todir: "${stagingDir}/WEB-INF/classes"){
		fileset(dir:"grails-app/conf",includes:"log4jConf.groovy")
	}
	copy(todir: "${stagingDir}/WEB-INF/classes"){
		fileset(dir:"grails-app/conf",includes:"QuartzConfig.groovy")
	}
}
来实现。这里为什么要拷贝这两个文件也是有原因的,以后会说明。

(4)Config.groovy。主要的配置文件。具体说明看官方文档。我遇到很多问题跟这个文件有关,后边会专门说明。

(5)DataSource.groovy:数据库配置文件。具体说明看官方文档。我遇到很多问题跟这个文件有关,后边会专门说明。

(6)UrlMapping.groovy:定义了grails项目中url的格式。我们一般不用动它。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值