spring在xml文件中配置bean的三种方法
2014-07-02 22:15
260 查看
一.最常见,也是缺省,是调用spring的缺省工厂类
spring缺省工厂类:org.springframework.beans.factory.support.DefaultListableBeanFactory使用其静态方法preInstantiateSingletons()
配置文件中最普通最基本的定义一个普通bean
<bean id="DvdTypeDAOBean" class="com.machome.dvd.impl.DvdTypeDAO" ></bean>
二.使用用户自定义的工厂类的静态方法进行创建
定义工厂类,返回new实例
public class DvdTypeDAOFactory {
public static DvdTypeDAO getInstance(){
return new DvdTypeDAO();
}
}
这里只是简单的定义了一个工厂类,你可以定义更复杂的:
比如单例类(提高利用效率)
比如含有ThreadLocal属性的类(多线程下解决线程安全问题)
spring配置文件中, bean的class不设要new的类,而是设成工厂类,然后后面加factory-method属性
<bean id="DvdTypeDAOBean" class="com.machome.core.impl.DvdTypeDAOFactory" factory-method="getInstance" />
三.在spring中建立工厂类的Bean,然后调用该工厂bean建立真正的bean
public class DvdTypeDAOFactory {
public DvdTypeDAO getInstance(){
return new DvdTypeDAO();
}
}
spring配置文件中
<bean id="DvdTypeDAOFactoryBean" class="com.machome.core.impl.DvdTypeDAOFactory" />
这个工厂bean还是被spring的DefaultListableBeanFactory. preInstantiateSingletons创建
<bean id="DvdTypeDAOBean" factory-bean="DvdTypeDAOFactoryBean" factory-method="getInstance" />
spring缺省工厂类:org.springframework.beans.factory.support.DefaultListableBeanFactory使用其静态方法preInstantiateSingletons()
配置文件中最普通最基本的定义一个普通bean
<bean id="DvdTypeDAOBean" class="com.machome.dvd.impl.DvdTypeDAO" ></bean>
二.使用用户自定义的工厂类的静态方法进行创建
定义工厂类,返回new实例
public class DvdTypeDAOFactory {
public static DvdTypeDAO getInstance(){
return new DvdTypeDAO();
}
}
这里只是简单的定义了一个工厂类,你可以定义更复杂的:
比如单例类(提高利用效率)
比如含有ThreadLocal属性的类(多线程下解决线程安全问题)
spring配置文件中, bean的class不设要new的类,而是设成工厂类,然后后面加factory-method属性
<bean id="DvdTypeDAOBean" class="com.machome.core.impl.DvdTypeDAOFactory" factory-method="getInstance" />
三.在spring中建立工厂类的Bean,然后调用该工厂bean建立真正的bean
public class DvdTypeDAOFactory {
public DvdTypeDAO getInstance(){
return new DvdTypeDAO();
}
}
spring配置文件中
<bean id="DvdTypeDAOFactoryBean" class="com.machome.core.impl.DvdTypeDAOFactory" />
这个工厂bean还是被spring的DefaultListableBeanFactory. preInstantiateSingletons创建
<bean id="DvdTypeDAOBean" factory-bean="DvdTypeDAOFactoryBean" factory-method="getInstance" />
相关文章推荐
- spring 实例化定义在xml配置文件中的bean的三种方法?
- 加载spring配置文件的常用三种方法
- 加载Spring配置文件常用的三种方法
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- MyEclipse添加xml文件自动提示方法,以spring配置文件为例
- spring事务配置方法(BeanNameAutoProxyCreator)
- 加载spring配置文件的常用三种方法
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置
- spring事务配置方法(TransactionProxyFactoryBean)
- spring中构造bean实例的三种方法
- 从spring配置文件中读取bean的方法
- Spring中实例化bean的三种方法
- spring2.5 实例化bean的三种方法
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- 添加xml文件自动提示方法,以spring配置文件为例
- 加载spring配置文件的常用三种方法
- Spring 三种实例化Bean方法
- Spring取出bean的方法和xml里的配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(五):使用@Component 来简化bean的配置