spring的bean管理(xml方式)

Bean实例化的方式

1.在spring里面通过配置文件创建对象

 

2.bean实例化三种方式实现

第一种 使用类的无参数构造创建(重点)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean id="user" class="cn.itcast.ioc.User"></bean>
</beans>

注意:类里面没有无参的构造会出现异常

第二种 使用静态工厂创建

(1)创建静态的方法,返回类对象

public class Bean2Factory {
	
	//静态的方法,返回Bean2对象
	public static Bean2 getBean2() {
		return new Bean2();		
	}
}
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

		
    <!-- 使用静态工厂创建对象 -->
    <bean id="bean2" class="cn.itcast.bean.Bean2Factory" factory-method="getBean2">                
    </bean>
</beans>

第三种 使用实例工厂创建

(1)创建不是静态的方法,返回类对象

public class Bean3Factory {
	
	//普通的方法,返回Bean3对象
	public Bean3 getBean3() {
		return new Bean3();
	}
}
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

		<!-- 使用静态工厂创建对象 -->
		<!-- 创建工厂对象 -->
		<bean id="bean3Factory" class="cn.itcast.bean.Bean3Factory"></bean>
		<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
</beans>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值