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 < ...
随机推荐
- Laravel 在哪些地方使用了 trait ?
laravel 框架大量使用了traits. 简单举几个例子: 在Eloquent中使用了trait .然后在model初始化的时候,有个boot方法,会自动判断当前的类用了哪些trait.然后得到一 ...
- Core Graphics Layer Drawing
[Core Graphics Layer Drawing] CGLayer objects (CGLayerRef data type) allow your application to use l ...
- Mac完整卸载Android Studio的方法
1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ Studio.app rm -Rf ~/Library/Pr ...
- BZOJ 2002 Bounce 弹飞绵羊 (分块或动态树)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 13768 Solved: 6989[Subm ...
- metasploitable实践
使用Fimap和metasploitable2文件包含漏洞测试 fimap 首先查看msf已经存在的漏洞: root@kali:~# fimap -u 'http://192.168.136.130/ ...
- Linux 基础教程 37-进程命令
pidof 我们知道每个小孩一出生就会一个全国唯一的编号来对其进行标识,用于以后上学,办社保等,就是我们的身份证号.那么在Linux系统中,用来管理运行程序的标识叫做PID,就是大家熟知的进程 ...
- (博弈 sg入门)kiki's game -- hdu -- 2147
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2147 题意: 在一个n*m的棋盘上,从 (1,m),即右上角开始向左下角走. 下棋者只能往左边(lef ...
- handsontable-developer guide-cell function
renderer 展示的数据不是来自于数据源,而是先把DOM和其他信息传给renderer,然后展示. //五种展示函数 TextRenderer: default NumericRenderer A ...
- c3p0--常见异常
获取资源timeout: 异常信息如下: Caused by: java.sql.SQLException: An attempt by a client to checkout a Connecti ...
- 创建TFS团队项目时自动建立代码库的文件夹结构
很多客户都跟我提过一个这样的需求,即需要在创建团队TFS项目时,自动创建起源代码库的文档结构,例如类似下列结构的文件夹: <teamProject> |- DEVELOPMENT ...