zoukankan      html  css  js  c++  java
  • 单例模式应用_ _ _同时涉及到监听器的应用

    当第一次启动项目的时候,就需要访问一些数据,仔细思考一下,

      1)就会想到该数据的应该是全局的变量,而且不会被改动,这时候我们想到了单例

      2)又 “第一次启动项目的时候” 这句话,我们想到了监听器,具体就想到了 ServletContextListener

    eg:

    单例:FileUploadAppProperties

     1 import java.util.HashMap;
     2 import java.util.Map;
     3 
     4 public class FileUploadAppProperties {
     5     
     6     private Map<String, String> properties = new HashMap<>();
     7     
     8     private FileUploadAppProperties(){}
     9     
    10     private static FileUploadAppProperties instance = new FileUploadAppProperties();
    11     
    12     public static FileUploadAppProperties getInstance() {
    13         return instance;
    14     }
    15     
    16     public void addProperty(String propertyName, String propertyValue){
    17         properties.put(propertyName, propertyValue);
    18     }
    19     
    20     public String getProperty(String propertyName){
    21         return properties.get(propertyName);
    22     }
    23     
    24 }

    监听器:FileUploadAppListener

     1 import java.io.File;
     2 import java.io.IOException;
     3 import java.io.InputStream;
     4 import java.util.Map;
     5 import java.util.Properties;
     6 
     7 import javax.servlet.ServletContextEvent;
     8 import javax.servlet.ServletContextListener;
     9 
    10 import com.atguigu.fileupload.app.utils.FileUploadAppProperties;
    11 
    12 /**
    13  * Application Lifecycle Listener implementation class FileUploadAppListener
    14  *
    15  */
    16 public class FileUploadAppListener implements ServletContextListener {
    17 
    18     /**
    19      * Default constructor. 
    20      */
    21     public FileUploadAppListener() {
    22         // TODO Auto-generated constructor stub
    23     }
    24 
    25     /**
    26      * @see ServletContextListener#contextInitialized(ServletContextEvent)
    27      */
    28     public void contextInitialized(ServletContextEvent arg0) {
    29         InputStream in = getClass().getClassLoader().getResourceAsStream("/upload.properties");
    30         
    31         Properties properties = new Properties();
    32         try {
    33             properties.load(in);
    34             
    35             for(Map.Entry<Object, Object> prop: properties.entrySet()){
    36                 String propertyName = (String) prop.getKey();
    37                 String propertyValue = (String) prop.getValue();
    38                 
    39                 FileUploadAppProperties.getInstance().addProperty(propertyName, propertyValue);
    40             }
    41             
    42         } catch (IOException e) {
    43             e.printStackTrace();
    44         }
    45     }
    46 
    47     /**
    48      * @see ServletContextListener#contextDestroyed(ServletContextEvent)
    49      */
    50     public void contextDestroyed(ServletContextEvent arg0) {
    51         // TODO Auto-generated method stub
    52     }
    53     
    54 }

    src / upload.properties

    1 exts=pptx,docx,doc
    2 file.max.size=1048576
    3 total.file.max.size=5242880
    纸上学来终觉浅,觉知此事需躬行
  • 相关阅读:
    问题001:Java软件,属于系统软件还是应用软件呢?
    Apache.Tomcat 调用Servlet原理之Class类的反射机制,用orc类解释
    CharSquence 接口的作用,多态以增强String
    eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>
    tomcat.apache startup.bat闪退两种解决方法
    c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538
    JSON格式自动解析遇到的调用方法问题.fromJson() ..readValue()
    shell command to replace UltraEdit
    根据内容最后一位进行排序
    利用left join 筛选B表中不包含A表记录
  • 原文地址:https://www.cnblogs.com/dreamHighMjc/p/7577384.html
Copyright © 2011-2022 走看看