⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib0114.html

📁 j2ee架构师手册
💻 HTML
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Configuration Management Strategies</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0113.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0115.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch17"></a>
<div class="section">
<h2 class="first-section-title"><a name="567"></a><a name="ch17lev1sec4"></a>Configuration Management Strategies</h2><p class="first-para">Most J2EE applications have some configurable properties, such as:</p>
<ul class="itemizedlist">
<li class="first-listitem">
<p class="first-para">Names for database connection pools</p>
<a name="568"></a><a name="IDX-238"></a>
</li>
<li class="listitem">
<p class="first-para">Logging level indicators</p>
</li>
<li class="listitem">
<p class="first-para">Mail group names for error messages</p>
</li>
</ul>
<p class="para">I usually have one class that's responsible for reading and interpreting configuration files, and I put static accessors on that class to make the properties available. An example appears in <a class="internaljump" href="#ch17list09">listing 17.9</a>.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 17.9: </span>Implementing an Environment Class</span><a name="569"></a><a name="ch17list09"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
   1:public class SampleEnvironment
   2:{
   3:    // Some code omitted
   4:    public static String getDatabaseConnectionPoolName()
   5:    {
   6:        return _myEnvironment.getProperty("db.pool");
   7:    }
   8:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">Having each object read the configuration file and search for the properties they care about is more correct from an object-oriented design perspective. However, it simply isn't practical in many cases. If your application has 300 classes that need access to application properties, having them all read a configuration file is a bit disk intensive. Many times, the number of needed properties exceeds what can practically be fed into the JVM as a system property (with the "-D" option) at runtime.</p>
<p class="para">The advantage of using one class is that configuration file management occurs in one place in the application. Developers can find and change the class easily, and it's simple to reference the application properties from other classes. You can even change configuration file formats and not affect the rest of your application.</p>
<p class="para">CementJ provides a base class called <span class="fixed">ApplicationEnvironment</span> (from package <span class="fixed">org.cementj.base</span>) that provides basic functionality. <span class="fixed">ApplicationEnvironment</span>, by default, provides support for configuration files in a properties format (see <span class="fixed">java.util.Properties</span>), but it's possible to override that and use XML or other formats. <span class="fixed">ApplicationEnvironment</span> also checks for file updates at a configurable interval. This feature is useful because it allows you to change the behavior of your application without stopping and restarting your EJB container.</p>
<p class="para">
<a class="internaljump" href="#ch17list10">Listing 17.10</a> is an example implementation of <span class="fixed">Application-Environment</span>. See CementJ JavaDoc for additional details.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 17.10: </span>Implementing the ApplicationEnvironment Class</span><a name="570"></a><a name="ch17list10"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
   1:package book.sample.env;
   2:
   3:import org.cementj.base.ApplicationEnvironment;
   4:
   5:public class SampleEnvironment
   6:    extends ApplicationEnvironment
   7:{
   8:    protected SampleEnvironment(){}
   9:
  10:    protected String getConfigurationFileName()
  11:        {return CONFIG_FILE_NAME;}
  12:    private static final SampleEnvironment
  13:        _myEnvironment = new SampleEnvironment();
  14:
  15:    public static String getDatabaseConnectionPoolName()
  16:    {
  17:        return _myEnvironment.getProperty("db.pool");
  18:    }
  19:
  20:    public static final String     CONFIG_FILE_NAME
  21:                                = "myapp.properties";
  22:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<a name="571"></a><a name="IDX-239"></a>
<p class="para">Another example of establishing strategies for exception handling, logging, and coding conventions drawn from the open source community can be found at <a target="_top" class="url" href="http://jakarta.apache.org/cactus/participating/coding_conventions.html">http://jakarta.apache.org/cactus/participating/coding_conventions.html</a>.</p>
<p class="last-para">This example is the coding conventions established for the open source product Cactus that is used to facilitate writing test cases for server-side classes, such as servlets or enterprise beans. The URL above is an excellent example of formally establishing architectural policies discussed in this chapter and communicating them. The exception-handling and logging strategies do differ somewhat from the advice I've provided here. I still provide it as an example because establishing clear guidelines for developers to follow is more important than the minor disagreements I have with some of the line items.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0113.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0115.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -