首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >axis2网络服务。在哪里放置我自己的配置

axis2网络服务。在哪里放置我自己的配置
EN

Stack Overflow用户
提问于 2010-02-17 20:35:29
回答 2查看 4.8K关注 0票数 0

我已经使用axis2和POJO部署(到Tomcat服务器)用Java语言编写了Web Service。我的服务打开一个到MySQL数据库的连接。为此,我需要连接字符串。我把连接字符串放在哪里,这样我就不必把它硬编码到代码中?我如何从代码中访问它?我希望在服务级别的某个位置设置此参数,而不是全局设置整个服务器。这个是可能的吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-17 20:47:15

您可以使用tomcat为您配置数据库连接,然后使用JNDI查找javax.sql.DataSource。

看看tomcat的这些内容:

使用JNDI还意味着你会自动变得更兼容一些,以防你需要移动到不同的web容器/应用服务器。

票数 1
EN

Stack Overflow用户

发布于 2010-03-04 18:11:05

如果要使用配置文件,可以在以下位置放置一个配置文件:

代码语言:javascript
复制
axis2/WEB-INF/services/classes/config-file.xml

您可以使用AxisService类加载器在代码中访问该文件,该类加载器在startUp(ConfigurationContext configctx,AxisService服务)方法期间可用。当您的服务启动时(无论是部署后还是容器重启后),都会触发startUp()。

代码语言:javascript
复制
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属性:

代码语言:javascript
复制
<!-- 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文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2280570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档