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

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

2018-12-07 21:14 288 查看

新建工程,导入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 -->
<!-- 配置Bean
class:是利用反射的原理,通过全类名方式在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>

此时的配置就相对简单!

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: