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

spring学习笔记00 helloword-IOC&DI概述-配置bean-属性配置细节

2016-12-01 20:38 381 查看
注意:有些文字放在代码里,虽然看起来不是很舒服,是因为不放在代码段里面,有些如< > 这样的符号会被转义;

1.spring的helloword:

这里介绍一个新的eclipse插件:spring tool suit这个是用于spring的eclipse的插件,可以方
便关于spring的框架开发。
1).ioc(依赖注入):
注意:一共五个架包,一个两个java类,一个spring的bean的配置文件
----------------------------------------------------------------------------------------
bean的配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<!-- 配置bean -->
<bean id="helloword" class="com.spring.Helloword"> <!-- id标示,从IOC容器中获取bean实例会用到 -->
<!-- name对应set方法  value是赋的值-->
<property name="name" value="spring"></property>
</bean>
</beans>


java文件01:


package com.spring;
public class Helloword {
private String name;
public void setName(String name) {
System.out.println("set.......");
this.name = name;
}

public void hello(){
System.out.println("heillo:"+name);
}
//无参构造器
public Helloword(){
System.out.println("helloword.......con");
}
}


java文件02:
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] a) {
// TODO Auto-generated constructor stub
//1.创建spring的ioc容器
//注意:在new的时候,调用了bean相应类的无参构造方法,并且调用了set方法 传入了参数;
//具体了可以把最后两条语句注释掉;
ApplicationContext ctx =new  ClassPathXmlApplicationContext("spring.xml");
//2.从IOC容器中获取bean实例:
Helloword hello= (Helloword)ctx.getBean("helloword");
hello.hello();
}
}


2.IOC和DI

依赖注入的方式:属性注入;构造器注入;
IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为
回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一
种合适的方式来接受资源. 这种行为也被称为查找的被动形式;
DI(Dependency Injection) — IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容
器的资源注入. 相对于 IOC 而言,这种表述更直接;
Spring 4.x 新特性:泛型依赖注入;
bean 之间的关系:继承;依赖;
bean 的作用域:singleton;prototype;WEB 环境作用域;


3.配置bean:

(反射,无参构造器)
class的全类名,通过反射的方式在IOC的容器中创建bean,所以要求bean中必须有无参构造器;
id:表示容器中的bean(id是Bean 的名称):
在IOC容器中必须是唯一的;
若 id 没有指定,Spring 自动将权限定性类名作为 Bean 的名字;
id 可以指定多个名字,名字之间可用逗号、分号、或空格分隔;
IOC的实现(两种方式):Beanfactory(是spring框架的基础设施)和     applicationcontext(面向开发者),几乎所有的应用场
合都直接使用applicationcontext而非beanfactory;无论使用何种方式,配置文件是相同的;
applicationcontext有两个主要的实现类:
1.ClassPathXmlApplicationContext:从 类路径下加载配置文件;
2.FileSystemXmlApplicationContext: 从文件系统中加载配置文件;
ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 Applicat
ionContext 具有启动、刷新和关闭上下文的能力
ApplicationContext 在初始化上下文时就实例化所有单例的 Bean;
WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作;
spring 3种依赖注入的方式:1.属性注入;2.构造器注入;3.工厂方法注入(很少使用,不推荐使用);
属性注入:
1.属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象
2.属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性               或 <value> 子节点指定属性值
3.<属性注入是实际应用中最常用的注入方式>
配置文件:
<!-- 配置bean 通过属性注入-->
<!-- id标示  -->


<bean id="helloword" class="com.spring.Helloword">
<!-- name对应set方法  value是赋的值  调用类中的setter方法-->
<property name="name" value="spring"></property>
</bean>


构造器注入:
通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性
注意:使用构造器注入属性值可以指定参数的位置(index)和参数的类型(type);


<bean id="car" class="com.spring.Car">
<constructor-arg value="Auti" index="0"></constructor-
15d6c
arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>

<bean id="car2" class="com.spring.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>


java代码:

public static void main(String[] a) {
// TODO Auto-generated constructor stub
//1.创建spring的ioc容器
//注意:在new的时候,调用了bean相应类的无参构造方法,并且调用了set方法 传入了参数;
//具体了可以把最后1. 2. 3. 4. 两条语句注释掉;
ApplicationContext ctx =new  ClassPathXmlApplicationContext("spring.xml");
//2.从IOC容器中获取bean实例:
Helloword hello= (Helloword)ctx.getBean("helloword"); // 实例化   调用无参构造
hello.hello();
Car car=(Car)ctx.getBean("car");// 1.
System.out.println(car);            //2.
Car var=(Car)ctx.getBean("car2");  //3.
System.out.println(var);      //4.
}


解释看注解:

<bean id="person" class="com.spring.Person">
<property name="age" value="45"></property>
<property name="name" value="renchenhao"></property>
<!-- 非基本类型数据 引用型数据 可以用内部bean定义也行 -->
<!-- <property name="car" ref="car2"></property> -->
<!-- 内部bean 不能被外部引用 -->
<property name="car">
<bean class="com.spring.Car">
<constructor-arg value="ford"></constructor-arg>
<constructor-arg value="changha" type="java.lang.String"></constructor-arg>
<constructor-arg value="2000000" type="int"></constructor-arg>
</bean>
</property>
</bean>


注意:

1.使用构造函数注入的时候,可以不需要无参构造函数;

2.使用属性注入的时候,必须要有无参构造;

4.属性配置细节:

1.若字面值中包含特殊字符比如<>,可以使用 "<![CDATA[ 特殊字符  ]]>" 把字面值包裹起来。
2.属性值可以用value子节点配置:


<bean id="car2" class="com.spring.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
<constructor-arg type="int">
<value>267</value><!-- value字节点进行配置 -->
</constructor-arg>
</bean>


3.基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式
4.要使 Bean 能够相互访问,就必须在 Bean 配置文件中指定对 Bean 的引用,在 Bean 的配置文件中, 可以通过 <ref> 元素或者添加一个ref的子节点,其中属性为Bean或构造器参数指定对Bean的引用;


5.注入参数详解:null 值和级联属性:
1).null 值:
<bean id="person2" class="com.spring.Person">
<constructor-arg value="renchea"></constructor-arg>
<constructor-arg value="4000000"></constructor-arg>
<constructor-arg><null/></constructor-arg><!-- <null/> 是特有的写法,为null值必须这么写-->
</bean>


注意:在设置value的值得时候:如果始终报如下异常:

Error creating bean with name 'person2' defined in class path resource [spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)


那就是你的value值的类型和构造方法的参数类型对不上等这一系列的问题;

2).级联属性赋值:
<bean id="person3" class="com.spring.Person">
<constructor-arg value="22222"></constructor-arg>
<constructor-arg value="121325"></constructor-arg>
<constructor-arg ref="car"></constructor-arg>
<!--  为级联属性赋值,注意:属性需要先初始化后才可以为级联属性赋值.否则会有异常,和struts2不同,struts2可以自动生成,很多时候我们用不着为级联属性赋值,还有因为下面用了property所以在Car中必须要有maxSpeed的setter方法; -->
<property name="car.maxSpeed" value="5000"></property>
</bean>


6.集合属性:

1).List集合:

配置 java.util.List 类型的属性, 需要指定 <list>  标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.


//在pojo Person类中的数据类型是这样的,集合的数据类型;
private List<Car> cars;


<bean id="person2" class="com.spring.Person">
<property name="age" value="45"></property>
<property name="name" value="renchenhao"></property>
<property name="cars">
<list>
<ref bean="car"/><!-- 通过 <ref> 指定对其他 Bean 的引用 -->
<bean class="com.spring.Car"> <!-- 通过<bean> 指定内置 Bean 定义 -->
<constructor-arg value="ford"></constructor-arg>
<constructor-arg value="changh" type="java.lang.String"></constructor-arg>
<constructor-arg value="9000000" type="int"></constructor-arg>
</bean>
<null/> <!-- 通过null指定空元素 -->
</list>
</property>
</bean>


==========================================================

注意(遇到的问题):

-- 在pojo类中如有如下属性():
private List<Car> cars;
private Car car;
在spring的配置文件bean节点中不知道为什么只要是以属性注入(构造器方法没试过)的方式给car属性赋值的时候,一运行都会变成cars属性;


以下是两种bean配置方法不同,效果一样:


<!-- 使用内部bean的方法: -->>
<bean id="person" class="com.spring.Person">
<property name="age" value="40"></property>
<property name="name" value="renchenhao"></property>
<property name="car">
<bean  class="com.spring.Car">
<constructor-arg value="ford"></constructor-arg>
<constructor-arg value="changha" type="java.lang.String"></constructor-arg>
<constructor-arg value="2000000" type="int"></constructor-arg>
</bean>
</property>
</bean>


OR

<!-- 以下是引用的方法: -->
<bean id="person" class="com.spring.Person">
<property name="age" value="40"></property>
<property name="name" value="renchenhao"></property>
<property name="car" ref="car2"></property>
</bean>


以上两种bean配置方法都是输出同样的结果;

pojo类的源码:

package com.spring;
import java.util.List;

import com.spring.*;
public class Person {
private String name;
private int age;
private List<Car> cars;
private Car car;

public Person() {
super();
}
public Person(String name, int age, List<Car> cars) {
super();
this.name = name;
this.age = age;
this.cars = cars;
}

public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}

}


控制台输出结果: Person [name=renchenhao, age=40, cars=null]

疑惑:为什么明明属性名指定为car还是在给cars赋值,并且为空,希望日后能解决,也希望看这篇博客的大神们能帮我解惑,十分感谢;

===========================================================

2). 数组的定义和 List 一样, 都使用 <list>配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样,这里就不贴代码了;


3).Map集合:

(Map集合一样可以配置内部bean,具体的方法参照前面的方法;)

-Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值;
-必须在 <key> 标签里定义键,因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素;
-可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义;


<bean id="person2" class="com.spring.Person">
<property name="age" value="45"></property>
<property name="name" value="renchenhao"></property>
<property name="cars">
<map>               <!-- value-ref的属性用的是bean的id  -->
<entry key="SS" value-ref="car2"></entry>
<entry key="AA" value-ref="car"></entry>
</map>
</property>
</bean>


还有一种形式:

注意:entry节点的value属性的值为String类型,这样设置的前提是,Map的泛型为Map<String,String>或者Map不指定泛型;


<bean id="person2" class="com.spring.Person">
<property name="age" value="45"></property>
<property name="name" value="renchenhao"></property>
<property name="cars">
<map>
<entry key="SS" value="car2"></entry>
<entry>
<key><value>car-KEY</value></key><!-- 这是key -->
<value>car-value</value><!-- 这是值 -->
</entry>
</map>
</property>
</bean>


控制台输出结果:

Person [name=renchenhao, age=45, cars={SS=Car [brand=Baoma, corp=Shanghai, price=0.0, maxSpeed=267], car-KEY=car-value}]

4). java.util.Properties

使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性,在key属性中写入属性值.


//java代码:
package com.spring;

import java.util.Properties;

public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public DataSource() {
super();
}
@Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}

}


<!-- bean配置:  -->
<bean id="datasource" class="com.spring.DataSource">
<property name="properties">
<props>
<prop key="user">user</prop>
<prop key="password">password</prop>
<prop key="url">localhst:8080/test</prop>
<prop key="driverClass">com.jdbc.driver</prop>
</props>
</property>
</bean>


控制台输出结果:

DataSource [properties={driverClass=com.jdbc.driver, user=user, url=localhst:8080/test, password=password}]

5).使用 utility scheme 定义集合:

—使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合;

—可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 根元素里添加 util schema 定义(就是必须要添加util的命名空间);

<!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util的命名空间 -->
<util:list id="cars">
<ref bean="car"></ref>
<ref bean="car2"></ref>
</util:list>
<bean id="person3" class="com.spring.Person">
<property name="name" value="jack"></property>
<property name="age" value="49"></property>
<property name="cars" ref="cars"></property>
</bean>


6).使用 p 命名空间使用p命名空间的前提是必须导入p命名空间:

<!-- 通过p的命名空间为bean的属性赋值! 相对与传统的配置方式更加简介!  -->
<bean id="person5" class="com.spring.Person" p:age="30" p:name="renchenlkja" p:cars-ref="cars"></bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring 框架 学习笔记
相关文章推荐