Spring_自动装配 & bean之间的关系 & bean的作用域
1.自动装配
beans-autowire.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"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="address2" class="com.aff.spring.beans.autowire.Address" p:city="hefei" p:street="wanda"></bean>
<bean id="address" class="com.aff.spring.beans.autowire.Address" p:city="wuhu" p:street="wanda"></bean>
<bean id="car" class="com.aff.spring.beans.autowire.Car" p:brand="audi" p:price="250000"></bean>
<!-- 可以使用autowire 属性指定自动装配的方式
byName 根据bean 的名字和当前 bean 的setter 风格的属性名进行自动装配,若有匹配的, 则进行自动装配,
若没有匹配的, 则不装配
byType 根据 bean的类型 和当前bean 的属性的类型进行自动装配,
若IOC容器中有一个以上的类型匹配的bean, 则抛异常
-->
<bean id="person" class="com.aff.spring.beans.autowire.Person" p:name="Hxl" autowire="byName"></bean>
</beans>
Main.java
package com.aff.spring.beans.autowire; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
//Person [name=Hxl, addres=Address [city=hefei, street=wanda], car=Car [brand=audi, price=250000.0]] }
}
Person.java
package com.aff.spring.beans.autowire;
public class Person {
private String name;
private Address address;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", address=" + address + ", car=" + car + "]";
}
}
Address.java
package com.aff.spring.beans.autowire;
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
public Address() {
super();
}
public Address(String city, String street) {
super();
this.city = city;
this.street = street;
}
}
Car.java
package com.aff.spring.beans.autowire;
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
public Address() {
super();
}
public Address(String city, String street) {
super();
this.city = city;
this.street = street;
}
}
2.bean之间的关系:继承;依赖
beans-relation.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: bean 的abstract 属性为true 的bean ,
这样的bean 不能被IOC 容器实例化,只用来被继承配置 -->
<!--若一个bean的 class 属性都没有指定,则该bean 必须为一个抽象bean-->
<bean id="address" p:city="hefei" p:street="wanda" abstract="true"></bean> <!-- bean 配置的继承 : 使用 bean 的parent 属性 指定继承 那个bean的配置 -->
<bean id="address2" class="com.aff.spring.beans.autowire.Address" p:street="buxinjie" parent="address"></bean> <bean id="car" class="com.aff.spring.beans.autowire.Car" p:brand="audi" p:price="260000"></bean> <!-- 要求再配置 Person 时 , 必须有一个关联的Car 换句话说, Person 这个bean 依赖于Car这个bean-->
<bean id="person" class="com.aff.spring.beans.autowire.Person" p:name="Tom" p:address-ref="address2" depends-on="car"></bean>
</beans>
Main
package com.aff.spring.beans.relation; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.aff.spring.beans.autowire.Address;
import com.aff.spring.beans.autowire.Person; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-relation.xml");
// Address address = (Address) ctx.getBean("address");
// System.out.println(address);
// Address [city=hefei, street=wanda] Address address2 = (Address) ctx.getBean("address2");
System.out.println(address2);
// Address [city=hefei, street=buxinjie] Person person = (Person) ctx.getBean("person");
System.out.println(person);
//Person [name=Tom, address=Address [city=hefei, street=buxinjie], car=null] }
}
3.bean 的作用域:singleton;prototype;

beans-scope.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 的scope 属性来配置 bean 的作用域
singleton 默认的,容器初始化时创建bean实例, 在整个生命周期内只创建一个bean, 单例的
prototype : 原型的,容器初始化 不创建bean的实例,而在每次请求时都创建一个新的bean 实例,并返回
-->
<bean id="car" class="com.aff.spring.beans.autowire.Car" scope="singleton">
<property name="brand" value="audi"></property>
<property name="price" value="250000"></property>
</bean> </beans>
Main
package com.aff.spring.beans.scope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.aff.spring.beans.autowire.Car; public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml"); Car car = (Car) ctx.getBean("car");
Car car2 = (Car) ctx.getBean("car");
System.out.println(car==car2);
//true
}
}
目录

Spring_自动装配 & bean之间的关系 & bean的作用域的更多相关文章
- XML配置里的Bean自动装配与Bean之间的关系
需要在<bean>的autowire属性里指定自动装配的模式 byType(根据类型自动装配) byName(根据名称自动装配) constructor(通过构造器自动装配) 名字须与属性 ...
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...
- 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/
1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...
- Spring Bean之间的关系
bean之间的关系:继承和依赖继承bean的配置 Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的bean称为子bean 子bean从父bean中继承配置,包括 ...
- 峰Spring4学习(5)bean之间的关系和bean的作用范围
一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; priv ...
- Spring学习--Bean 之间的关系
Bean 之间的关系:继承.依赖. Bean 继承: Spring 允许继承 bean 的配置 , 被继承的 bean 称为父 bean , 继承这个父 bean 的 bean 称为子 bean. 子 ...
- Spring初学之bean之间的关系和bean的作用域
一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- 25、自动装配-@Profile根据环境注册bean
25.自动装配-@Profile根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的 ...
随机推荐
- 虚拟 IP 设为静态 IP
一:虚拟机设置桥接模式 1.进入虚拟机设置中将网络适配器设置成桥接模式 2.编辑--虚拟网络编辑器--选择桥接 二:将虚拟IP设置成静态IP (1)方案一:进入虚拟机系统 System 设置 (2)方 ...
- 1秒内通关扫雷?他创造属于自己的世界记录!Python实现自动扫雷
五一劳动节假期,我们一起来玩扫雷吧.用Python+OpenCV实现了自动扫雷,突破世界记录,我们先来看一下效果吧. 中级 - 0.74秒 3BV/S=60.81 相信许多人很早就知道有扫雷这么一款经 ...
- Integer和int及String的总结
秉承着总结发表是最好的记忆,我把之前遇到的问题在这里总结和大家分享一下,希望大家共同进步: 一.Integer和int首先说下自动拆装箱,基本数据类型转换为包装类型的过程叫装箱,反之则是拆箱,其中最特 ...
- CSS的基本语法及页面引用
CSS的基本语法及页面引用 CSS基本语法 CSS的定义方法是: 选择器 { 属性:值; 属性:值; 属性:值;} 选择器是将样式和页面元素关联起来的名称,属性是希望设置的样式属性每个属性有一个或多个 ...
- Spring 学习 之 再探publish-event机制
之前的文章链接:https://blog.csdn.net/qq_41907991/article/details/88544777 我们要知道的是,Spring的publish-event使用的是监 ...
- 王颖奇 20171010129《面向对象程序设计(java)》第十三周学习总结
实验十三 图形界面事件处理技术 实验时间 2018-11-22 1.实验目的与要求 (1) 掌握事件处理的基本原理,理解其用途: (2) 掌握AWT事件模型的工作机制: (3) 掌握事件处理的基 ...
- Git使用教程之SSH连接方式配置(二)
什么是GitHub?这个网站就是提供Git仓库托管服务的. 什么是SSH Key?你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,大白话理解就是这两个仓库如果要进行远程同步,则我们需 ...
- 一道题带你搞定Python函数中形参和实参问题
昨天在Python学习群里有位路人甲问了个Python函数中关于形参和实参一个很基础的问题,虽然很基础,但是对于很多小白来说不一定简单,反而会被搞得稀里糊涂.人生苦短,我用Python. 为了解答大家 ...
- SpringBoot基础实战系列(三)springboot单文件与多文件上传
springboot单文件上传 对于springboot文件上传需要了解一个类MultipartFile ,该类用于文件上传.我此次使用thymeleaf模板引擎,该模板引擎文件后缀 .html. 1 ...
- 解决Hystrix dashboard Turbine 一直 Loading…… 及其他坑
问题一.请求 /hystrix.stream 报错,我这里以端口9001为例 请求 http://localhost:9001/hystrix.stream 报404 是因为Srping Boot 2 ...