您的位置:首页 > 编程语言 > Java开发

Spring 控制反转的几种方式

2012-03-08 22:21 190 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: