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获取注入对象的方法实例 的全部内容, 来源链接: utcz.com/z/358032.html

回到顶部