spring 自动装配

 spring自动装配分两种,按名字:byName,按类型:byType

 

 

<?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-2.0.xsd"
 default-autowire="byName/byType">
 
 <bean id="abstractBean" abstract="true">
  <property name="id" value="123"></property>
  <property name="name" value="John"></property>
 </bean>
 <!--
 <bean id="bean2" class="testBean.Bean2">
  <property name="bean3" ref="bean3"/>
  <property name="bean4" ref="bean4"/>
 </bean>
  -->
  <bean id="bean2" class="testBean.Bean2"/>
 <bean id="bean3" class="testBean.Bean3" parent="abstractBean">
  <property name="name" value="Jim"></property>
  <property name="password" value="aab"></property>
 </bean>
 
 <bean id="bean4" class="testBean.Bean4" parent="abstractBean"></bean>
</beans>

==================================Bean2.java===================

public class Bean2 {
 
 private Bean3 bean3;//按名字装载,spring会根据配置文件中的id查找实例
 
 private Bean4 bean4;//按类型.spring会根据类型查找实例

 public Bean3 getBean3() {
  return bean3;
 }

 public void setBean3(Bean3 bean3) {
  this.bean3 = bean3;
 }

 public Bean4 getBean4() {
  return bean4;
 }

 public void setBean4(Bean4 bean4) {
  this.bean4 = bean4;
 }

 
}

=========================

你可能感兴趣的:(spring,bean,Class,encoding)