RestFul Web服务,它读取属性文件

时间:2015-06-18 15:52:53

标签: java rest

我有几个Jersey REST Web服务说 SendPassword ResetPassword ,其目的是发送电子邮件。

为了发送电子邮件,我在tomcat下配置了一个属性文件,所有这一切都正常

SendPassword.java的代码有点像这样

 @Path("/sendpassword")
    public class SendPassword {
        @GET
        @Produces("application/json")
        public String sendPasswordToEmail(@QueryParam("empid") String empid)
                throws JSONException
        {
            try {

            SendEmailUtility.sendmail("weqw","2312");

        }
        catch(Exception e)
        {
        }
}

SendEmailUtility.java

public class SendEmailUtility 
    {
    public static String sendmail(String sendemalto,String generatedpwd) throws IOException {
        String result = "fail";
        File configDir = new File(System.getProperty("catalina.base"), "conf");
        File configFile = new File(configDir, "email.properties");
        InputStream stream = new FileInputStream(configFile);
        Properties props_load = new Properties();
        props_load.load(stream);
        final String username = props_load.getProperty("username");
        final String password = props_load.getProperty("password");
        Properties props_send = new Properties();
        props_send.put("mail.smtp.auth","true");
        props_send.put("mail.smtp.starttls.enable","true");
                        Transport.send(message);
          // Some code to send email
            result = "success";
        } catch (MessagingException e) {
            result = "fail";
            e.printStackTrace();
        }
        return result;
    }

}

正如您所看到的,我正在阅读每次调用websercice的属性文件

(由于阅读操作有点贵),有没有办法解决这个问题?

请您告诉我最新的处理方法。

提前致谢。

3 个答案:

答案 0 :(得分:2)

我建议使用资源包,它不需要InputStream

创建一个属性文件,并将其与java代码一起直接放在包中

示例文件夹结构

com
   - preethi
            -SendPassword.java
            -email.properties

然后你可以像

一样编码
ResourceBundle props_load = ResourceBundle.getBundle("com.preethi.email");
final String username = props_load.getString("username");

这样您就不必担心打开和关闭流或文件路径

答案 1 :(得分:1)

有几种方法可以做到这一点,一种方法是使props_load成为类的私有静态成员并像这样调用它

    public class SendEmailUtility 
    {
       private static Properties props;

      private static Properties getProperties() {
       if (props == null) {
        File configDir = new File(System.getProperty("catalina.base"), "conf");
        File configFile = new File(configDir, "email.properties");
        InputStream stream = new FileInputStream(configFile);
        props = new Properties();
        props.load(stream);
    }
    return props; 
    }
    public static String sendmail(String sendemalto,String generatedpwd) throws  IOException {
        String result = "fail";
        Properties props_load = getProperties();
        final String username = props_load.getProperty("username");
        final String password = props_load.getProperty("password");
        Properties props_send = new Properties();
        props_send.put("mail.smtp.auth","true");
        props_send.put("mail.smtp.starttls.enable","true");
                Transport.send(message);
          // Some code to send email
            result = "success";
        } catch (MessagingException e) {
            result = "fail";
            e.printStackTrace();
        }
        return result;
    }

}

我建议的其他设计是制作一个电子邮件服务类,如EmailSender或EmailService,然后将其注入SendPassword类

@Path("/sendpassword")
    public class SendPassword {
        @Autowired
        EmailService emailService;
        @GET
        @Produces("application/json")
        public String sendPasswordToEmail(@QueryParam("empid") String empid)
                throws JSONException
        {

答案 2 :(得分:0)

您可以使用lazy-getter来获取和缓存Properties对象。

private static Properties props;

private static Properties getProperties() {
    if (props == null) {
        File configDir = new File(System.getProperty("catalina.base"), "conf");
        File configFile = new File(configDir, "email.properties");
        InputStream stream = new FileInputStream(configFile);
        props = new Properties();
        props.load(stream);
    }
    return props;
}

每次要使用“属性”时,请致电getProperties()。它会在第一次调用时缓存它。每个后续调用都将返回缓存对象。

注意:此示例不会捕获任何异常。