Spring---学习第二天

it2026-03-02  4

-------------------------Spring–属性配置细节----------------------------------------- 字面值:可用字符串表示的值,可以通过元素标签value属性进行注入 基本数据类型及其封装类,String等类型都可以采取字面值注入的方式。 若字面值中包含特殊字符,可以使用<!CDATD[]>把字面值包裹起来。

**引用其他的Bean引用**

组成应用程序的Bean经常需要相互协作以完成应用程序的功能,要使用Bean能够相互访问,就必须在Bean配置文件中指定Bean的引用。在Bean的配置文件中,可以通过元素或者ref属性为Bean的属性或构造器参数指定对Bean的引用。 <bean id="person" class="org.example.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <!--可以使用property 的ref属性建立bean之间的关系--> <property name="car" ref="car2"></property> </bean>

也可以在属性或构造器里面包含Bean的声明,这样的Bean称为内部Bean。 不能被外部使用,只能内部引用。

<bean id="person" class="org.example.Person"> <property name="name" value="Tom"></property> <property name="age" value="24"></property> <!-- &lt;!&ndash;可以使用property 的ref属性建立bean之间的关系&ndash;&gt; <property name="car" ref="car2"></property>--> <!--内部Bean--> <property name="car"> <bean class="org.example.Car"> <constructor-arg value="fute"></constructor-arg> <constructor-arg value="changan"></constructor-arg> <constructor-arg value="20" type="double"></constructor-arg> </bean> </property> </bean>

注入参数讲解:null值和级联属性。 可以使用专用的元素标签为Bean的字符串或其他对象类型的属性注入null值。

<!--测试赋值null --> <constructor-arg><null/></constructor-arg>

和Struts,Hiberante 等框架一样,Spring支持级联属性的配置。可以直接在

<!--测试级联属性--> <constructor-arg ref="car"></constructor-arg> <property name="car.maxSpeed" value="250"></property>

**集合属性:**

在Spring中可以通过一组内置的xml标签(例如:,或)来配置集合属性。 配置java.util.List类型的属性,需要指定标签,在标签里包含一些元素,这些标签可以通过指定简单的常量值,通过指定对其他Bean的引用,通过指定内置Bean定义,通过指定空元素,甚至可以在内嵌其他集合。 数组的定义和list一样,都是用<list> 配置java.util.Set需要使用<set>标签,定义元素的方法与List一样。 <!--测试如何配置集合属性--> <!--使用list节点,为list的属性赋值--> <bean class="org.example.beansCollection.Person" id="person3"> <property name="name" value="Mike"></property> <property name="age" value="27"></property> <property name="cars"> <list> <ref bean="car3"/> <ref bean="car4"/> </list> </property> </bean>

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

<!--配置Map属性值,节点map 的entry 子节点配置map类型的成员变量--> <bean id="person4" class="org.example.beansCollection.Person1"> <property name="name" value="Rose"></property> <property name="age" value="28"></property> <property name="cars"> <map> <entry key="AA" value-ref="car3"></entry> <entry key="BB" value-ref="car4"></entry> </map> </property> </bean>

使用<props>定义java.util.Properties,该标签使用多个<prop> 做为子标签,每个<prop>标签必须定义key属性。

<!--配置Properties属性值: 使用prop子节点来为Properties属性赋值--> <bean id="datasource" class="org.example.beansCollection.DataSource"> <property name="properties"> <props> <prop key="user">root</prop> <prop key="password">123456</prop> <prop key="jdbcUrl">jabc:mysql:///test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> 使用utility scheme 定义集合 使用基本的集合标签定义集合时,不能将集合作为独立的Bean定义,导致其他的Bean无法引用该集合,所以无法在不同Bean之间共享。 可以使用util schema里面的集合标签定义独立的集合Bean,需要注意的是,必须在<beans>根元素里 添加util schema定义。 <!--配置单例的集合Bean,以供多个bean 进行引用--> <util:list id="cars"> <ref bean="car3"/> <ref bean="car4"/> </util:list> <bean id="person5" class="org.example.beansCollection.Person"> <property name="name" value="jack"></property> <property name="age" value="12"></property> <property name="cars" ref="cars"></property> </bean> 使用P命名空间 为了简化xml文件的配置,越来越多的xml文件采用属性而非配置星系。 Spring 从2.5版本开始引入一个新的P命名空间,可以通过<bean>元素属性的方式配置Bean属性 使用P命名空间后,基于xml的配置方式将进一步简化。 !--通过p命名空间为bean的属性赋值,相对于传统的配置方式更加的简洁。--> <bean id="person6" class="org.example.beansCollection.Person" p:age="30" p:name="queen" p:cars-ref="cars"></bean>

------------------------------Spring–自动装配------------------------------------------

Spring IOC容器可以自动装配Bean,需要做的仅仅是在的autowire属性里指定自动装配的模式。 byType(根据类型自动装配):若IOC容器里有多个与目标Bean类型一样致的Bean,在这种情况下,Spring将无法判定那个Bean最合适该属性,所以不能执行自动装配。 byName(根据名称自动装配):必须将目标Bean的名称和属性名设置的完全相同。 constructor(通过构造器自动装配):当Bean中存在多个构造器时,此种自动装配方式会很复杂。不推荐使用。 <!--可以使用 autowire属性自动装配的方式,ByName根据Bean的名字和当前Bean的setter风格的属性名进行 自动装配,若有匹配的则自动装配,没有匹配的不装配。--> <bean id="person7" class="org.example.autowire.Person" p:name="TOM" autowire="byName"></bean> 自动装配的缺点:在Bean配置文件里设置autowrie属性进行自动装配捋会装配Bean的所有属性,然而,若只希望装配个别的属性时,autowrie属性就不灵活了。autowrie属性要么根据类型自动装配,要么根据名称自动装配,不能两者兼得。 一般情况下,在实际项目中很少用到自动装配功能,因为和自动装配功能所带来的好处来说,明确清晰的配置文档更有说服力一些。 ---------------------------Spring–bean之间的关系------------------------------bean之间的关系:继承;依赖 <bean id="address" class="org.example.autowire.Address" p:city="datong" p:street="wudaokou" ></bean> <!--bean-配置继承:使用bean的Parent属性--> <bean id="address1" class="org.example.autowire.Address" p:street="dazhongsi" parent="address"></bean> Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父Bean的Bean称为子Bean子Bean从父Bean中继承配置,包括Bean的属性配置子Bean也可以覆盖从父Bean继承过来的配置。父Bean可以作为配置模板,也可以作为Bean实例,若只想把父Bean作为模板,可以设置<Bean>的abstract属性为true,这样Spring将不会实例化这个Bean。并不是<Bean>元素里所有的属性都会被继承,比如:atuowire,abstract等。也可以忽略父Bean的class属性,让子Bean指定自己的类,而共享的属性配置,但此时abstract必须设为true。

依赖:依赖Bean的配置

Spring允许用户通过depends-on属性设定Bean前置依赖Bean,前置依赖Bean会本Bean实例化之前创建好。 如果前置依赖多个Bean,则可通过逗号,空格的方式配置Bean的名称。 <bean id="car6" class="org.example.autowire.Car" p:brand="Audi" p:price="200"></bean> <!--要求在配置Person时必须有一个关联的car!,换句话说person 这个bean依赖于car这个bean--> <bean id="person" class="org.example.autowire.Person" p:name="TOM" p:address-ref="address1" depends-on="car6"></bean>

-------------------------------Spring–bean的作用域-----------------------------------

<!--使用bean的Scope属性配置Bean的作用域 默认为singleton 创建bean实例在整个容器的生命周期内只创建这一个bean,单例的。 prototype:原型的,容器初始化时不创建bean实例, 而在每次请求时都创建一个新的bean实例,并返回。--> <bean id="car" class="org.example.autowire.Car" scope="prototype"> <property name="brand" value="Audi"></property> <property name="price" value="3000"></property> </bean>

------------------------------Spring–使用外部属性文件------------------------------ 背景:

在配置文件里面配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路径,数据资源配置信息等)。而这些部署细节实际上需要和Bean配置相分离。Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置文件里使用形式${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换变量。Spring还允许在属性文件中使用${propName},以实现属性之间的相互引用。 <!--导入属性文件--> <context:property-placeholder location="classpath:dbconfig.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--使用外部属性文件的属性--> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> </bean>

--------------------------------------Spring–SpEL--------------------------------------- Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象的强大表达式语言。 语法类似于EL:SpEL使用#{…}作为定界符,所有在大框号中的字符都被认为是SpEL SpEL为bean的属性进行动态赋值提供了便利。

通过SpEL可以实现:通过bean的id对bean进行引用调用方法以及引用对象中的属性计算表达式的值正则表达式的匹配

字面量 整数:#{5} 小数:#{89.2} 科学计数法:#{1e4}

Spring可以使用单引号或双引号作为字符串的定界符号 #{“Chuck”} #{‘Chuck’} #{fslse}

最新回复(0)