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
<?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
相关文章推荐
- Spring2.5学习笔记1-控制反转-DI容器配置元数据方式
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- Spring实现控制反转(IOC)的三种方式(二)——构造器注入
- Spring实现IOC(控制反转)的三种方式
- Spring 使用注解的方式实现IOC和DI(控制反转和依赖注入)
- Spring学习之路第一步 : xml方式实现IOC(控制反转)
- spring IOC(控制反转)和DI(依赖注入)以及三种依赖注入方式的比较
- Spring实现控制反转(IOC)的三种方式(一)——属性注入
- Spring总结——控制反转,注入(配置和注解两种方式)
- Spring实现控制反转(IOC)的三种方式(三)——自动装配
- spring四种依赖注入方式 ( 依赖注入DI+ 控制反转IOC的原理)
- spring——控制反转简单例子 (Spring三种注入方式)
- Spring实现控制反转(IOC)的三种方式(零)——理解IOC
- Spring的控制反转的三种方式
- Spring配置文件 在web.xml的几种配置方式
- Spring 配置 事务的几种方式
- spring学习笔记03--依赖注入与控制反转
- Spring通过@Value注解注入属性的几种方式
- Spring —— IOC控制反转
- Spring 常用的几种依赖注入方式