java ResourceBundle

本文探讨了在 JDK 1.6 中使用 ResourceBundle 类进行资源文件加载的方法,并对比了不同环境下(如 Eclipse 插件与独立应用)的配置文件加载方式。通过具体的示例代码展示了如何在打包为 jar 或 war 文件的应用中正确读取配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jdk1.6中的ResourceBundle类,简单的用法:

    public boolean InitFromhiberante()
    {
    	ResourceBundle bundle = ResourceBundle.getBundle("config.databaseconnection");
		try
		{
			String url = bundle.getString("dataSource.url");// 设置路径
			String user = bundle.getString("dataSource.username");
			String pwd = bundle.getString("dataSource.password");
	   		Class.forName(bundle.getString("dataSource.driverClassName"));
        	conn = DriverManager.getConnection(url,user,pwd);    		 		
		}  catch (Exception e)
		{
			// TODO Auto-generated catch block
			Logger.getLogger(this.getClass()).error(e.getMessage(), e);
		}
    	return true;
    }
 

以上代码存入在axis2 war在tomcat D:\apache-tomcat-6.0.16\webapps\axis2\WEB-INF\classes\config解析后的不会出问题.

在分析axis myeclipse插件源代码时,其打成包时没出现问题源代码:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.axis2.tool.service.eclipse.plugin;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * The main plugin class to be used in the desktop.
 */
public class ServiceArchiver extends AbstractUIPlugin {
    //The shared instance.
    private static ServiceArchiver plugin;
    //Resource bundle.
    private ResourceBundle resourceBundle;
    private static ImageDescriptor wizardImageDescriptor;

    /**
     * The constructor.
     */
    public ServiceArchiver() {
        super();
        plugin = this;
        try {
            resourceBundle =
                    ResourceBundle.getBundle(
                            "org.apache.axis2.tool.service.resource.ServiceResources");
        } catch (MissingResourceException x) {
            resourceBundle = null;
        }
    }

    /**
     * This method is called upon plug-in activation
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
    }

    /**
     * This method is called when the plug-in is stopped
     */
    public void stop(BundleContext context) throws Exception {
        super.stop(context);
    }

    /**
     * Returns the shared instance.
     */
    public static ServiceArchiver getDefault() {
        return plugin;
    }

    /**
     * Returns the string from the plugin's resource bundle,
     * or 'key' if not found.
     */
    public static String getResourceString(String key) {
        ResourceBundle bundle = ServiceArchiver.getDefault().getResourceBundle();
        try {
            return (bundle != null) ? bundle.getString(key) : key;
        } catch (MissingResourceException e) {
            return key;
        }
    }

    /**
     * Returns the plugin's resource bundle,
     */
    public ResourceBundle getResourceBundle() {
        return resourceBundle;
    }

    public static ImageDescriptor getWizardImageDescriptor() {
        if (wizardImageDescriptor == null) {
            wizardImageDescriptor =
                    ServiceArchiver.imageDescriptorFromPlugin(
                            "Axis_Service_Archiver", "icons/asf-feather.gif");
        }
        return wizardImageDescriptor;
    }
}
 
以上不会出现问题。

而自己在axis2是打包后,以下代码采用:

    public boolean InitFromhiberante()
    {
    	ResourceBundle bundle = ResourceBundle.getBundle("com.leg3s.databaseconnection");//换打成jar包位置
		try
		{
			String url = bundle.getString("dataSource.url");// 设置路径
			String user = bundle.getString("dataSource.username");
			String pwd = bundle.getString("dataSource.password");
	   		Class.forName(bundle.getString("dataSource.driverClassName"));
        	conn = DriverManager.getConnection(url,user,pwd);    		 		
		}  catch (Exception e)
		{
			// TODO Auto-generated catch block
			Logger.getLogger(this.getClass()).error(e.getMessage(), e);
		}
    	return true;
    }

 会出现找不到些文件~

 

databaseconnection.properties配置文件如下:

写道


dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/sp?useUnicode=true&characterEncoding=UTF-8
dataSource.username=root
dataSource.password=123456

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值