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

Spring学习笔记一

2016-03-04 00:00 537 查看
IOC容器里配置bean
applicationContext.xml
必须有个无参的构造器
class:bean的全类名
通过bean id获得这个bean必须唯一

spring提供2种IOC容器容器实现方式
beanfactory用在spring本身
applicationContext用在开发者
配置方式相同

ApplicationContext有2个主要的实现类
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext

ApplicationContext下面有
ConfigurableApplicationContext启动更新关闭上下文过程

ApplicationContext初始化上下文的时候,实例化所有单例的bean

WebApplicationContext用于web

ApplicationContext的getBean方法读取bean

用类型获取,容器内只能有一个
ctx.getBean(HelloWorld.class);

用id获取
ctx.getBean(helloworld);

注入方式
属性注入(最常用)
构造器注入
工厂方法注入

//编程
区分重载构造器

字面值可用字符串表示的值,通过value标签或者value属性注入,value子节点
//字面值表示特殊字符
<![CDATA[<beijing>]]>

//编程, property 的ref
bean之间的引用

//编程
内部bean不能被外部bean引用

//编程
null值和级联属性
<null/>
默认值就是null

//编程
级联属性
属性先初始化后才能赋值,否则异常

//编程List属性赋值
集合属性List,也能定义内部bean

//编程
集合属性Map,Map有k/v

//编程
Properties
HashTable子类
<props>
<prop>

//编程
集合配置
把集合的配置拿出来做成一个公用的bean
namespace: util.list

//编程
使用p命名空间
namespace: p
加入xmlns:p="http://www.springframework.org/schema/p"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" >

<bean name="helloworld" class="nocollection.HelloWorld">
<property name="name" value="yaocheng"></property>
</bean>

<bean id="car1" class="nocollection.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="dazhong" index="1"></constructor-arg>
<constructor-arg value="3000" type="double"></constructor-arg>
</bean>
<bean id="car2" class="nocollection.Car">
<constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
<constructor-arg type="java.lang.String">
<value>
<![CDATA[<beijing>]]>
</value>
</constructor-arg>
<constructor-arg type="int">
<value>100</value>
</constructor-arg>
</bean>
<bean id="person1" class="nocollection.Person">
<property name="name" value="Tom"/>
<property name="age" value="24"/>
<!--
<property name="car"><ref bean="car2"></ref></property>
-->
<property name="car">
<bean class="nocollection.Car">
<constructor-arg value="Ford"/>
<constructor-arg value="changan"/>
<constructor-arg value="200000" type="double"/>

</bean>
</property>
<property name="car.maxSpeed" value="100"/>

</bean>

<bean id="person2" class="nocollection.Person">
<constructor-arg value="Jerry"/>
<constructor-arg value="25"/>
<constructor-arg ref="car2"/>
</bean>
<bean id="person3" class="nocollection.Person">
<constructor-arg value="Jerry"/>
<constructor-arg value="25"/>
<constructor-arg ref="car1"/>
<property name="car.maxSpeed" value="50"/>
</bean>

<bean id="person4" class="collection.Person">
<property name="name" value="Tom"/>
<property name="age" value="26"/>
<property name="cars">
<list>
<ref bean="car1"/>
<ref bean="car2"/>
</list>
</property>
</bean>

<bean id="persona" class="collection.PersonMap">
<property name="name" value="rosy"/>
<property name="age" value="20"/>
<property name="cars">
<map>
<entry key="A" value-ref="car1"></entry>
<entry key="B" value-ref="car2"></entry>
</map>
</property>
</bean>

<bean id="datasource" class="collection.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="pwd">pwd</prop>
<prop key="jdbcUrl">jdbc:mysql://test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>

<util:list id="cars">
<ref bean="car1"></ref>
<ref bean="car2"></ref>
</util:list>

<bean id="person5" class="collection.Person">
<property name="name" value="yao"></property>
<property name="age" value="20"></property>
<property name="cars" ref="cars"></property>
</bean>

<bean id="person6" class="collection.Person" p:name="yao" p:age="20" p:cars-ref="cars">
</bean>

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