Spring注入方式(2)
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)的更多相关文章
- Spring注入方式(1)
		Spring支持3种依赖注入方式,分别为属性注入.构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解. 1.常量注入 属性注入是通过setter方法注入Bean的属性 ... 
- Spring注入方式及用到的注解
		注入方式: 把DAO实现类注入到service实现类中,把service的接口(注意不要是service的实现类)注入到action中,注 入时不要new 这个注入的类,因为spring会自动注入,如 ... 
- Spring课程 Spring入门篇 2-2 Spring注入方式
		课程链接: 本节主要讲了以下两块内容: 1 xml两种注入方式 2 注入方式代码实现 3 特别注意 1 xml两种注入方式 构造注入和set注入 2 注入方式代码实现 2.1 set注入方式的实现 实 ... 
- 【原创】Spring 注入方式
		Spring 强烈推荐注解在构造器上,且对于不能为null的字段或者属性都用断言. 1. 设值注入 原理:通过setter方法注入 XML配置方式:bean下的property标签,用value指定基 ... 
- Spring注入方式及注解配置
		一:基于xml的DI(Dependency Injection) 注入类型: 定义学生Student实体类和小汽车Car实体类:进行封装和生成ToString(),并自定义属性Car Student ... 
- Spring源码学习之:你不知道的spring注入方式
		前言 在Spring配置文件中使用XML文件进行配置,实际上是让Spring执行了相应的代码,例如: 使用<bean>元素,实际上是让Spring执行无参或有参构造器 使用<prop ... 
- 一个接口多个实现类的Spring注入方式
		1. 首先, Interface1 接口有两个实现类 Interface1Impl1 和 Interface1Impl2 Interface1 接口: package com.example.serv ... 
- Spring注入方式
- Spring学习三----------注入方式
		© 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ... 
随机推荐
- Resolving multicopy duplications de novo using polyploid phasing  用多倍体相位法解决多拷贝复制的新问题
			抽象.虽然单分子测序系统的兴起已经实现组装复杂地区的能力空前提高在基因组中,基因组中的长节段重复仍然是装配中具有挑战性的前沿. 分段重复同时具有丰富的基因并且倾向于大的结构重排,使得它们的序列的分辨率 ... 
- 第六章 Windows应用程序对键盘与鼠标的响应 P121 6-8
			基于键盘与鼠标应用的程序设计 一.实验目的 1.掌握键盘与鼠标在应用程序中的消息响应机制. 二.实验内容及步骤 实验任务 1.熟悉键盘的消息响应: 2.熟悉鼠标的消息响应: 实验内容 设计一个窗口 ... 
- ie高版本浏览器不支持velocity的问题解决
			<head><meta http-equiv="X-UA-Compatible" content="IE=5"></head> ... 
- 阿里云服务器ECS按ctrl+alt+delete无法登录
			今天在使用阿里云服务器远程桌面的时候发现怎么也进入不了,远程桌面无法连接,于是想到了在阿里云服务器管理控制台可以使用连接管理终端进行远程桌面连接,下面详细介绍阿里云服务器操作经验. 操作步骤如下 登录 ... 
- 转  group_concat函数详解
			MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔 ... 
- 排序:桶排序Bucket sort
			补充说明三点 1,桶排序是稳定的 2,桶排序是常见排序里最快的一种,比快排还要快…大多数情况下 3,桶排序非常快,但是同时也非常耗空间,基本上是最耗空间的一种排序算法 无序数组有个要求,就是成员隶属于 ... 
- Hdu1051 Wooden Sticks                                                                                            2017-03-11 23:30             62人阅读              评论(0)              收藏
			Wooden Sticks Problem Description There is a pile of n wooden sticks. The length and weight of each ... 
- (轉載)sql server xml字段的操作
			原文轉自:http://blog.csdn.net/hliq5399/article/details/8315373 另外可參考:https://msdn.microsoft.com/en-us/li ... 
- HDU 5974 A Simple Math Problem(数论+结论)
			Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ... 
- tomcat启动时就频繁gc和full gc
			一个小业务,流量并不大,功能也很简单,spring framework+mybatis+quartz,一启动就看到gc的频次和full gc的频次非常高: 4.202: [Full GC 4.202: ... 
