3、引用其他bean

  Bean经常需要相互协作完成应用程序的功能,bean之间必须能够互相访问,就必须在bean配置之间指定对bean的引用,可以通过节点<ref>或者ref来为bean属性指定对bean的引用,也可以在属性或者构造器里包含bean的声明,这样bean称为内部bean。

bean中引用其他bean,其中Car为对象。

 <!-- 通过构造方法配置bean属性 -->
<bean id="car" class="hello.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="ShangHai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double" ></constructor-arg> </bean> <bean id="person" class="hello.Person">
<property name="name" value = "Tom"></property>
<property name="age" value = "24"></property>
<property name="car" ref="car"></property>
</bean>

内部bean

 <bean id="person" class="hello.Person">
<property name="name" value = "Tom"></property>
<property name="age" value = "24"></property>
<!--
<property name="car" ref="car"></property>--> <!-- 内部bean -->
<property name="car">
<!-- 内部bean不能被外部bean使用 -->
<bean id="car3" class="hello.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="Changan"></constructor-arg>
<constructor-arg value="200000" type="double"></constructor-arg> </bean>
</property>
</bean>

4、集合属性

在Spring中可以通过<list>、<set>或者<map>来配置集合属性。

通过list配置集合属性

Person.java

 package com.spring;

 import java.util.List;

 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 + ", cars=" + cars + "]";
} }

Car.java

 package com.spring;

 public class Car {
private String brand;
private double price;
private int maxSpeed;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
} }

beans-collection.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd"> <bean id="car1" class="com.spring.Car">
<property name="brand" value="audi"></property>
<property name="price" value="400000"></property>
<property name="maxSpeed" value="240"></property>
</bean> <bean id="car2" class="com.spring.Car">
<property name="brand" value="baoma"></property>
<property name="price" value="700000"></property>
<property name="maxSpeed" value="270"></property>
</bean> <bean id="person" class="com.spring.Person">
<property name="name" value="Jerry"></property>
<property name="age" value="41"></property>
<property name="cars">
<list>
<ref bean="car1" />
<ref bean="car2" />
</list>
</property>
</bean>
</beans>

结果:

通过map配置属性

Person.java

 package com.spring;

 import java.util.Map;

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

beans-collection

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd"> <bean id="car1" class="com.spring.Car">
<property name="brand" value="audi"></property>
<property name="price" value="400000"></property>
<property name="maxSpeed" value="240"></property>
</bean> <bean id="car2" class="com.spring.Car">
<property name="brand" value="baoma"></property>
<property name="price" value="700000"></property>
<property name="maxSpeed" value="270"></property>
</bean> <bean id="person" class="com.spring.Person">
<property name="name" value="Jerry"></property>
<property name="age" value="41"></property>
<property name="carMap">
<map>
<entry key="1" value-ref="car1"></entry>
<entry key="2" value-ref="car2"></entry>
</map>
</property>
</bean>
</beans>

结果:

配置Set属性,和配置list一样。

5、p名字空间注入,也需要setter方法。

Student.java

 package com.spring;

 public class Student {
private String name;
private String number; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
} @Override
public String toString() {
return "Student [name=" + name + ", number=" + number + "]";
} }

Main.java

 package com.spring;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans-p.xml");
Student student = (Student) ac.getBean("student");
System.out.println(student);
}
}

beans-p.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.spring.Student" p:name="Merry" p:number="1120141314"></bean> </beans>

执行结果

6、property注入

Student.java

 package com.spring;

 import java.util.Properties;

 public class Student {
private String name;
private String number;
private Properties properties; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "Student [name=" + name + ", number=" + number + ", properties=" + properties + "]";
} }

beans-p.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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-4.3.xsd">
<bean id="student" class="com.spring.Student">
<property name="name" value="Merry"></property>
<property name="number" value="1120141213"></property>
<property name="properties" >
<props>
<prop key="user">root</prop>
<prop key="psw">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property> </bean>
</beans>

执行结果

7、c命名空间注入

Spring注入方式(2)的更多相关文章

  1. Spring注入方式(1)

    Spring支持3种依赖注入方式,分别为属性注入.构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解. 1.常量注入 属性注入是通过setter方法注入Bean的属性 ...

  2. Spring注入方式及用到的注解

    注入方式: 把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注 入时不要new 这个注入的类,因为spring会自动注入,如 ...

  3. Spring课程 Spring入门篇 2-2 Spring注入方式

    课程链接: 本节主要讲了以下两块内容: 1 xml两种注入方式 2 注入方式代码实现 3 特别注意 1 xml两种注入方式 构造注入和set注入 2 注入方式代码实现 2.1 set注入方式的实现 实 ...

  4. 【原创】Spring 注入方式

    Spring 强烈推荐注解在构造器上,且对于不能为null的字段或者属性都用断言. 1. 设值注入 原理:通过setter方法注入 XML配置方式:bean下的property标签,用value指定基 ...

  5. Spring注入方式及注解配置

    一:基于xml的DI(Dependency Injection) 注入类型: 定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car Student ...

  6. Spring源码学习之:你不知道的spring注入方式

    前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ...

  7. 一个接口多个实现类的Spring注入方式

    1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ...

  8. Spring注入方式

  9. Spring学习三----------注入方式

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...

随机推荐

  1. Linux远程桌面实现(转)

    http://blog.csdn.net/txg703003659/article/details/6619652 先说一下本人的实现环境: 控制端:Ubuntu 11.04 被控制端:CentOS ...

  2. JAVA里的CAS算法简析

    Atomic 从JDK5开始, java.util.concurrent包里提供了很多面向并发编程的类. 使用这些类在多核CPU的机器上会有比较好的性能.主要原因是这些类里面大多使用(失败-重试方式的 ...

  3. Laravel中Trait的用法实例详解

    本文实例讲述了Laravel中Trait的用法.分享给大家供大家参考,具体如下: 看看PHP官方手册对Trait的定义: 自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 trait ...

  4. An interview question from MicroStrategy

    去年校招时的一道面试题,觉得蛮有意思,贴出来. Question: Spy start at a, during an interval he moves |b| to right when b &g ...

  5. 解决windows搭建jenkins执行selenium无法启动浏览器问题

    因为jenkins是用windows installer 安装成windows的服务了,那么jenkins是一个后台服务,所以跑selium cases 的时候不显示浏览器 Step 1. Contr ...

  6. ZOJ 3702 Gibonacci number 2017-04-06 23:28 28人阅读 评论(0) 收藏

    Gibonacci number Time Limit: 2 Seconds      Memory Limit: 65536 KB In mathematical terms, the normal ...

  7. 试题 D: 数的分解 蓝桥杯

    试题 D: 数的分解本题总分: 10 分[问题描述]把 2019 分解成 3 个各不相同的正整数之和,并且要求每个正整数都不包含数字 2 和 4,一共有多少种不同的分解方法?注意交换 3 个整数的顺序 ...

  8. [label][WorldPress] 一个很方便查找定位WorldPress源代码位置的网址

    作为 WordPress 的新手,根本不熟悉那些函数究竟是什么作用的,所以就必须要去看源代码. 要去查看源代码,那么你就必须要熟悉 WordPress 下面文件的作用,以及那个文件中定义了有哪些函数? ...

  9. Discuz showmessage函数解析[转]

    函数相关文件 \source\function\function_core.php\source\function\function_message.php ## 函数解释 /** * 显示提示信息 ...

  10. [Erlang35]Erlang18的time

    在Erlang 18中, 我们还是可以使用erlang:now/0 但也已经 deprecated啦,大量使用可能引发瓶颈. The default time warp mode has the sa ...