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的作用域的更多相关文章

  1. XML配置里的Bean自动装配与Bean之间的关系

    需要在<bean>的autowire属性里指定自动装配的模式 byType(根据类型自动装配) byName(根据名称自动装配) constructor(通过构造器自动装配) 名字须与属性 ...

  2. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  4. 3.spring:自动装配/Bean之间的关系/作用域/外部文件/spel/

    1.自动装配/手动装配 xml配置文件里的bean自动装配 Spring IOC 容器里可以自动的装配Bean,需要做的仅仅是在<bean>的autowire属性里面指定自动装配模式 -& ...

  5. Spring Bean之间的关系

    bean之间的关系:继承和依赖继承bean的配置 Spring允许继承bean的配置,被继承的bean称为父bean,继承这个父bean的bean称为子bean 子bean从父bean中继承配置,包括 ...

  6. 峰Spring4学习(5)bean之间的关系和bean的作用范围

    一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; priv ...

  7. Spring学习--Bean 之间的关系

    Bean 之间的关系:继承.依赖. Bean 继承: Spring 允许继承 bean 的配置 , 被继承的 bean 称为父 bean , 继承这个父 bean 的 bean 称为子 bean. 子 ...

  8. Spring初学之bean之间的关系和bean的作用域

    一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  9. 25、自动装配-@Profile根据环境注册bean

    25.自动装配-@Profile根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的 ...

随机推荐

  1. 题目分享P

    题意: 给出一棵n个节点的树,这棵树的每条边有一个权值,这个权值只可能是0或1. 在一局游戏开始时,会确定一个节点作为根.接下来从女生开始,双方轮流进行 操作.当一方操作时,他们需要先选择一个不为根的 ...

  2. MySQL JDBC Driver 8.0+设置服务器时区

    遇到一个问题,线下环境测试数据的查询完全没有问题,但是线上环境却没法查询出数据,并且从mybatis输出的日志来看,查询参数也没有问题,数据库中数据也是存在的,查询参数类型是java.util.Dat ...

  3. D. Distance in Tree(树型Dp计数)

    \(其实思路都能想到一点,就是去重这里特别麻烦,没有好的思路.\) \(设dp[i][j]为以i为根深度为j的节点数量\) \(dp[parent][j]=\sum{dp[son][j-1]}\) \ ...

  4. E - No Pain No Game 线段树 离线处理 区间排序

    E - No Pain No Game  HDU - 4630 这个题目很好,以后可以再写写.这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u01003321 ...

  5. docker-compose安装rabbitmq

    编写时间:2020-05-08 参考文档:docker安装rabbitmq 1. 编写docker-compose.yml version: '3' services: rabbitmq: image ...

  6. 循环结构(for、while)

    3.4用for语句实现循环结构 什么是循环结构 for语句 1.什么是循环结构? 循环结构又称为重复结构,是利用计算机运算速度快以及能进行逻辑控制的特点来重复执行某些操作.重复执行的部分称为循环体. ...

  7. 关于 k210 的 micropython 添加 ussl 模块,实现 https 访问支持的那些事。

    起因 事情已经过去快一周了吧,继上次修复 maixpy k210 的 esp8285 at 通信后,突然遇到泽畔大大问,要不要做 ussl 的支持? 评估了一下各方的实现,想了一下自己也刚好在做网络层 ...

  8. QTreeWidget更新后保存节点的展开状态

    class Xx : public QWidget { Q_OBJECT struct ItemState{ ItemState(); int _id; bool _isExpend; }; publ ...

  9. 设计模式之GOF23状态模式

    状态模式state 场景:当具有许多状态并且需要频繁改变时,用这种模式 -电梯的运行:维修,正常,自动关门,自动开门,向上运行,向下运行,消防状态 -红绿灯:红灯,黄灯,绿灯 -企业或政府系统:公文的 ...

  10. 两个有序数组 A1 A2 的合并

    /** * 问题6.有序数组 A1 A2 的合并 */ @Test public void orderArrayMerge() { // 两个有序数组 A1 A2 的合并 int[] A1 = {1, ...