Spring 控制反转的几种方式

来源:互联网 发布:自己怎么注册淘宝店铺 编辑:程序博客网 时间:2024/05/13 04:06
 1. 属性注入

<?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 id="PersonDaoBean" class="_1.dao.impl.PersonDaoBean"></bean>
<bean id="PersonServiceBean" class="_1.service.impl.PersonServiceBean">
<property name="name" value="kenqq"></property>
<property name="age" value="22"></property>
<property name="personDao" ref="PersonDaoBean"></property>
</bean>
</beans>

2.构造器注入 (略

3.注释注入

...

xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="PersonDaoBean" class="_1.dao.impl.PersonDaoBean"></bean>
<bean id="PersonServiceBean" class="_1.service.impl.PersonServiceBean">
<property name="name" value="kenqq"></property>
<property name="age" value="22"></property>
<!-- <property name="personDao" ref="PersonDaoBean"></property>-->
</bean>
</beans>

在被控制类的接口 setter上面插入: @Resource

4.自动扫描注入

在dao类上:@Repository

Server类上:@Service("p")  (这个就是bean 名

当然 控制类的接口 setter上面插入: @Resource

原创粉丝点击