三种实例化bean的方法

情形如下:我们有一个PersonDaoImpl类,需要交给spring容器进行管理

PersonDao.java

package com.chris.dao;

public interface PersonDao {

	public abstract void save();

}

PersonDaoImpl.java

package com.chris.dao.impl;

import com.chris.dao.PersonDao;

public class PersonDaoImpl implements PersonDao {

	/* (non-Javadoc)
	 * @see com.chris.dao.PersonDao#save()
	 */
	@Override
	public void save(){
		System.out.println("save....");
	}
}

 

方法一:通过类默认构造器进行实例化

     在spring配置文件beans.xml中配置如下代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	....
	<bean name="personDaoBean" class="com.chris.dao.impl.PersonDaoImpl"/>
                      ....
</beans>

     注意:通过这种方法将类交给Spring容器管理,在容器实例化该对象的时候使用的是类默认构造方法.所以在类中必须提供默认的构造方法,否则容器不能对该类进行实例化。

 

 

方法二:通过静态工厂方法进行实例化

静态工厂类PersonDaoFactory.java

package com.chris.factory;

import com.chris.dao.PersonDao;
import com.chris.dao.impl.PersonDaoImpl;

public class PersonDaoFactory {

	//静态工厂方法
	public static PersonDao getInstence(){
		return new PersonDaoImpl();
	}
}

 在spring配置文件beans.xml中配置如下代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	....
	<bean name="personDaoBean" class="com.chris.factory.PersonDaoFactory" factory-method="getInstence"/> 
	<!--class属性为静态工厂类 factory-method为静态工厂方法-->
                      ....
</beans>

 方法三:通过实例工厂进行实例化

实例工厂类PersonDaoFactory.java

package com.chris.factory;

import com.chris.dao.PersonDao;
import com.chris.dao.impl.PersonDaoImpl;

public class PersonDaoFactory {

	public PersonDao getInstence(){
		return new PersonDaoImpl();
	}
}

 

 在spring配置文件beans.xml中配置如下代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<!--首先配置实例工厂,将实例工厂交给容器管理 -->
	<bean name = "personDaoFacotry" class = "com.chris.factory.PersonDaoFactory"/>
	<!--factory-bean中填写实例工厂在容器中的名称   factory-method为工厂中获取对象实例方法-->
	<bean name = "personDaoBean" factory-bean="personDaoFacotry" factory-method="getInstence"/>

</beans>

  

经过上述三种方法任选一种进行配置后,我们就可以从spring容器中获取我们需要的PersonDaoImpl对象了

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
PersonDao personDao = (PersonDao) applicationContext.getBean("personDaoBean");
personDao.save();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值