因为applicationContext.xml 是唯一的,所以可以写成单例模式
package com.founder.util; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; final public class ApplicationContextUtil { /** * 由于applicationContext.xml 的唯一性,在这里可以把它写成单例模式 */ private static ApplicationContext applicationContext = null; private ApplicationContextUtil(){} //空的构造方法 static{ /** * static 静态代码快,只在该类被加载时,执行一次 */ applicationContext = new ClassPathXmlApplicationContext("com/founder/config/applicationContext.xml"); } public static ApplicationContext getApplicationContext(){ return applicationContext; } }
package com.founder.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.founder.service.UserService; import com.founder.util.ApplicationContextUtil; public class Test { public static void main(String[] arg){ /** * 传统的方法,调用UserService 的 sayHello()方法 */ /*UserService userService = new UserService(); userService.setName("Aloys"); userService.sayHello();*/ /** * 接下来,我们使用Spring的框架 * * 1.引入Spring的开发包 * 2.添加到build path路径 * 3.创建applicationContext.xml * 4.配置bean * 5.得到spring容器对象 */ /*ApplicationContext appContex = new ClassPathXmlApplicationContext("com/founder/config/applicationContext.xml"); //通过类路径来加载配置文件 UserService userService = (UserService) appContex.getBean("userService"); userService.sayHello();*/ ((UserService)(ApplicationContextUtil.getApplicationContext().getBean("userService"))).sayHello(); } }
单例模式:
0
Java 单例相关问题5
Singleton 模式主要保证在JAVA应用程序中,一个Class只有一个实例存在。一般Singleton模式通常有几种形式:
//第一种形式:定义一个类,他的构造函数为private的,它有一个静态的私有变量,在类初始化时实例化,通过一个public的getIntance方法获取对它的引用,继而调用其中的方法。 public class Singleton { private static Singleton instance = new Singleton(); private Singleton(){} public static Singleton getInstance() { return instance; } } //第二种形式: public class Singleton { private static Singleton instance; private Singleton() {} public static Singleton getInstance() { if(instance == null) { return new Singleton (); } return instance; } } }
问题补充:
public class Singleton { private static Singleton instance = null; public static synchronized Singleton getInstance() { if (instance==null) instance=new Singleton(); return instance; } }
//还可以再优化: public class Singleton { private static Singleton instance = null; public static Singleton getInstance() { if (instance !=null ) return instance ; else { } instance=new Singleton(); return instance; } } synchronized
//还可以再优化: public class Singleton { private static Singleton instance = null; public static Singleton getInstance() { if (instance !=null ) return instance ; else { } instance=new Singleton(); return instance; } } synchronized
你看完这篇http://coolshell.cn/articles/265.html
估计就全明白了
第一种不是懒惰加载,初始化会费时一点点,也许花了时间初始化后面不一定真用得上.
第二种方法如果经常被调用,因为有同步问题,会对性能有影响.
其实单例有更换的办法,可以使用内部类,枚举,或者双重锁.推荐我收藏的几个Blog给你看看.
http://chenjianjx.iteye.com/blog/1839117
http://www.importnew.com/6461.html
http://jiangzhengjun.iteye.com/blog/652440
第二种方法如果经常被调用,因为有同步问题,会对性能有影响.
其实单例有更换的办法,可以使用内部类,枚举,或者双重锁.推荐我收藏的几个Blog给你看看.
http://chenjianjx.iteye.com/blog/1839117
http://www.importnew.com/6461.html
http://jiangzhengjun.iteye.com/blog/652440
第一种问题,对象开始可以不初始化,等到获取实例的时候才初始化;
第二种问题,没有线程同步,会有线程安全问题。
相关推荐
《ApplicationContext.xml——Spring框架的核心配置文件详解》 在Java开发领域,Spring框架是不可或缺的一部分,它以其强大的依赖注入(DI)和面向切面编程(AOP)能力,为开发者提供了便利。而`ApplicationContext...
<set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/> ``` Spring 处理 Struts 中 Action 类的依赖关系主要有三种方法: 1. **使用 `...
同时,由于Struts2默认创建单例Action,但Spring通常管理多例Bean,因此需要在`applicationContext.xml`中为每个Action Bean添加`scope="prototype"`属性,确保每次请求都能得到新的Action实例。 4. **Hibernate...
集成的关键在于WebWork的配置文件`xwork.xml`和Spring的配置文件`applicationContext.xml`之间的桥梁搭建。具体来说: 1. **XWork配置文件** (`xwork.xml`):通过`external-ref`元素引用Spring管理的Bean,例如: ...
`applicationContext.xml`是Spring应用上下文的主配置文件,它定义了bean的声明、bean之间的依赖关系以及其他配置信息。在Spring框架中,每个bean都代表一个对象实例,这些bean通过XML或注解方式定义,并由Spring...
例如,我们可以在`applicationContext.xml`配置文件中定义bean,Spring会根据配置生成并管理这些bean。 【Spring的bean配置】 在Spring中,我们使用XML文件来配置bean。`<bean>`元素代表一个对象,`id`属性是bean...
"app+hib"配置文件通常指的是应用于Struts2框架的应用上下文(`applicationContext.xml`)和Hibernate持久化层的配置文件(`hibernate.cfg.xml`)。这两个文件对于Struts2应用的性能优化至关重要。 首先,我们来...
- **加入 Spring 配置文件**:创建Spring的配置文件,通常命名为`applicationContext.xml`。 - **配置 web.xml**:在`web.xml`中配置Spring上下文初始化监听器,以便于启动时加载Spring配置。 3. **整合 Spring ...
然后,在`applicationContext.xml`中,我们定义Action类的Bean,并添加`scope="prototype"`,因为每个HTTP请求都需要一个新的Action实例,而Spring默认使用单例模式: ```xml <bean id="yourAction" class="com....
可以在 applicationContext.xml 文件中加载一个 ResourceBundleMessageSource Bean,例如: <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <value>...
最后,虽然使用工具类的方法可以方便地获取ApplicationContext,但需要谨慎处理单例的问题,确保线程安全,以及避免重复创建ApplicationContext实例,这可能会导致资源浪费或者产生异常。在实际项目中,还应该根据...
本篇将深入解析Spring框架的配置机制,主要围绕默认配置文件`applicationContext.xml`进行详细讲解。 一、Spring配置文件基础 `applicationContext.xml`是Spring应用上下文的主要配置文件,用于定义bean的实例化、...
在上面的配置中,我们使用了ContextLoaderListener来加载Spring容器,并指定了applicationContext.xml文件的位置。 通过使用ApplicationContext,我们可以轻松地获取到Spring容器中的bean,并且可以根据需要来获取...
在本例中,通过 `<bean>` 标签在 `applicationContext.xml` 中定义 Bean,如 `myDAO`,并设置其类及单例属性。 2. **WebWork2 IoC**:WebWork2 自带了简单的IoC功能,允许在 `xwork.xml` 中配置 Action 依赖的 Bean...
例如,如果有 `applicationContext-db.xml`、`applicationContext-service.xml` 等,可以这样写: ```xml <param-name>contextConfigLocation <param-value>/WEB-INF/applicationContext-db.xml,/WEB-INF/...
return new String[] { "file:webapp/WEB-INF/applicationContext.xml","file:webapp/WEB-INF/action-servlet.xml" }; } } 在上面的代码中,我们继承了AbstractDependencyInjectionSpringContextTest类,并 ...
Spring的配置通常分为多个文件,如`applicationContext-action.xml`、`applicationContext-service.xml`、`applicationContext-dao.xml`和`applicationContext-common.xml`,分别对应Action层、Service层、DAO层和...
- 创建Spring的核心配置文件(如`applicationContext.xml`),配置Bean的定义、数据源、事务管理器等。根据描述,这里使用的是Spring 2.0,注意非单例模式的配置应使用`scope="prototype"`。 5. **处理jar包冲突**...
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student1 = applicationContext.getBean("student", Student.class); Student ...
- Spring默认会在/WEB-INF目录下查找名为`applicationContext.xml`的配置文件,但这个默认配置可以通过在`web.xml`中重定向来改变。 - Spring的配置文件可以放在类路径(classpath)下,并可以重命名,此时需要在`...