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>

总结

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

相关文章

  • Java数组扩容实例代码

    Java数组扩容实例代码

    这篇文章主要介绍了Java数组扩容实例代码,具有一定借鉴价值,需要的朋友可以参考下
    2017-11-11
  • Java正则表达式之分组和替换方式

    Java正则表达式之分组和替换方式

    这篇文章主要介绍了Java正则表达式之分组和替换方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java pdu短信解码全面解析

    Java pdu短信解码全面解析

    本文是根据python的方法改写的pdu短信解码,非常不错,代码简单易懂具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-10-10
  • Java多线程实现Callable接口

    Java多线程实现Callable接口

    本文给大家分享的是使用Java多线程来实现callable接口的方法,以及使用方法,另外还有一个网友的实例,希望能够对大家掌握Java多线程有所帮助。
    2016-06-06
  • 玩转spring boot 结合jQuery和AngularJs(3)

    玩转spring boot 结合jQuery和AngularJs(3)

    玩转spring boot,这篇文章主要介绍了结合jQuery和AngularJs,玩转spring boot,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • 关于ResponseEntity类和HttpEntity及跨平台路径问题

    关于ResponseEntity类和HttpEntity及跨平台路径问题

    这篇文章主要介绍了关于ResponseEntity类和HttpEntity及跨平台路径问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • Spring动态配置计时器触发时间的实例代码

    Spring动态配置计时器触发时间的实例代码

    这篇文章主要介绍了Spring动态配置计时器触发时间的实例代码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-06-06
  • Java日常练习题,每天进步一点点(58)

    Java日常练习题,每天进步一点点(58)

    下面小编就为大家带来一篇Java基础的几道练习题(分享)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望可以帮到你
    2021-08-08
  • Java设计模式之代理模式详解

    Java设计模式之代理模式详解

    这篇文章主要介绍了Java设计模式之代理模式详解,文中有非常详细的代码示例,对正在学习java的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-05-05
  • Java对象比较之equals与hashCode详解

    Java对象比较之equals与hashCode详解

    这篇文章主要介绍了Java对象比较之equals与hashCode详解,equals 方法和 hashCode 方法是 Object 类中的两个基础方法,它们共同协作来判断两个对象是否相等,需要的朋友可以参考下
    2023-12-12

最新评论