spring IOC

最近在看spring框架,这一篇是关于ioc的 ,目前感觉自己的项目不需要,还是先学习下,实现类很简单就不留记录了,只是将配置记录下。

set注入:

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--配置bean,配置后该类由spring管理-->
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<property name="springDao" ref="springDao1"></property>
<!--(1)依赖注入,配置当前类中相应的属性,
<property>标签中的name就是SpringAction类中的SpringDao属性名,ref指下面<bean name="springDao"...>,
这样其实是spring将SpringDaoImpl对象实例化并且调用SpringAction的setSpringDao方法将SpringDao注入:-->

</bean>
<bean name="springDao1" class="com.bless.impl.SpringDaoImpl"></bean>

</beans>




构造器注入:

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--配置bean,配置后该类由spring管理-->
<!--配置bean,配置后该类由spring管理-->
<bean name="springAction" class="cons.action.SpringAction">
<!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置-->
<constructor-arg index="1" ref="user"></constructor-arg>
<constructor-arg index="0" ref="springDao"></constructor-arg>
<!-- index="0"表示当构造函数是传入的多个参数,用来区分是第几个参数用的-->
</bean>
<bean name="springDao" class="cons.impl.SpringDaoImpl"></bean>
<bean name="user" class="cons.User"></bean>

</beans>


工厂注入:
[code="java"]
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--配置bean,配置后该类由spring管理-->
<!--配置bean,配置后该类由spring管理-->
<bean name="springAction" class="newFactory.SpringAction">
<!--(4)使用实例工厂的方法注入对象,对应下面的配置文件(4)-->
<property name="factoryDao" ref="f"></property>
</bean>

<!--(4)此处获取对象的方式是从工厂类中获取实例方法-->
<bean name="d" class="newFactory.Factory"></bean>
<bean name="f" factory-bean="d" factory-method="getFactoryDaoImpl"></bean>
<!-- 其实上面的2句可以由下面的带替换,主要还是先setFactory注入,然后在Factory里new 对象
<bean name="f" class="newFactory.FactoryDaoImpl"></bean>
-->
</beans>

[/code]


上面的工厂注入是实例动态注入,主要还是先setFactory注入,然后在Factory里new 对象
,还有一个是静态的,就是在申明的时候不是new 而是static申明的 ,另外工厂注入在我看来还是前面的2种方法合起来的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值