现在的位置: 首页 > 综合 > 正文

applicationContext对象获取方法

2014年09月05日 ⁄ 综合 ⁄ 共 3149字 ⁄ 字号 评论关闭

代码获取spring applicationContext中的bean对象

初次使用SSH时发现很多朋友在使用

Java代码 

  1. ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");  

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");



获取ApplicationContext对象,进过自己测试这段代码适合C/S程序,在javaweb中这样获取ApplicationContext对象是很慢的。每次去getBean(”beanid“)时都会去加载applicationContext.xml",这样效率很低的。 

Spring中提供了获取ApplicationContext对象的接口,在org.springframework.context.ApplicationContextAware中我们只需实现ApplicationContextAware接口的setApplicationContext方法,然后Spring的配置文件applicationContext.xml中注册实现ApplicationContextAware接口的类的类,并用静态的ApplicationContext变量把ApplicationContext保存下来,并在web.config中加上以下代码在网站启动时加载到内存中,然后就可以随心所欲的使用静态的ApplicationContext了。 


继承接口代码: 

Java代码 

  1. package com.ssh.springfactory;  

  2. import org.springframework.beans.BeansException;  

  3. import org.springframework.context.ApplicationContext;  

  4. import org.springframework.context.ApplicationContextAware;  

  5. publicclass SpringFactory implements ApplicationContextAware{  

  6. privatestatic ApplicationContext context;  

  7. @Override

  8. publicvoid setApplicationContext(ApplicationContext applicationContext)  

  9. throws BeansException {  

  10. this.context=applicationContext;  

  11.    }  

  12. publicstatic Object getObject(String id) {  

  13.        Object object = null;  

  14.        object = context.getBean(id);  

  15. return object;  

  16.    }  

  17. }  

package com.ssh.springfactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringFactory implements ApplicationContextAware{

private static ApplicationContext context;

@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context=applicationContext;
}


public static Object getObject(String id) {
Object object = null;
object = context.getBean(id);
return object;
}
}




注册对象,因为SpringFactory继承了ApplicationContextAware接口,我们在applicationContext.xml中注册该类: 

Java代码 

  1. <bean id="springfactory"class="com.ssh.springfactory.SpringFactory"></bean>
     

<bean id="springfactory" class="com.ssh.springfactory.SpringFactory"></bean>




在web.config中初始化Spring容器Ioc: 

Java代码 

  1. <listener>  

  2.    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

  3. </listener>  

  4. <context-param>  

  5.    <param-name>contextConfigLocation</param-name>  

  6.    <param-value>/WEB-INF/applicationContext.xml</param-value>  

  7. </context-param>  

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>




然后就可以随心所欲的使用SpringFactory中的getObject方法了: 

Java代码 

  1. publicstatic Object getObject(String id) {  

  2.    Object object = null;  

  3.    object = context.getBean(id);  

  4. return object;  

  5. }  

public static Object getObject(String id) {
Object object = null;
object = context.getBean(id);
return object;
}



这样就不会再每次去获得对象时去new IOC对象了: 

Java代码 

  1. ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");  

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");



你们也可以自己去断点ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");这段代码,用的时间不是一般的长……^_^.  

抱歉!评论已关闭.