使用标签的缺点在于必需要有源代码(由于标签必须放在源代码上),当我们并没有程序源代码的时候。我们仅仅有使用xml进行配置。

比如我们在xml中配置某个类的属性

          

  1. <bean name="studentService"class="com.bjsxt.service.StudentService">
  2. <property name="studentDao"ref="stuDaoImpl"></property><!-- 普通值用value 对于特定的类的对象则用ref须要注意的是 ref须要在xml文件里进行配置-->
  3. </bean>

(1)Xml配置(加入了加下划线部分)

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config /><!-- 自己主动装配一定要加上此片段-->
<bean name="s"class="com.bjsxt.dao.impl.StudentDaoImpl"></bean>
<bean name="student"class="com.bjsxt.model.Student" scope="singleton" lazy-init="true"init-method="init" destroy-method="destroy"></bean>
<!-- prototype 每次生成的对象不同 prototype
lazy-init=false 默认在容器进行初始化的时候就初始化了该对象
-->
<bean name="studentService"class="com.bjsxt.service.StudentService"> </bean>
</beans>

(2)类 注意在这里的两种装配方式 第一种由于破坏了封装性不建议 spring容器在进行查找时。是依照xml进行查找

packagecom.bjsxt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; importcom.bjsxt.dao.*;
import com.bjsxt.dao.impl.*;
importcom.bjsxt.model.*;
public class StudentService {
/*@Autowired //依照类型进行匹配
@Qualifier("s")依照名称进行匹配 第一种表示方式放在属性名的前面
*/
privateStudentDao studentDao;
//减少了耦合性:StudentService并不知道详细由谁来保存学生s 由ioc容器来控制装配 publicStudentDao getStudentDao() {
return studentDao;
}
@Autowired//放到set方法上
public void setStudentDao( @Qualifier("s")StudentDao studentDao) {
this.studentDao = studentDao;
}
public void add(Student s)
{
this.studentDao.StudentSave(s);
}
}

最新文章

  1. 使用EF Oracle实现DevExpress绑定大数据的ServerMode模式
  2. maven File encoding has not been set
  3. Android中Activity、Service和线程之间的通信
  4. apache+tomcat整合
  5. Eclipse 环境安装和配置优化
  6. C++中的重载,隐藏,覆盖,虚函数,多态浅析
  7. 背包问题递归java
  8. .NET 创建并写CSV文件
  9. C#Redis字符串
  10. Postman 测试web接口(推荐)
  11. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A B题
  12. 你为什么还坚持.NET
  13. PageRank_网页排名_MapReduceJava代码实现思路
  14. c# winform打印excel(使用NPOI+Spire.xls+PrintDocument直接打印excel)
  15. 【c#】队列(Queue)和MSMQ(消息队列)的基础使用
  16. Oracle高级查询之over(partition by...)
  17. JavaSE-类
  18. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(八):MyBatis分页功能实现
  19. CSS样式----css样式表和选择器(图文详解)
  20. Load balancing 各组件的比较

热门文章

  1. iOS推送原理和证书生成简介
  2. react native windows 搭建(完整版)
  3. WPF中使用WPFMediaKit视频截图案例
  4. C# 连接和操作SQL SERVER数据库
  5. MySQL 一般模糊查询的几种用法
  6. mysql TIMESTAMPDIFF
  7. UVA12096 集合栈计算机(map和vector实现双射关系+集合的交并运算的STL)
  8. rsync用于同步目录
  9. thinkpad win8.1 无线连接受限
  10. [BZOJ 1305] 跳舞