1. 程式人生 > >單例模式及搭建配置文件

單例模式及搭建配置文件

height style asstream prope ssp sources 控制 input man

單例模式

  技術分享圖片

//讀取配置文件的工具類-單例模式
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>                                                                        

單例模式及搭建配置文件