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. unittest(执行用例)

    from selenium import webdriver from time import sleep import unittest#导入unittest库 import HTMLTestRun ...

  2. Kafka平滑滚动升级2.4.0指南

    今天测试了下kafka从2.0.0滚动升级至2.4.0,下面做一下记录.这个链接是Kafka官网对升级2.4.0的指南,可以参考  http://kafka.apache.org/24/documen ...

  3. 区间dp E - Multiplication Puzzle POJ - 1651

    E - Multiplication Puzzle  POJ - 1651 这个题目没有特别简单,但是也没有我想象之中的那么难,这个题目时区间dp,因为我们是要对区间进行考虑的. 但是呢,这个也和动态 ...

  4. search(11)- elastic4s-模糊查询

    很多时候搜索用户对查询语句具有模糊感觉,他们只能提供大约的描述.比如一个语句的部分,或者字句顺序颠倒等.通过模糊查询可以帮助用户更准确的找出他们希望搜索的结果. 模糊查询包括前后缀,语句(phrase ...

  5. LFU C# 实现

    周六早上  做了下力扣的LRU 题目  后面接着看了LFU 缓存  难度提高了不少 首先 先说下 这2着 的差别把 LRU :最近 最少使用算法(Least  Recently Used).LRU 是 ...

  6. 出现Please make sure you have the correct access rights and the repository exists.问题解决

    问题: 有一段时间没有用码云了,当输入 git push -u origin master命令出现Please make sure you have the correct access rights ...

  7. 推荐一款 python 管理工具:anaconda

    1.jpg 2.jpg 3.jpg 4.jpg 5.jpg 6.jpg 7.jpg 8.jpg 9.jpg 10.jpg 11.jpg 12.jpg 13.jpg 14.jpg 15.jpg 16.j ...

  8. linux centos7 和 windows下 部署 .net core 2.0 web应用

    centos7 下部署asp.net core 2.0应用 安装CentOS7 配置网络[可选] 安装.Net core2.0 创建测试Asp.net Core应用程序 正式部署项目 安装VMware ...

  9. zsy后台管理系统-架构设计

    Zsy框架总体架构设计 1.Mysql数据库,存储所有表的数据. 2.Zsy-基础项目(Zsy-Model,Zsy-Dao,Zsy-Service,Zsy-Web),基于SSM框架.项目功能包含基本的 ...

  10. 00002-layui 右侧呼出页面,PopupLayer

    我这里的功能是弹出 右侧搜索 的页面: top.layui.admin.popupRight({ id: 'LAY_business_PopupLayer' ,area: '350px' ,succe ...