spring中通过ApplicationContext getBean获取注入对象的方法实例

 更新时间:2019年03月30日 11:05:20   作者:helentang1987  
今天小编就为大家分享一篇关于spring中通过ApplicationContext getBean获取注入对象的方法实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

用SpringContextUtil实现ApplicationContextAware

package util;
import java.util.Locale;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContextUtil
 implements ApplicationContextAware
{
 private static ApplicationContext context;
 @Override
 public void setApplicationContext(ApplicationContext contex)
  throws BeansException
 {
  System.out.println("--------------------contex---------"+contex);
  SpringContextUtil.context = contex;
 }
 public static ApplicationContext getApplicationContext() { 
   return context; 
 } 
 public static Object getBean(String beanName) {
  return context.getBean(beanName);
 }
 public static String getMessage(String key) {
  return context.getMessage(key, null, Locale.getDefault());
 }
}

工具类

package redis;
import redis.clients.jedis.JedisPool;
import util.SpringContextUtil;
public class RedisUtil {
 private static JedisPool jedisPool;
 static{
  jedisPool = (JedisPool)SpringContextUtil.getBean("jedisPool"); 
 }
  public static JedisPool getJedisPool(){
  if(jedisPool == null){
   jedisPool = (JedisPool)SpringContextUtil.getBean("jedisPool"); 
  }
  return jedisPool;
  }
  public void flusDB(){
  jedisPool.getResource().flushDB();
  }
  public static String set(String key,String value){
  return jedisPool.getResource().set(key, value);
  }
  public static String get(String key){
  return jedisPool.getResource().get(key);
  }
  public static Long del(String key){
  return jedisPool.getResource().del(key);
  }
}

在Spring的配置文件中配置这个类,Spring容器会在加载完Spring容器后把上下文对象调用这个对象中的setApplicationContext方法

<!--1 自动扫描 将标注Spring注解的类自动转化Bean--> 
 <context:component-scan base-package="com.first,com.util" /> 
 <!--2 加载数据资源属性文件 --> 
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  <property name="locations">
   <list>
   <value>classpath:jdbc.properties</value>
   <value>classpath:redis.properties</value>
   </list>
  </property>
 </bean> 
 <bean id="springContextUtil" class="util.SpringContextUtil"></bean>
 <import resource="redis-config.xml"/>
在web项目中的web.xml中配置加载Spring容器的Listener
<!-- 初始化Spring容器,让Spring容器随Web应用的启动而自动启动 --> 
<listener> 
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener>

spring配置文件注入Bean类

 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="300" /> <!-- 最大能够保持idel状态的对象数 -->
    <property name="testOnBorrow" value="true" /> <!-- 当调用borrow Object方法时,是否进行有效性检查 -->
    <property name="maxActive" value="200" /> 
    <property name="minIdle" value="10"/> 
     <property name="maxWait" value="300" /> 
     <property name="testOnReturn" value="true" /> 
     <property name="testWhileIdle" value="true" /> 
  </bean>
  <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
    <constructor-arg name="host" value="${redis_addr}" />
    <constructor-arg name="port" value="${redis_port}" type="int" />
    <constructor-arg name="timeout" value="${redis_timeout}" type="int" />
    <constructor-arg name="password" value="#{'${redis_password}'!=''?'${redis_password}':null}" />
    <constructor-arg name="database" value="${redis_db_index}" type="int" />
  </bean>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • Spring手写简化版MVC流程详解

    Spring手写简化版MVC流程详解

    Spring MVC是Spring Framework的一部分,是基于Java实现MVC的轻量级Web框架。本文将通过简单示例带大家掌握SpringMVC简化版手写方法,感兴趣的可以了解一下
    2022-11-11
  • Java如何操作MongoDB常用API文档

    Java如何操作MongoDB常用API文档

    这篇文章主要介绍了Java如何操作MongoDB常用API文档,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • SpringBoot2整合Redis缓存三步骤代码详解

    SpringBoot2整合Redis缓存三步骤代码详解

    这篇文章主要介绍了SpringBoot2整合Redis缓存三步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Spring中BeanFactory解析bean详解

    Spring中BeanFactory解析bean详解

    本篇文章主要介绍了Spring中BeanFactory解析bean详解 ,详细的介绍了使用BeanFactory对bean进行解析的实例,有兴趣的可以了解一下。
    2017-04-04
  • JMeter参数化4种实现方式(小结)

    JMeter参数化4种实现方式(小结)

    参数化是自动化测试脚本的一种常用技巧,可将脚本中的某些输入使用参数来代替,JMeter提供了多种参数化方式,下面就其中常用的4种展开阐述,感兴趣的可以来了解一下
    2021-12-12
  • 关于Java创建线程的2种方式以及对比

    关于Java创建线程的2种方式以及对比

    这篇文章主要给大家介绍了关于Java创建线程的2种方式以及对比的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-01-01
  • 解析Springboot集成Tile38客户端之Set命令实现示例

    解析Springboot集成Tile38客户端之Set命令实现示例

    这篇文章主要为大家介绍了解析Springboot集成Tile38客户端之Set命令实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Java SpringCache+Redis缓存数据详解

    Java SpringCache+Redis缓存数据详解

    本篇文章主要介绍了浅谈SpringCache与redis缓存数据的解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-10-10
  • netflix.discovery.shared.transport.TransportException:Cannot execute request on any known server

    netflix.discovery.shared.transport.TransportException:Cannot

    这篇文章主要介绍了netflix.discovery.shared.transport.TransportException:Cannot execute request on any known server报错问题及解决方法,感兴趣的朋友一起看看吧
    2023-09-09
  • 关于 Math.random()生成指定范围内的随机数的公式推导问题

    关于 Math.random()生成指定范围内的随机数的公式推导问题

    在 java 中,用于生成随机数的 Math 方法 random()只能生成 0-1 之间的随机数,而对于生成指定区间,例如 a-b 之间的随机数,却只能用相关计算公式,今天通过本文给大家介绍Math.random()生成随机数的公式推导问题,感兴趣的朋友一起看看吧
    2022-09-09

最新评论