笔记-spring 注入

来源:互联网 发布:python初学者看什么书 编辑:程序博客网 时间:2024/04/27 03:10
package com.dora.testingPerson.bean;import com.dora.testingPerson.ibean.Axe;import com.dora.testingPerson.ibean.Person;public class American implements Person {String americanName = null;Axe axe = null;public void setAmericanName(String name) {this.americanName = name;}public void setAxe(Axe axe) {this.axe = axe;}public void useAxe() {// TODO Auto-generated method stubSystem.out.println("hello, my name is "+this.americanName + ". " + axe.chop());}}


ExampleBean类

package com.dora.testingPerson.bean;import com.dora.testingPerson.ibean.Person;public class ExampleBean {private American person = new American();public Person getPerson() {return this.person;}}


ExampleBean有一个成员对象是American

applicationContext.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-3.0.xsd"><bean id="stealaxe" class="com.dora.testingPerson.bean.StealAxe" >  <property name="helloWord" value="a steal axe is helpful in the wild!" /></bean>  <bean id="exampleBean" class="com.dora.testingPerson.bean.ExampleBean">   <property name="person.americanName" value="孙悟空" />   <property name="person.axe" ref="stealaxe" /> </bean></beans>

 

Spring要先调用ExampleBean的getPerson方法先去的American的实例子,再调用American的setAmericanName和setAxe方法。

所以,ExampleBean方法一定要提供一个getPerson方法,spring才能给americanName和axe赋值。