BeanFactoryAware的使用

使用ClassPathXmlApplicationContext 加载bean.xml文件才能将实现BeanFactoryAware接口的bean的BeanFactory注入进去。

 

springIoC容器核心为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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值