温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring中通过ApplicationContext getBean获取注入对象的示例分析

发布时间:2021-05-22 11:16:45 来源:亿速云 阅读:372 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关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中通过ApplicationContext getBean获取注入对象的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI