使用ClassPathXmlApplicationContext 加载bean.xml文件才能将实现BeanFactoryAware接口的bean的BeanFactory注入进去。
spring的IoC容器核心为BeanFactory接口,该接口的最常用实现是XmlBeanFactory类,该类使用xml配置文件来初始化一个BeanFactory实例。
ApplicationContext是对BeanFactory接口的扩展,在大多数的应用中,使用ApplicationContext的优先级要高于BeanFactory,两者对比如下:
特性 |
BeanFactory |
ApplicationContext |
Bean 实例化/装配 |
Yes |
Yes |
自动 BeanPostProcessor 注册 |
No |
Yes |
自动 BeanFactoryPostProcessor 注册 |
No |
Yes |
便捷的 MessageSource 访问( i18n) |
No |
Yes |
ApplicationEvent 发送 |
No |
Yes |
package com.cc.bsp.persistence.dao.hibernate.expand.namedql.content;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import com.cc.bsp.persistence.dao.hibernate.expand.namedql.builder.DynamicHibernateStatementBuilder;
import com.cc.bsp.persistence.exception.NamedQLException;
import com.cc.bsp.persistence.exception.PersistenceException;
public class NamedQLContent implements BeanFactoryAware{
private static final Logger LOGGER = LoggerFactory.getLogger(NamedQLContent.class );
private static Map<String, String> namedHQLQueries = new HashMap<String, String>();
private static Map<String, String> namedSQLQueries = new HashMap<String, String>();
private static BeanFactory beanFactory = null;
private DynamicHibernateStatementBuilder hibernateStatementBuilder = null;
private static NamedQLContent instance = null;
private NamedQLContent(){
}
public static NamedQLContent getInstance(){
if(instance ==null){
instance = (NamedQLContent) beanFactory.getBean("namedQLContent");
instance.initialize();
}
return instance;
}
private DynamicHibernateStatementBuilder getHibernateStatementBuilder() {
return hibernateStatementBuilder;
}
public void setHibernateStatementBuilder(
DynamicHibernateStatementBuilder hibernateStatementBuilder) {
this.hibernateStatementBuilder = hibernateStatementBuilder;
}
public void reload(){
try {
getHibernateStatementBuilder().init();
namedHQLQueries.clear();
namedSQLQueries.clear();
Map<String, String> tempHQL = getHibernateStatementBuilder().getNamedHQLQueries();
Map<String, String> tempSQL = getHibernateStatementBuilder().getNamedSQLQueries();
namedHQLQueries.putAll(tempHQL);
namedSQLQueries.putAll(tempSQL);
} catch (IOException e) {
e.printStackTrace();
LOGGER.error(e.getMessage());
} catch (NamedQLException e) {
e.printStackTrace();
LOGGER.error(e.getMessage());
}
}
public void initialize() {
Map<String, String> tempHQL = getHibernateStatementBuilder().getNamedHQLQueries();
Map<String, String> tempSQL = getHibernateStatementBuilder().getNamedSQLQueries();
namedHQLQueries.putAll(tempHQL);
namedSQLQueries.putAll(tempSQL);
}
public void addHQL(String sqlName, String sqlstr)
throws PersistenceException {
if (namedHQLQueries.containsKey(sqlName)) {
throw new PersistenceException("sqlName is exits");
}
namedHQLQueries.put(sqlName, sqlstr);
}
public void addSQL(String sqlName, String sqlstr)
throws PersistenceException {
if (namedSQLQueries.containsKey(sqlName)) {
throw new PersistenceException("sqlName is exits");
}
namedSQLQueries.put(sqlName, sqlstr);
}
public String getHQL(String sqlName){
return namedHQLQueries.get(sqlName);
}
public String getSQL(String sqlName){
return namedSQLQueries.get(sqlName);
}
@SuppressWarnings("static-access")
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}
<bean id="namedQLContent" class="com.cc.bsp.persistence.dao.hibernate.expand.namedql.content.NamedQLContent" scope="singleton"> <property name="hibernateStatementBuilder" ref="hibernateStatementBuilder"></property> </bean>