Method instance of getting injection object through ApplicationContext getBean in spring

  • 2021-07-13 05:22:52
  • OfStack

Realization of ApplicationContextAware with SpringContextUtil


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());
 }
}

Tool class


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);
  }
}

Configure this class in the configuration file of Spring, and the Spring container will call the setApplicationContext method in the context object after loading the Spring container


<!--1  Automatic scanning   Will mark Spring Automatic class conversion of annotations Bean--> 
 <context:component-scan base-package="com.first,com.util" /> 
 <!--2  Load data resource properties file  --> 
 <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"/>
 In web In the project web.xml Configuration loading in Spring Container Listener
<!--  Initialization Spring Container, let Spring Container with Web Start automatically when the application starts  --> 
<listener> 
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener>

The spring configuration file is injected into the Bean class


 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="300" /> <!--  Maximum can be maintained idel Number of objects in state  -->
    <property name="testOnBorrow" value="true" /> <!--  When the call borrow Object Method, whether to check the validity  -->
    <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>

Summarize


Related articles: