应用配置与服务器配置全解析
立即解锁
发布时间: 2025-08-21 01:41:13 阅读量: 3 订阅数: 2 


OSGi与Spring构建模块化Java应用
# 应用配置与服务器配置全解析
## 1. 日志配置
在应用开发过程中,日志记录是一项重要的功能。为了控制应用及其依赖包的日志记录,我们需要对 Pax Logging 进行配置。Pax Logging 可以通过配置管理服务进行配置,其 PID 为 “org.ops4j.pax.logging”。
操作步骤如下:
1. 创建一个名为 `org.ops4j.pax.logging.properties` 的文件,路径为 `conf/services`。
2. 由于我们通过 Log4J API 使用 Pax Logging,因此可以在 `org.ops4j.pax.logging.properties` 文件中填充 Log4J 配置。以下是一个示例:
```properties
log4j.rootLogger=WARN, file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.threshold=INFO
log4j.appender.file.File=/Users/wallsc/logs/dude.log
log4j.appender.file.MaxBackupIndex=20
log4j.appender.file.MaxFileSize=20MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
```
在上述配置中,我们不仅将日志级别收紧为 WARN,还将日志记录到每日滚动的日志文件中,而不是控制台。重新构建分发 ZIP 文件,解压并启动应用后,你会发现控制台的日志输出减少了。
## 2. 应用细节配置
### 2.1 Spring 属性占位符配置 Spring Bean
在开发索引包和爬虫包时,我们硬编码了 Compass 索引路径和 Maven 仓库 URL。在生产环境部署应用时,我们需要能够从外部配置这些细节。Spring-DM 可以通过两种方式帮助我们实现这一目标:
- 用 OSGi 配置管理服务的值补充 Spring 的属性占位符功能。
- 用 OSGi 配置管理服务的值自动装配 Spring Bean 属性。
下面我们先来看如何使用 Spring 的属性占位符功能来配置索引包的索引路径。
Spring 框架支持通过 `<util:property-placeholder>` 元素将配置细节外部化。这个元素可以将 Spring 上下文定义中的硬编码值替换为占位符变量,然后在运行时,Spring 会用来自其他属性值源的值替换这些占位符。
例如,我们可以将 Compass 索引的硬编码路径替换为占位符变量:
```xml
<compass:compass name="compass" >
<compass:connection>
<compass:file path="${index.location}" />
</compass:connection>
<compass:mappings>
<compass:class name="dwmj.domain.JarFile"/>
</compass:mappings>
</compass:compass>
```
我们使用 `<util:property-placeholder>` 来将占位符变量 `${index.location}` 替换为在其他地方配置的值。通常,`<util:property-placeholder>` 会从属性文件中提取属性值,但我们希望它从 OSGi 配置管理服务中提取配置细节。
从 Spring 2.5.6 开始,`<util:property-placeholder>` 有了一个新的 `properties-ref=` 属性,用于指示它应该从另一个类型为 `java.util.Properties` 的 Spring Bean 中提取属性值。
我们可以在索引包的 `index-context.xml` 中添加以下 XML:
```xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:compass="http://www.compass-project.org/schema/spring-core-config"
xmlns:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.compass-project.org/schema/spring-core-config
http://www.compass-project.org/schema/spring-compass-core-config-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- ... -->
<ctx:property-placeholder properties-ref="cmProps" />
<!-- ... -->
</beans>
```
在这个例子中,`properties-ref=` 属性指定 `<util:property-placeholder>` 应该引用另一个 ID 为 `cmProps` 的 Spring Bean。
Spring-DM 在 Spring 综合命名空间中提供了一个 `<osgix:cm-properties>` 元素。这个元素从配置管理服务中读取属性,并在 Spring 上下文中创建一个包含这些属性值的 Bean。由 `<osgix:cm-properties>` 创建的 Bean 类型为 `java.util.Properties`,非常适合连接到 `<util:property-placeholder>`。
因此,如果我们要使用 OSGi 配置管理服务为 `<util:property-placeholder>` 提供属性,我们需要在 `index-osgi.xml` 中添加以下 `<osgix:cm-properties>`:
```xml
<beans:bean
```
0
0
复制全文