我已经使用axis2和POJO部署(到Tomcat服务器)用Java语言编写了Web Service。我的服务打开一个到MySQL数据库的连接。为此,我需要连接字符串。我把连接字符串放在哪里,这样我就不必把它硬编码到代码中?我如何从代码中访问它?我希望在服务级别的某个位置设置此参数,而不是全局设置整个服务器。这个是可能的吗?
发布于 2010-02-17 20:47:15
您可以使用tomcat为您配置数据库连接,然后使用JNDI查找javax.sql.DataSource。
看看tomcat的这些内容:
使用JNDI还意味着你会自动变得更兼容一些,以防你需要移动到不同的web容器/应用服务器。
发布于 2010-03-04 18:11:05
如果要使用配置文件,可以在以下位置放置一个配置文件:
axis2/WEB-INF/services/classes/config-file.xml您可以使用AxisService类加载器在代码中访问该文件,该类加载器在startUp(ConfigurationContext configctx,AxisService服务)方法期间可用。当您的服务启动时(无论是部署后还是容器重启后),都会触发startUp()。
import org.apache.axis2.engine.ServiceLifeCycle;
public class LifeCycleImpl implements ServiceLifeCycle {
public void startUp(ConfigurationContext configctx, AxisService service) {
InputStream in = service.getClassLoader().getResourceAsStream("config-file.xml");
//Extract your database config from the input stream
//Create database connection
//Store the connection as a service parameter using service.AddParameter
}在服务实现类的init(ServiceContext serviceContext)方法期间,您可以通过ServiceContext.getAxisService().getParamterValue()方法访问在ServiceLifeCycle.startUp()期间创建的数据库连接。
注意:您必须在服务的services.xml文件中指定ServiceLifeCycle实现类,作为service标记的class属性:
<!-- The class attribute defines the hook into the Service lifecycle methods
startUp and shutDown -->
<service name="YourService" class="com.macima.webservice.LifeCycleImpl">
<!--Specify the web service's implementation class -->
<parameter name="ServiceClass">com.macima.webservice.ServiceImpl</parameter>
<!--Declare methods exposed by the web service-->
<operation name="getSomething">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</parameter>
</service>使用这种方法,您的配置文件保存在aar文件之外。这样做的好处是,您可以在不同的测试环境中升级相同的aar文件,在环境特定的配置文件中选择每个环境的相关设置。此外,您可以编辑配置文件,而不必打开aar文件。
https://stackoverflow.com/questions/2280570
复制相似问题