zoukankan      html  css  js  c++  java
  • Spring入门(5)-自动装配Bean属性

    Spring入门(5)-自动装配Bean属性

    本文介绍如何装配Bean属性。

    0. 目录

    1. ByName
    2. ByType
    3. constructor
    4. 默认自动装配
    5. 混合使用自动装配和显示装配

    1. ByName

    把与Bean的属性具有相同名字(或ID)的其他Bean自动装配到Bean对应的属性中。如果没有跟属性名称相匹配的Bean,则该属性不进行装配。

    package com.chzhao.springtest;
    
    public class PersonBll implements IPersonBll {
    
    	private Person person;
    
    	public Person getPerson() {
    		return person;
    	}
    
    	public void setPerson(Person person) {
    		this.person = person;
    	}
    
    	public void show() {
    		System.out.println(this.person.getName());
    	}
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
    	http://www.springframework.org/schema/beans/spring-beans.xsd">
    	<bean name="person" class="com.chzhao.springtest.Person">
    		<property name="name" value="老王" />
    	</bean>
    	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byName">
    	</bean> 
    </beans>
    

    2. ByType

    把与Bean的属性具有相同类型的其他Bean自动装配到Bean对应的属性中。如果没有跟属性类型相匹配的Bean,则该属性不进行装配。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
    	http://www.springframework.org/schema/beans/spring-beans.xsd">
    	<bean name="laowang" class="com.chzhao.springtest.Person">
    		<property name="name" value="老王" />
    	</bean>
    	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="byType">
    	</bean> 
    </beans>
    

    如上,如果设置为byType,Bean的名字随便设置了,不需要为person

    3. constructor

    把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。

    package com.chzhao.springtest;
    
    public class PersonBll implements IPersonBll {
    
    	public PersonBll(Person person) {
    		this.person = person;
    	}
    
    	private Person person;
    
    	public void show() {
    		System.out.println(this.person.getName());
    	}
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
    	http://www.springframework.org/schema/beans/spring-beans.xsd">
    	<bean name="laowang" class="com.chzhao.springtest.Person">
    		<property name="name" value ="老王"></property>
    	</bean>
    	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll" autowire="constructor">
    	</bean> 
    </beans>
    

    4. 默认自动装配

    Spring可以通过设置default-autowire使所有Bean采取相同配置。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
    	http://www.springframework.org/schema/beans/spring-beans.xsd"
    	default-autowire="constructor">
    	<bean name="laowang" class="com.chzhao.springtest.Person">
    		<property name="name" value ="老王"></property>
    	</bean>
    	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll">
    	</bean> 
    </beans>
    

    5. 混合使用自动装配和显示装配

    即使对某个Bean的属性采用了自动装配,仍然可以通过显示装配配置某个属性。

    package com.chzhao.springtest;
    
    public class PersonBll implements IPersonBll {
    
    	public Person getPerson() {
    		return person;
    	}
    
    	public void setPerson(Person person) {
    		this.person = person;
    	}
    
    	private Person person;
    
    	public void show() {
    		System.out.println(this.person.getName());
    	}
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
    	http://www.springframework.org/schema/beans/spring-beans.xsd"
    	default-autowire="byType"
    	>
    	<bean name="laowang" class="com.chzhao.springtest.Person">
    		<property name="name" value="老王" />
    	</bean>
    	<bean name="laoli" class="com.chzhao.springtest.Person">
    		<property name="name" value="老李" />
    	</bean>
    	<bean  name="PersonBll" class="com.chzhao.springtest.PersonBll" >
    		<property name="person" ref="laoli"></property>
    	</bean> 
    </beans>
    
  • 相关阅读:
    【工具类】图片压缩工具类,可压缩jpg, png等图片
    【Nginx用法】nginx location正则表达式写法,详解Nginx location 匹配规则(很详细哦)
    【Nginx异常】[error] 4236#29900: OpenEvent(“Global gx_reload_27128“) failed (5: Access is denied)
    【Nginx异常】Nginx启动一闪而过没反应,Nginx双击打开后,没有启动成功,也没有进程,且127.0.0.1:8080访问不到
    开启vue-element-ui打包生成报告
    Cas 5.2.x 使用 实现SSO单点登录的问题
    springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题
    企业微信会话存档开发与问题
    高手怎么查找CPU过高的Java代码。具体到行
    ubuntu中清除开始菜单中的应用图标
  • 原文地址:https://www.cnblogs.com/wardensky/p/4198923.html
Copyright © 2011-2022 走看看