SpringDM笔记6-Fragment及配置Log4j Fragment Bundle
?? 未配置日志配置文件前,系统会抛出下面异常:
?? log4j:WARN No appenders could be found forlogger(org.springframework.osgi.extender.internal
?? .boot.ChainActivator). log4j:WARN Please initialize the log4j system properly.
1. Using the fragement configuration pattern
??? 一个Fragment是一个incomplate的Bundle,没有指定主Bundle是不能单独存在的,他不许附属于一个Host
??? Bundle. 它可以包含配置信息,classs文件,资源文件,但它不能有自己的Activator类或类加载器。
?
??? The host bundle can’t itself be a fragment and must be a full-fledged bundle, even if it relies on its
??? fragment to add classes or resources. A fragment can serve many purposes, such as completing
??? its host bundle with specific classes or providing configuration through resources (property or XML
??? files).
2. SpringDM's extender Export出来的通过Fragment可被修改的值:
??? (1)spring-osgi-extender.jar Bean Values Exposed for Fragments:见附件spring-osgi-extender.jar?
??? Bean Values Exposed for Fragments.jpg;
??? (2)spring-osgi-web-extender.jar Bean Values Exposed for Fragments:见附件spring-osgi-web-
??? extender.jar Bean Values Exposed for Fragments.jpg.
3. 实现Log4jFragment
??? (1) Fragement目录结构:
??? log4j-config/
?????? META-INF/
????????? MANIFEST.MF
?????? log4j.properties???????
??? (2)MAINFESt.MF文件:
???? Manifest-Version: 1.0
???? Fragment-Host: com.springsource.org.apache.log4j
???? Bundle-Version: 1.0.0
???? Bundle-Name: Log4J Configuration
???? Created-By: 1.6.0_13 (Sun Microsystems Inc.)
???? Bundle-ManifestVersion: 2
???? Bundle-SymbolicName: com.springsource.org.apache.log4j.config
?
???? (3) log4j.properties:
???? log4j.rootLogger=INFO, FILE
???? log4j.appender.FILE=org.apache.log4j.FileAppender
???? log4j.appender.FILE.File=SpringDM.log
???? log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
???? log4j.appender.FILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n%M
????
???? (4) 打包生成JAR:
???? 在log4j-config上一级目录中执行:
???? jar cvfm log4j-config.jar log4j-config/META-INF/MANIFEST.MF -C log4j-config 。
???? 生成log4j-config.jar。
4.? 安装该Fragement至OSGi 容器
???? 在config.ini文件组中增加:bundles/log/log4j-config.jar
???? 启动容器后,该Bundle的状态为:RESOLVED。
5.? 实现启用SpringDM注解功能的Fragment
???? (1)目录组织结构
???? SpringDM-Annotation-config/
???????????? META-INF/
??????????????? spring/
????????????????????? extender/
??????????????????????????? annotation-activator.xml
??????????????? MANIFEST.MF??
???? (2) MANIFEST.MF文件
???? Manifest-Version: 1.0
???? Bundle-ManifestVersion: 2
???? Fragment-Host: org.springframework.bundle.osgi.extender
???? Bundle-SymbolicName: com.apress.springosgi.ch4.fragment
???? Bundle-Name: HelloWorld Spring-OSGi Fragment
???? Bundle-Description: Spring-DM Fragment for activating OSGi annotation scanning
???? (3) annotation-activator.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"
??????????? xsi:schemaLocation="http://www.springframework.org/schema/beans
??????????? http://www.springframework.org/schema/beans/spring-beans.xsd">
??????????? <bean id="extenderProperties">
?????????????????? <property name="process.annotations" value="true"/>
??????????? </bean>
???? </beans>