您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Mule-配置(spring--properties--log4j)
Mule中很好的结合了spring,在Mule3.x中将spring3.x作为核心组件,可以开箱即用,和一般J2EE应用开发无异。下面介绍一下:一.属性占位(property-placeholder)Mule利用springcontext:property-placeholder就能实现属性替换。比如:下面的${address}通过test.properties里的address属性替换。将来开发有数据库操作的时候,就可以把数据库相关配置放在jdbc.properties里了。[html]viewplaincopy1.?xmlversion=1.0encoding=UTF-8?2.3.mulexmlns:scripting=:http==:doc=:context=:spring==CE-3.3.1xmlns:xsi=:schemaLocation=9.://://://://://://://:property-placeholderlocation=classpath:test.properties/context:property-placeholder15.16.flowname=mule-maven-testFlow1doc:name=mule-maven-testFlow117.http:inbound-endpointexchange-pattern=request-responseaddress=http://${address}doc:name=HTTP/18.loggermessage=#[groovy:message.toString()]level=INFOdoc:name=LoggerMessage/19.scripting:componentdoc:name=Groovy20.scripting:scriptengine=Groovyfile=scripts/build-response.groovy/21./scripting:component22./flow23./mule注意:新创建的xxx.mflow中并没有xmlns:context要用context:property-placeholder必须加上:[html]viewplaincopy1.xmlns:context=因为Mule工程遵从maven规范,所以test.properties直接放在src/main/resources下即可。(还可以放在src/main/app下)二.读取properties类似的,将某个properties放在src/main/resources下,就可以通过spring配置来读取它。因此我们实现一个PropertyUtils静态类来实现读取。[java]viewplaincopy1.packagecom.neusoft.fx;2.3.importjava.io.IOException;4.importjava.io.InputStream;5.importjava.util.Properties;6.7.publicclassPropertyUtils{8.9.privatestaticPropertiesproperties;10.11.publicvoidinit(){12.ClassLoaderloader=Thread.currentThread().getContextClassLoader();13.InputStreamresourceAsStream=loader.getResourceAsStream(message.properties);14.properties=newProperties();15.try{16.properties.load(resourceAsStream);17.}catch(IOExceptione){18.e.printStackTrace();19.}20.}21.22.publicstaticStringget(Stringkey){23.return(String)properties.get(key);24.}25.}mule的配置:[html]viewplaincopy1.?xmlversion=1.0encoding=UTF-8?2.3.mulexmlns:scripting=:http==:doc=:context=:spring==CE-3.3.1xmlns:xsi=:schemaLocation=9.://://://://://://://:property-placeholderlocation=classpath:test.properties/context:property-placeholder14.spring:beans15.spring:beanid=propertyUtilsinit-method=initscope=singletonclass=com.neusoft.fx.PropertyUtilsname=Bean/16./spring:beans17.flowname=mule-maven-testFlow1doc:name=mule-maven-testFlow118.http:inbound-endpointexchange-pattern=request-responseaddress=http://${address}doc:name=HTTP/19.loggermessage=#[groovy:message.toString()]level=INFOdoc:name=LoggerMessage/20.scripting:componentdoc:name=Groovy21.scripting:scriptengine=Groovyfile=scripts/build-response.groovy/22./scripting:component23.loggermessage=#[groovy:message.toString()]level=INFOdoc:name=LoggerMessage/24./flow25./mule在groovy中也可以直接使用:[java]viewplaincopy1.importcom.neusoft.fx.*;2.3.defcontentType=message.getInboundProperty(Content-Type);4.defresponse=;5.if(contentType==application/xml){6.response=PropertyUtils.get(response.xml);7.}else{8.response=PropertyUtils.get(response.json);9.}10.11.response三.log4j通过MuleIDE创建的mule工程中并没有log4j的配置文件,直接拷贝一份到src/main/resources下即可实现对mule应用的log配置。例:[plain]viewplaincopy1.#Defaultloglevel2.log4j.rootCategory=INFO,console3.4.log4j.appender.console=org.apache.log4j.ConsoleAppender5.log4j.appender.console.layout=org.apache.log4j.PatternLayout6.log4j.appender.console.layout.ConversionPattern=%-5p%d[%t]%c:%m%n7.8.#log4j.appender.INFO=org.apache.log4j.ConsoleAppender9.#log4j.appender.INFO.layout=org.apache.log4j.PatternLayout10.#log4j.appender.INFO.layo
本文标题:Mule-配置(spring--properties--log4j)
链接地址:https://www.777doc.com/doc-4603257 .html