新建工程,导入jar,添加spring配置文件(配置文件xxxx.xml)!

1.Helloword实现

Helloword.java

public class HelloWord {private String name;public void hello(){System.out.println("Hello:" + name);}public String getName() {return name;}public void setName(String name) {System.out.println("setname...");this.name = name;}public HelloWord() {System.out.println("con..");}
}

applicationContext.xml

<!-- HelloWord --><!-- 配置Beanclass:是利用反射的原理,通过全类名方式在IOC容器中创建Bean,所以要求Bean必须有无参的构造器id:标识容器中的Bean,id的值唯一--><!-- 属性注入 -->
<bean id="helloword" class="com.MrChengs1.HelloSpring.HelloWord"><property name="name" value="spring"></property>
</bean>

测试

//1创建Spring的IOC容器对象
//ApplicationContext代表IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//注意:此时setter方法和无参的构造器已经被调用了

//2.IOC容器获得Bean实列
//getBean获得的是配置文件的 id的值
//利用id定位到容器中的Bean
HelloWord helloWord = (HelloWord) ctx.getBean("helloword");

//利用类型得到IOC中的Bean
//该方法不需要进行强制转换,但是如果我们有两个class类在Helloword的话,即在配置文件中,实例化两个HelloWord类,此时不知道返回哪一个类
//HelloWord helloWord = ctx.getBean(HelloWord.class);

//3.调用hello方法
helloWord.hello();

con..
setname...
Hello:spring

SpringIoc:IOC(控制反转)

是一个比较抽象的概念,是spring框架的核心,用来消减计算机程序的耦合问题。

对象的实例不在由调用者进行创建,交给spring容器来创建。

spring容器负责控制程序之间的关系,而不是直接由调用者的程序代码直接控制。

这样控制权由调用者转到spring容器,控制权发生了翻转-----spring的控制反转

Dependency Injection:DI(依赖注入)

是IOC的另一个说法,从不同的角度描述相同的概念。

spring容器负责将依赖的对象赋给调用者的成员变量,相当于为调用者注入他所依赖的实例。

Spring容器:
在Spring容器读取Bean配置Bean之后创建Bean实例之前,必须对它进行实例化,只有在
容器实例化之后,才可以从IOC容器里获取Bean实例化并使用。
Spring提供了 两种类型的IOC容器实现
->BeanFactory,IOC容器实现
->ApplicationContext提供了更多的高级特性是BeanFactory的子接口
->BeanFactory是spring框架的基础实施,面向Spring对象本身
ApplicationContext面向使用Spring框架的开发者,几乎使用所有的应用场合
都直接使用ApplicationContext面非底层的BeanFactory
->无论使用何种方式,配置文件是相同 的

ApplicationContext:
主要实现类:
ClassPathXmlApplicationContext:从类路径下加载文件
FileSystemXmlApplicationContext:从文件系统中加载配置文件
ConfigurableApplicationContext扩展于ApplicationContext新增加两个方法具有refersh()和close(),让AoolicationContext具有启动和刷新的,关闭的能力
ApplicationContext在初始化上下文的时候就实例化所有的单例的Bean
WebApplicationContext是专门为WEB应用而准备的,它允许从相对于WEB根目录的路径中完成初始化准备工作

2.属性注入

属性注入就是通过setter方法进行Bean的实行值或依赖的对象
属性注入后使用<property>元素,使用name的属性名称,value属性或子节点进行指定属性值
属性注入是最长使用的注入!
属性注入必须要有无参的构造方法!!!!!!!!!!!!

利用java Bean规范所定义的setter方法来完成注入,灵活性且可读性高。

反射机制实现的。

<!-- 属性注入 -->
<bean id="helloword" class="com.MrChengs1.HelloSpring.HelloWord"><property name="name" value="spring"></property>
</bean>

3.构造器注入

由java反射机制,通过构造方法完成相关的依赖注入。

通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实力话之后就可以使用
构造器注入在<constructer-age>元素里面声明属性,<constructer-age>中没有name属性

car.java

public class car {private String brand;private String corp;private double price;private int maxSpeech;public car(String brand, String corp, double price) {super();this.brand = brand;this.corp = corp;this.price = price;}public car(String brand, String corp, int maxSpeech) {super();this.brand = brand;this.corp = corp;this.maxSpeech = maxSpeech;}
public car() {super();// TODO Auto-generated constructor stub
    }public car(String brand, String corp, double price, int maxSpeech) {super();this.brand = brand;this.corp = corp;this.price = price;this.maxSpeech = maxSpeech;}  //setter.....
}

applicationContext.xml

使用<constructor-arg value="..." index="..."></constructor-arg>

constructor-arg:元素用于定义构造器方法的参数

index:用于指定参数的位置,默认从零开始

value:对属性进行赋值

<!-- 构造器注入                       使用构造器方法可以指定参数的位置和类型,以区分重载构造器-->
<bean id="car" class="com.MrChengs1.HelloSpring.car"><constructor-arg value="LeiNuo" index="0"></constructor-arg><constructor-arg value="shanghai" index="1"></constructor-arg><constructor-arg value="300" index="2"></constructor-arg>
</bean>

测试:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

car car = (com.MrChengs1.HelloSpring.car) ctx.getBean("car");
System.out.println(car.toString());

car [brand=LeiNuo, corp=shanghai, price=0.0, maxSpeech=300]

注意:仅使用index有时候也是无法区别要使用哪一个构造器的此时可以配合使用type进行区别

<bean id="car1" class="com.MrChengs.HelloSpring.car"><constructor-arg value="AoDI" index="0"></constructor-arg><constructor-arg value="shanghai" index="1"></constructor-arg><constructor-arg value="240" index="2" type="int"></constructor-arg></bean> 

这样完全可以区分开使用那个构造器进行使用
使用构造器方法可以指定参数的位置和类型,以区分重载构造器

4.CDATA的使用

CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data)

即把所有的特殊字符都可以转为纯文本的方式进行输出。

语法:<![CDATA[  字符  ]]>

字面值:可以使用字符串表示的值,可以通过<value>元素标签或者value属性值进行注入
  基本数据类型及其封装类,String等类型都可以采用字面值的方式进行注入
  若字面值中包含特殊的字符串,可以使用 <![CDATA[]]>把字面值包起来
  <constructor-arg index="1"><value><![CDATA[<shanghai>]]></value></constructor-arg>
  注意CDATA不能放在value属性值里面进行赋值,必须放在value的标签里面<value>

<bean id="car1" class="com.MrChengs1.HelloSpring.car"><constructor-arg value="AoDI" index="0"></constructor-arg><constructor-arg  index="1"><value><![CDATA[<shanghai>]]></value></constructor-arg><constructor-arg  index="2" type="int"><value>245</value></constructor-arg>
</bean>

测试:

       car car1 = (com.MrChengs1.HelloSpring.car) ctx.getBean("car1");System.out.println(car1.toString());

car [brand=AoDI, corp=<shanghai>, price=0.0, maxSpeech=245]

此时可以看到"<>"也进行纯文本的输出!

5.引入其他的bean/内部bean

内部Bean:就是在bean标签里面的bean,内部Bean不可以被外部bean引用

注:内部bean不需要进行写id,内部bean外部的bean不能引用

ref:引用其他的bean

Person.java

public class Person {private String name;private int age;private car car;public Person(String name, int age, com.MrChengs1.HelloSpring.car car) {super();this.name = name;this.age = age;this.car = car;}public Person() {super();
    }//setter....
}

1).使用属性注入

applicationContext.xml

<!-- 引用其他的bean    属性注入-->
<bean id='person' class="com.MrChengs1.HelloSpring.Person"><property name="name" value="Tom"></property><property name="age" value="18"></property>  <!--引用其他bean --><!-- <property name="car"><ref bean="car"/></property> --><!-- <property name="car" ref="car1"></property> --> <!-- 内部bean --><property name="car"><bean class="com.MrChengs1.HelloSpring.car"><constructor-arg value="Ford" index="0"></constructor-arg><constructor-arg value="beijing" index="1"></constructor-arg><constructor-arg value="100000" index="2"></constructor-arg></bean></property>
</bean>

测试:

       Person p = (Person) ctx.getBean("person");System.out.println(p);

Person [name=Tom, age=18, car=car [brand=Ford, corp=beijing, price=0.0, maxSpeech=100000]]

2).构造器注入

applicationContext.xml

<!-- 引用其他的bean    构造器注入-->
<bean id="person1"  class="com.MrChengs1.HelloSpring.Person" ><constructor-arg value="Jerry"></constructor-arg><constructor-arg value="34"></constructor-arg><constructor-arg ref="car"></constructor-arg>
</bean>

测试:

       Person p1 = (Person) ctx.getBean("person1");System.out.println(p1);

Person [name=Jerry, age=34, car=car [brand=LeiNuo, corp=shanghai, price=0.0, maxSpeech=120]]

6.null和级联属性

可以使用专用的 <null/>元素标签为Bean的字符串或其他的字符串注入null
和Strus,Hilberante等框架一样Spring支持级联属性
属性需要先初始化,后才可以为其级联属性赋值

applicationContext.xml

<!-- 测试null 和 级联属性 -->
<bean id="person2"  class="com.MrChengs1.HelloSpring.Person" ><constructor-arg value="Bob"></constructor-arg><constructor-arg value="32"></constructor-arg><!-- null --> <!-- <constructor-arg ><null/></constructor-arg> --><!-- 级联   此时没有maxSpeech的值--><!-- 属性需要先初始化,后才可以为其级联属性赋值 --><constructor-arg ref="car"></constructor-arg><property name="car.maxSpeech" value="120"></property>
</bean>

测试:

       Person p2 = (Person) ctx.getBean("person2");System.out.println(p2);

Person [name=Bob, age=32, car=car [brand=LeiNuo, corp=shanghai, price=0.0, maxSpeech=120]]

7.p命名空间

导入命名空间

    xmlns:p="http://www.springframework.org/schema/p"

applicationContext.xml

<!-- P命名空间  先导入命名空间 -->
<bean id="person6" class="com.MrChengs2.Collection.Person" p:name="MrCheng" p:age="22"p:cars-ref="cars"></bean>
</beans>

此时的配置就相对简单!

起作用直接对属性进行赋值!

转载于:https://www.cnblogs.com/Mrchengs/p/10085286.html

1.spring:helloword/注入/CDATA使用/其他Bean/null级联/p命名空间相关推荐

  1. spring如何注入作用域不同的bean

    Spring IoC容器不仅管理对象(bean)的实例化,而且还管理协作者(或依赖项)的连接. 如果要将(例如)HTTP请求范围的Bean注入(例如)另一个作用域更长的Bean,则可以选择注入AOP代 ...

  2. spring学习---IOC--基于xml--bean管理--spring创建对象--spring注入属性--其他属性注入--外部bean--内部bean

    基于xml配置以下 bean管理 实现两种操作 a).spring创建对象 1.基于XML方式创建对象: <!-- 配置User对象的创建--><bean id = "us ...

  3. Spring框架XML配置文件使用外部Bean属性注入

    Spring框架XML配置文件使用外部Bean属性注入 (1)创建两个类service类和dao类 (2)在service中调用dao里面的方法 (3)使用Spring框架进行调用 (4)创建测试类 ...

  4. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件...

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  5. Spring依赖注入Bean为空,注入失效场景

    场景介绍 使用spring往一个bean(BeanB)注入另一个bean(BeanA),发现BeanA为null,注入失败了. 代码展示 /*** @author huangd* @date 2021 ...

  6. Spring属性注入的三种方式(超详细)

    属性注入的三种方式 使用set方法进行注入 使用有参构造函数进行注入 使用p名称空间注入 首先了解下面两个名词的含义: IOC:控制反转(Inversion of Control,缩写为IoC),是面 ...

  7. Spring framework(4):IoC (2) Bean 装配

    Spring 装配 Bean 概述 Spring 容器启动的3个条件: Spring 本身:Spring 框架的类包都已经放置在应用程序的类路径下: Bean 配置信息:应用程序为 Spring 提供 ...

  8. Spring学习系列(二) 自动化装配Bean

    一.Spring装配-自动化装配 @Component和@ComponentScan 通过spring注解(@Component)来表明该类会作为组件类,并告知Spring要为这类创建bean,不过组 ...

  9. 最全的 Spring 依赖注入方式,你都会了吗?

    欢迎关注方志朋的博客,回复"666"获面试宝典 前言 Spring 正如其名字,给开发者带来了春天,Spring 是为解决企业级应用开发的复杂性而设计的一款框架,其设计理念就是:简 ...

最新文章

  1. spring中那些让你爱不释手的代码技巧
  2. java 选择 颜色的控件_JavaFX颜色选择器(ColorPicker)
  3. 【JavaWeb】数据库基础复习
  4. 如何在Mac计算机上轻松查找和删除类似照片
  5. Struts2中UI标签之非表单标签
  6. 【Android XMPP】 学习资料收集贴(持续更新)
  7. RAID磁盘阵列配置和调优小结
  8. Python:遍历指定目录下所有的c语言源代码文件
  9. MSRA-TD500数据集(MSRA Text Detection 500 Database)
  10. XenApp/XenDesktop 7.12新功能LHC解读
  11. JAVA实现的吸血鬼数字算法,高效率版本(已有网友给出算法说明)
  12. 拒绝访问,文件或目录损坏且无法读取解决办法
  13. 2020我们一起“只争朝夕,不负韶华”
  14. 实操:将C盘用户配置文件移动到非系统盘(windows10系统)
  15. 有什么软件做笔记比较好用?
  16. opencv——批量处理图片并保存
  17. 什么样的台灯灯光是好的?推荐中性色温的护眼台灯
  18. 打印300页的书本胶装需要多少钱
  19. 解决富文本编辑器wangeditor 光标跳动的问题
  20. 火车硬座车厢座位分布表

热门文章

  1. linux特殊符号大全
  2. 用树状数组解决求区间最值的问题:hdu1754
  3. 专业嵌入式软件开发——全面走向高质高效编程(含DVD光盘1张)
  4. HDU1029 - Ignatius and the Princess IV【水题】
  5. bzoj1624:[Usaco2008 Open] Clear And Present Danger 寻宝之路
  6. ORACLE数据库之PL/SQL触发器、rownum、动态SQL、数据库之视图与索引
  7. Android HttpClient post MultipartEntity - Android 上传文件
  8. PHP ----MySQL 数据库
  9. 关于python中文处理
  10. python asyncio回调函数_最近用 Python 的 asyncio,有好多不懂。。