林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

本文主要讲解了spring中constructor注入的4种不同写法和sette的3种不同写法

一、constructor注入4种不同写法

通过构造方法注入,就相当于给构造方法的参数传值set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选的,构造注入的优势是通过构造强制依赖关系,不可能实例化不完全的或无法使用的bean。

第1种方法:直接传值

[html] view plain copy

print?
  1. <!-- constructor方式注入写法1,直接传值 -->
  2. <bean id="student1" class="com.mucfc.beanfactory.Student">
  3. <constructor-arg value="小明" />
  4. <constructor-arg value="2001" />
  5. </bean>
 <!-- constructor方式注入写法1,直接传值 --><bean id="student1" class="com.mucfc.beanfactory.Student"><constructor-arg value="小明" /><constructor-arg value="2001" /></bean>

直接给参数赋值,这种方法也是根据顺序排的,所以一旦调换位置的话,就会出现bug,这种方法已经很原始了

第2种方法:根据索引赋值,索引都是以0开头的:

[html] view plaincopy print?
  1. <!-- constructor方式注入写法2,根据索引赋值 -->
  2. <bean id="student2" class="com.mucfc.beanfactory.Student">
  3. <constructor-arg index="0" value="阿狗" />
  4. <constructor-arg index="1" value="2002" />
  5. </bean>
<!-- constructor方式注入写法2,根据索引赋值 --><bean id="student2" class="com.mucfc.beanfactory.Student"><constructor-arg index="0" value="阿狗" /><constructor-arg index="1" value="2002" /></bean>

第3种方法是根据所属类型传值

这种方法基本上不怎么适用,因为一个类里可以有好几个相同基本类型的变量,很容易就混淆值传给哪一个参数了所以做好不要使用这种方法:

[html] view plaincopy print?
  1. <!-- constructor方式注入写法3,根据所属类型传值 -->
  2. <bean id="student3" class="com.mucfc.beanfactory.Student">
  3. <constructor-arg type="String" value="大白" />
  4. <constructor-arg type="int" value="2003" />
  5. <strong>    </bean></strong>
第4种方法:根据参数的名字传值:(推荐用法)

[html] view plaincopy print?
  1. <!-- constructor方式注入写法4,根据参数的名字传值:记得传入名是构造器的参数名(推荐用法) -->
  2. <bean id="student4" class="com.mucfc.beanfactory.Student">
  3. <constructor-arg name="name" value="大地" />
  4. <constructor-arg name="id" value="4503" />
  5. </bean>

在这几种方法里我感觉这种方法是最实用的,他是根据名字来传值的,所以基本上只要名字对了,这个值就可以获取到 

二、setter注入3种不同写法

1、

[html] view plain copy print?

  1. <!-- setter方式注入写法1,记得要有无参构造函数 -->
  2. <bean id="student5" class="com.mucfc.beanfactory.Student">
  3. <property name="std_name">
  4. <value>天天</value>
  5. </property>
  6. <property name="std_id">
  7. <value>2005</value>
  8. </property>
  9. </bean>
  <!-- setter方式注入写法1,记得要有无参构造函数 --><bean id="student5" class="com.mucfc.beanfactory.Student"><property name="std_name"><value>天天</value></property><property name="std_id"><value>2005</value></property></bean>

2、

[html] view plaincopy print?
  1. <!-- setter方式注入写法2,记得要有无参构造函数 -->
  2. <bean id="student6" class="com.mucfc.beanfactory.Student">
  3. <property name="std_name" value="水水" />
  4. <property name="std_id" value="3009" />
  5. </bean>

3、

[html] view plain copy print?

  1. <!-- setter方式注入写法7,记得要有无参构造函数  -->
  2. <bean id="student7" class="com.mucfc.beanfactory.Student"
  3. p:std_name="针地" p:std_id="3445" />
<!-- setter方式注入写法7,记得要有无参构造函数  --><bean id="student7" class="com.mucfc.beanfactory.Student"p:std_name="针地" p:std_id="3445" />

推荐用第2种或第3种,第1种要写的代码比较多,看直来也没那么顺。

三、使用范例

新建一个Javaproject工程,名字自已取吧,然后把Spring的jar文件 和commons-logging的jar文件加载进来就行

不懂看这里【Spring】Spring配置及第个Spring HelloWorld

新建一个包,添加一个Student.Java,代码如下:

[java] view plaincopy print?
  1. /**
  2. *功能 测试constructor和setter注入的不同写法
  3. *作者 林炳文(ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
  4. *时间 2015.4.4
  5. */
  6. package com.mucfc.beanfactory;
  7. public class Student {
  8. private String std_name;
  9. private int std_id;
  10. public Student(){
  11. }
  12. public Student(String name,int id) {
  13. std_name=name;
  14. std_id=id;
  15. }
  16. public String getStd_name() {
  17. return std_name;
  18. }
  19. public void setStd_name(String std_name) {
  20. this.std_name = std_name;
  21. }
  22. public int getStd_id() {
  23. return std_id;
  24. }
  25. public void setStd_id(int std_id) {
  26. this.std_id = std_id;
  27. }
  28. public String toString(){
  29. return "学生姓名:"+std_name+" 学生学号:"+std_id;
  30. }
  31. }

在当前工程的src文件夹下添加文件ApplicationContext.xml,就是工程-》右键-》new->other->xml.......

代码如下:

[html] view plaincopy print?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- constructor方式注入写法1,直接传值 -->
  7. <bean id="student1" class="com.mucfc.beanfactory.Student">
  8. <constructor-arg value="小明" />
  9. <constructor-arg value="2001" />
  10. </bean>
  11. <!-- constructor方式注入写法2,根据索引赋值 -->
  12. <bean id="student2" class="com.mucfc.beanfactory.Student">
  13. <constructor-arg index="0" value="阿狗" />
  14. <constructor-arg index="1" value="2002" />
  15. </bean>
  16. <!-- constructor方式注入写法3,根据所属类型传值 -->
  17. <bean id="student3" class="com.mucfc.beanfactory.Student">
  18. <constructor-arg type="String" value="大白" />
  19. <constructor-arg type="int" value="2003" />
  20. </bean>
  21. <!-- constructor方式注入写法4,根据参数的名字传值:记得传入名是构造器的参数名(推荐用法) -->
  22. <bean id="student4" class="com.mucfc.beanfactory.Student">
  23. <constructor-arg name="name" value="大地" />
  24. <constructor-arg name="id" value="4503" />
  25. </bean>
  26. <!-- setter方式注入写法1,记得要有无参构造函数 -->
  27. <bean id="student5" class="com.mucfc.beanfactory.Student">
  28. <property name="std_name">
  29. <value>天天</value>
  30. </property>
  31. <property name="std_id">
  32. <value>2005</value>
  33. </property>
  34. </bean>
  35. <!-- setter方式注入写法2,记得要有无参构造函数 -->
  36. <bean id="student6" class="com.mucfc.beanfactory.Student">
  37. <property name="std_name" value="水水" />
  38. <property name="std_id" value="3009" />
  39. </bean>
  40. <!-- setter方式注入写法7,记得要有无参构造函数  -->
  41. <bean id="student7" class="com.mucfc.beanfactory.Student"
  42. p:std_name="针地" p:std_id="3445" />
  43. </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"><!-- constructor方式注入写法1,直接传值 --><bean id="student1" class="com.mucfc.beanfactory.Student"><constructor-arg value="小明" /><constructor-arg value="2001" /></bean><!-- constructor方式注入写法2,根据索引赋值 --><bean id="student2" class="com.mucfc.beanfactory.Student"><constructor-arg index="0" value="阿狗" /><constructor-arg index="1" value="2002" /></bean><!-- constructor方式注入写法3,根据所属类型传值 --><bean id="student3" class="com.mucfc.beanfactory.Student"><constructor-arg type="String" value="大白" /><constructor-arg type="int" value="2003" /></bean><!-- constructor方式注入写法4,根据参数的名字传值:记得传入名是构造器的参数名(推荐用法) --><bean id="student4" class="com.mucfc.beanfactory.Student"><constructor-arg name="name" value="大地" /><constructor-arg name="id" value="4503" /></bean><!-- setter方式注入写法1,记得要有无参构造函数 --><bean id="student5" class="com.mucfc.beanfactory.Student"><property name="std_name"><value>天天</value></property><property name="std_id"><value>2005</value></property></bean><!-- setter方式注入写法2,记得要有无参构造函数 --><bean id="student6" class="com.mucfc.beanfactory.Student"><property name="std_name" value="水水" /><property name="std_id" value="3009" /></bean><!-- setter方式注入写法7,记得要有无参构造函数  --><bean id="student7" class="com.mucfc.beanfactory.Student"p:std_name="针地" p:std_id="3445" />
</beans>

好了,Ioc容器都配置好了。下面就是来使用了啦,直接看代码:

[java] view plaincopy print?
  1. /**
  2. *功能 测试constructor和setter注入的不同写法
  3. *作者 林炳文(ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
  4. *时间 2015.4.4
  5. */
  6. package com.mucfc.beanfactory;
  7. import org.springframework.beans.factory.xml.XmlBeanFactory;
  8. import org.springframework.core.io.ClassPathResource;
  9. public class Test {
  10. public static void main(String[] args) {
  11. XmlBeanFactory bFactory = new XmlBeanFactory(new ClassPathResource("ApplicationContext.xml"));
  12. Student stu1 = (Student) bFactory.getBean("student1");
  13. Student stu2 = (Student) bFactory.getBean("student2");
  14. Student stu3 = (Student) bFactory.getBean("student3");
  15. Student stu4 = (Student) bFactory.getBean("student4");
  16. Student stu5 = (Student) bFactory.getBean("student5");
  17. Student stu6 = (Student) bFactory.getBean("student6");
  18. Student stu7 = (Student) bFactory.getBean("student7");
  19. System.out.println("-------------constructor方式注入写法-------------");
  20. System.out.println(stu1);
  21. System.out.println(stu2);
  22. System.out.println(stu3);
  23. System.out.println(stu4);
  24. System.out.println("-------------setter方式注入写法,记得要有无参构造函数-------------");
  25. System.out.println(stu5);
  26. System.out.println(stu6);
  27. System.out.println(stu7);
  28. }
  29. }

输出结果:

这就是最后的结果,是不是很简单?

林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

SSM搭建-Spring之bean的属性值XML注入方式(4)相关推荐

  1. Spring配置中的bean直接引用其它bean的属性值

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! prin ...

  2. Spring-引用Bean的属性值

    概述 实例 基于XML方式的引用 基于注解的引用 概述 将应用系统的配置信息存放在配置文件中并非总是最合适的,如果应用以集群的方式部署,或者希望在运行期动态调整引用的某些配置,这时,将配置信息放到数据 ...

  3. java getclass 相等_java使用反射比较两个bean对象属性值是否相等

    import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import ja ...

  4. java对象上有横线_对象bean间属性值复制:无视大小写和下划线_和横杠-

    1.简要说明:在java代码开发过程中,总会遇到实体类bean直接的属性复制问题,将一个bean中的值复制到另一个bean中,这时如果属性名是形同的,(仅仅有大小写或带下划线_或带横杠-)的区别,那就 ...

  5. Spring中Bean管理操作基于XML配置文件方法实现

    Spring中Bean管理操作基于XML配置文件方法实现 基于XML配置文件方式实现 1.基于`xml`方式创建对象 2.基于`xml`方式注入属性 1.创建类,定义属性和对应的set方法 2.在Sp ...

  6. Spring中Bean初始化和销毁的多种方式

    Spring中Bean初始化和销毁的多种方式 一.Bean的多种初始化方式 1.PostConstruct注解 2.实现InitializingBean接口 3.声明init-method方法 二.B ...

  7. 使用Spring获取JavaBean的属性值匹配短信模板

    2019独角兽企业重金招聘Python工程师标准>>> 在做电信的项目过程中,遇到根据可配置的短信模板去适配相应的短信的内容. Spring的源码提供了解析object对象的属性方法 ...

  8. spring实现属性值的注入

    首先创建一个实体类User @Data @Builder @NoArgsConstructor @AllArgsConstructor public class User {private Strin ...

  9. Spring Boot : Bean标签属性

    1.美图 2.概述 属性 解释 ① id属性 Bean的名称在IOC容器中必须是唯一的. ② class属性 指定Bean对应实现类的全类名,即包名加类名,必须有无参构造. ③ scope属性: 前面 ...

  10. List中bean某属性值转换为list

    List<类> lst = new ArrayList<>() ; lst.stream().map(类::get需要取得仠的属性名).collect(Collectors.t ...

最新文章

  1. CommunityServer研习心得(转)
  2. iOS WKWebView JS原生交互之JS调用OC(附demo)
  3. 三个获取浏览器URL中参数值的方法
  4. 对话行癫:解密阿里云顶层设计和底层逻辑
  5. redis 依赖_springboot|springboot集成redis缓存
  6. mysql登录报错 ERROR 1045 (28000)
  7. 如何拿到阿里算法校招offer
  8. 分级时间轮优化普通时间轮定时器
  9. 浙江万里学院计算机专业宿舍,2020年浙江万里学院宿舍条件环境照片 宿舍空调相关配置介绍...
  10. “the import java.io cannot be resolved”错误的解决方法
  11. Qt中QScrollArea控件区域与滑动条的颜色不一致设置
  12. 游戏及相关CG行业知识分享大V全整合
  13. JDBCDataSource
  14. 一文带你明白什么是浏览器插件?
  15. C++左值,右值例子理解
  16. gnss_伽利略gnss消息认证过程
  17. bootstrap fileupload插件实现文件上传与前端回显图片
  18. Java EXCEL 表格导入导出(带下拉选-带VLOOKUP函数封装)
  19. 第一章 回归模型分析
  20. JavaScript学习笔记 Day1

热门文章

  1. discuz 模板php,Discuz 模板语句分析及知识技巧
  2. ONLYOFFICE Docs如何与NEXTCLOUD 24连接集成
  3. 社交网络时代下的网络营销
  4. [ECCV2020]NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis
  5. itools电脑显示服务器维护,win10系统iTools无法打开且服务无法启动的具体技巧
  6. 广东科技学院计算机学院院长,陈强-广东科技学院-计算机学院
  7. 薪火相传,构建生态——记约束求解基础与应用训练营
  8. 关键词搜索-免费搜索关键词排名软件
  9. 一生从未打过败仗的“杀神”白起是怎么死的?
  10. 学到了林海峰,武沛齐讲的Day16完