配置bean的一些细节

字面值

如果包含特殊符号,直接写会报错。可以用这个<![CDATA[]]>包裹起来。

比如这里的配置属性里面的value值包含<>等特殊符号,直接写会报错。可以用这个<![CDATA[]]>包裹起来。

<bean id="car2" class="com.spring.beans.Car" >
<constructor-arg value="Audi" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg index="1" type="java.lang.String">
<value><![CDATA[<shanghai^>]]></value>
</constructor-arg>
<constructor-arg type="double">
<value>250</value>
</constructor-arg>
</bean>

引用其他bean

可以使用<ref>元素或者property的ref属性,建立bean之间的引用关系

比如这里有个person类,里面有个属性是Car类。在配置bean的时候,需要给car属性赋值,这时候可以使用ref属性。

person类:

package com.spring.beans;

public class Person {

    private String name;
private int age; private 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 Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
} }

配置:可以使用property的ref属性,建立bean之间的引用关系

    <!-- 如果包含特殊符号,直接写会报错。可以用这个<![CDATA[]]>包裹起来 -->
<bean id="car2" class="com.spring.beans.Car" >
<constructor-arg value="Audi" index="0" type="java.lang.String"></constructor-arg>
<constructor-arg index="1" type="java.lang.String">
<value><![CDATA[<shanghai^>]]></value>
</constructor-arg>
<constructor-arg type="double">
<value>250</value>
</constructor-arg>
</bean> <bean id="person" class="com.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 可以使用property的ref属性,建立bean之间的引用关系 -->
<property name="car" ref="car2"></property>
</bean>

或者使用<ref>元素配置:

  <bean id="person" class="com.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 可以使用property的ref属性,建立bean之间的引用关系 -->
<!-- <property name="car" ref="car2"></property> -->
<property name="car">
<ref bean="car2"></ref>
</property>
</bean>

使用这个person类。

         Car car=(Car) ctx.getBean("car");
Car car2=(Car) ctx.getBean("car2");
System.out.println(car);
System.out.println(car2); Person person=(Person) ctx.getBean("person");
System.out.println(person);

运行结果:

也可以在属性或者构造器中包含Bean的声明,这样的bean称为内部bean。

    <bean id="person" class="com.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car">
<bean class="com.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="changan"></constructor-arg>
<constructor-arg value="200" type="double"></constructor-arg>
</bean>

</property>
</bean>

比如注入null值:

为级联属性赋值:

集合属性

使用list节点为list类型的属性赋值

这里拿<list>来做例子:

例如:

package com.spring.beans.collections;
import java.util.List;
import com.spring.beans.Car; public class Person { private String name;
private int age; private List<Car> cars; 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 + ", car=" + cars + "]";
}
}

配置:

<!-- 测试集合属性 -->
<bean id="person3" class="com.spring.beans.collections.Person">
<property name="name" value="mike"></property>
<property name="age" value="27"></property>
    <!-- 使用list节点为list类型的属性赋值 -->
<property name="cars">
<list>
<ref bean="car"></ref>
<ref bean="car2"></ref>
</list>
</property>

</bean>

运行:

set集合的使用跟list集合差不多类似;

Map集合的使用如下:

新建一个newPerson类:

package com.spring.beans.collections;
import java.util.Map;
import com.spring.beans.Car; public class NewPerson { private String name;
private int age;
private Map<String,Car> cars; 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 Map<String,Car> getCars() {
return cars;
} public void setCars(Map<String,Car> cars) {
this.cars =
cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", car=" + cars + "]";
}
}

配置:

<!-- 测试Map集合属性 -->
<bean id="person3" class="com.spring.beans.collections.NewPerson">
<property name="name" value="rose"></property>
<property name="age" value="28"></property>
<property name="cars">
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car2"></entry>
</map>
</property>
</bean>

配置properties属性值

使用props 和 prop 为属性 properties 赋值

package com.spring.beans.collections;
import java.util.Properties; public class DataSource { private Properties properties; public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
} @Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}

配置:

    <!-- 配置properties属性值 -->
<bean id="dataSource" class="com.spring.beans.collections.DataSource">
<!-- 使用props 和 prop 为属性 properties 赋值 -->
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">911581</prop>
<prop key="jdbcurl">jdbc:mysql:///sys</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>

运行结果:

配置单例的集合bean

配置单例的集合bean,以供多个bean使用,需要导入util命名空间。

为了导入util命名空间,需要到这里勾选上util。

然后配置如下:

以list集合的集合bean为例:

   <!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间 -->
<util:list id="cars5">
<ref bean="car1"></ref>
<ref bean="car2"></ref>
</util:list>

对于其他类型的集合,直接把list换成对应的集合类型名就行,然后util里面的内容按照上面的集合配置即可。

使用直接用ref即可:

    <bean id="person4" class="com.spring.beans.collections.NewPerson">
<property name="name" value="rose"></property>
<property name="age" value="28"></property>
<property name="cars" ref="car5"></property>
</bean>

通过p命名空间为bean属性赋值

通过p命名空间为bean的属性赋值 ,需要先导入p命名空间,相对于传统的配置方式更简洁。

首先导入p命名空间

配置bean:

<!-- 通过p命名空间为bean的属性赋值 ,需要先导入p命名空间-->
<bean id="person5" class="com.spring.beans.collections.Person"
p:age="30" p:name="张三丰" p:cars-ref="cars" ></bean>

注意:

1,为普通属性赋值的写法:p:属性名=“值”;

2,赋值的属性需要通过引用其他bean的,比如上面的p:cars-ref,这种引用其他bean的,写法是:赋值的属性-ref

---恢复内容结束---

03-spring学习-属性配置细节的更多相关文章

  1. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  2. Spring_配置Bean & 属性配置细节

    1.Spring容器 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.Sp ...

  3. Spring Boot 属性配置和使用

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  4. Spring Boot 属性配置和使用(转)

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  5. 4. selectKey语句属性配置细节

    selectKey语句属性配置细节:

  6. Spring 学习笔记 4. 尚硅谷_佟刚_Spring_属性配置细节

    1,字面值 •字面值:可用字符串表示的值,可以通过 <value> 元素标签或 value 属性进行注入. •基本数据类型及其封装类.String 等类型都可以采取字面值注入的方式 •若字 ...

  7. Sping Boot入门到实战之入门篇(三):Spring Boot属性配置

    该篇为Sping Boot入门到实战系列入门篇的第三篇.介绍Spring Boot的属性配置.   传统的Spring Web应用自定义属性一般是通过添加一个demo.properties配置文件(文 ...

  8. Spring Boot 属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  9. Spring Boot属性配置&自定义属性配置

    一.修改默认配置 例1.spring boot 开发web应用的时候,默认tomcat的启动端口为8080,如果需要修改默认的端口,则需要在application.properties 添加以下记录: ...

随机推荐

  1. Hive的严格模式

    在hive里面可以通过严格模式防止用户执行那些可能产生意想不到的不好的效果的查询,从而保护hive的集群. 用户可以通过 set hive.mapred.mode=strict 来设置严格模式,改成u ...

  2. 【扫描线】Gym - 101190E - Expect to Wait

    假设初始人数为0, 将每个时刻在等待的人数写下来,就是求个和. 如果纵坐标看成人数,横坐标看成时间,就是求个面积. 因为初始人数不一定为零,所以离线后扫描线即可回答所有询问. #include< ...

  3. 【Trie模板】HDU1251-统计难题

    [题意] n统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). [思路] 裸题,不过G++好像会超内存,C++就不会. #include<iostream> #include& ...

  4. [BZOJ1001](BeiJingOI 2006)狼抓兔子

    Description   Source: Beijing2006 [BJOI2006] 八中OJ上本题链接:http://www.lydsy.com/JudgeOnline/problem.php? ...

  5. llvm之旅第一站 - 编译及简单使用 LLVM 图解

    http://www.nagain.com/activity/article/4/ http://blog.csdn.net/snsn1984/article/details/8593380

  6. C# 注释换行

    在写程序的时候,有时需要对注释进行换行.如下: class Program { static void Main(string[] args) { Msg(); } /// <summary&g ...

  7. 【spring colud】spring cloud微服务项目搭建【spring boot2.0】

    spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ...

  8. 【hibernate】错误:org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from xx to xx

    所报错误: org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was ...

  9. 8.8.8.8和8.8.4.4 DNS域名解析服务器

    而Google表示推出免费DNS服务的主要目的就是为了改进网络浏览速度.改善网络用户的浏览体验,为此Google自行开发的软件对DNS服务器技术进行了改进,通过采用预获取技术提升性能,同时保证了DNS ...

  10. IntelliJ IDEA的几种常见的快捷键

    在编写代码的时候直接输入psv就会看到一个psvm的提示,此时点击tab键一个main方法就写好了. psvm 也就是public static void main的首字母. 依次还有在方法体内键入f ...