码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式及搭建配置文件

时间:2018-08-13 12:05:55      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:--   -o   idt   ace   pac   nmap   listener   ops   trace   

单例模式

  技术分享图片

//读取配置文件的工具类-单例模式
public class ConfigManager {
    private static Properties properties;
    private static ConfigManager configManager;

    // 私有构造器-读取数据库配置文件
    private ConfigManager() {
        properties = new Properties();
        String configFile = "database.properties";
        InputStream is = ConfigManager.class.getClassLoader()
                .getResourceAsStream(configFile);
        try {
            properties.load(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 全局访问点
    public static ConfigManager getInstance() {
        if (configManager == null) {
            configManager = new ConfigManager();
        }
        return configManager;
    }

    public String getValue(String key) {
        return properties.getProperty(key);
    }

}

 懒汉模式

  技术分享图片

 饿汉模式

  技术分享图片

  技术分享图片

Spring MVC-Controller的单例管理

  技术分享图片


 

搭建超市订单管理系统的配置文件

 applicationContext-jdbc.xml

<!-- 使spring扫描包下的所有类,让标注spring注解的类生效 
        若扫描到有@Component @Controller @Service等这些注解的类,则把这些类注册为bean -->
<context:component-scan base-package="cn.smbms.service"/> 
<context:component-scan base-package="cn.smbms.dao"/>

 springmvc-servlet.xml

<!-- 控制器的扫描 -->                                                                      
<context:component-scan base-package="cn.smbms.controller"/>                         
<mvc:annotation-driven/>                                                             
                                                                                     
<!-- 静态资源文件的引入 -->                                                                   
<mvc:resources location="/statics/" mapping="/statics/**"/>                          
                                                                                     
<!-- 视图解析:prefix:前缀,suffix:后缀 -->                                                    
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">     
    <property name="prefix" value="/WEB-INF/jsp/"/>                                  
    <property name="suffix" value=".jsp"/>                                           
</bean>                                                                              
                                                                                     
<!-- 全局异常 -->                                                                        
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">                                              
        <props>                                                                      
            <prop key="java.lang.RuntimeException">error</prop>                      
        </props>                                                                     
    </property>                                                                      
</bean>                                                                              

 web.xml

<!-- log4j config path -->                                                                
<context-param>                                                                           
    <param-name>log4jConfigLocation</param-name>                                          
    <param-value>classpath:log4j.properties</param-value>                                 
</context-param>                                                                          
<!-- webapp root path -->                                                                 
<context-param>                                                                           
    <param-name>webAppRootKey</param-name>                                                
    <param-value>SMBMS.root</param-value>                                                 
</context-param>                                                                          
<!-- Spring相关的配置 -->                                                                      
<context-param>                                                                           
    <param-name>contextConfigLocation</param-name>                                        
    <param-value>classpath:applicationContext-*.xml</param-value>                         
</context-param>                                                                          
                                                                                          
<!-- 监听器 -->                                                                              
<listener>                                                                                
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>     
</listener>                                                                               
<!-- 初始化Spring容器 -->                                                                      
<listener>                                                                                
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>                                                                               
                                                                                          
<!-- 字符编码过滤器 -->                                                                          
<filter>                                                                                  
    <filter-name>encodingFilter</filter-name>                                             
    <filter-class>                                                                        
        org.springframework.web.filter.CharacterEncodingFilter                            
    </filter-class>                                                                       
    <init-param>                                                                          
        <param-name>encoding</param-name>                                                 
        <param-value>UTF-8</param-value>                                                  
    </init-param>                                                                         
    <init-param>                                                                          
        <!-- 强制编码格式 -->                                                                   
        <param-name>forceEncoding</param-name>                                            
        <param-value>true</param-value>                                                   
    </init-param>                                                                         
</filter>                                                                                 
<filter-mapping>                                                                          
    <filter-name>encodingFilter</filter-name>                                             
    <url-pattern>/*</url-pattern>                                                         
</filter-mapping>                                                                         
                                                                                          
<!-- 配置Servlet -->                                                                        
<servlet>                                                                                 
    <servlet-name>springmvc</servlet-name>                                                
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
    <init-param>                                                                          
        <param-name>contextConfigLocation</param-name>                                    
        <param-value>classpath:springmvc-servlet.xml</param-value>                        
    </init-param>                                                                         
    <load-on-startup>1</load-on-startup>                                                  
</servlet>                                                                                
<servlet-mapping>                                                                         
    <servlet-name>springmvc</servlet-name>                                                
    <url-pattern>/</url-pattern>                                                          
</servlet-mapping>                                                                        

 

单例模式及搭建配置文件

标签:--   -o   idt   ace   pac   nmap   listener   ops   trace   

原文地址:https://www.cnblogs.com/xhddbky/p/9456132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!