1.代码结构图

xxx

2.bean代码

package com.xxx.bean;

/**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午1:25
* To change this template use File | Settings | File Templates.
*/
public abstract class People {
protected String name;
protected int age;
protected Pet pet; public abstract String speak(); @Override
public String toString() {
return "I am " + name + ", I'm " + age +
" years old. And I have a pet named " + pet.getName();
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Pet getPet() {
return pet;
} public void setPet(Pet pet) {
this.pet = pet;
}
} package com.xxx.bean; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午1:28
* To change this template use File | Settings | File Templates.
*/
public class Chinese extends People {
public Chinese(String name, int age, Pet pet) {
this.name = name;
this.age = age;
this.pet = pet;
} public Chinese() {
} @Override
public String speak() {
return "I can speak Chinese";
}
} package com.xxx.bean; import java.util.List; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午2:14
* To change this template use File | Settings | File Templates.
*/
public class American extends People {
private List<Pet> petList; @Override
public String speak() {
return "I can speak English!";
} public List<Pet> getPetList() {
return petList;
} public void setPetList(List<Pet> petList) {
this.petList = petList;
}
} package com.xxx.bean; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午1:26
* To change this template use File | Settings | File Templates.
*/
public abstract class Pet {
protected String name;
public abstract String bark(); @Override
public String toString() {
return "My name is " + name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
} package com.xxx.bean; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午1:28
* To change this template use File | Settings | File Templates.
*/
public class Dog extends Pet {
public Dog() {
} public Dog(String name) {
this.name = name;
} @Override
public String bark() {
return "Wang wang";
}
} package com.xxx.bean; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午1:29
* To change this template use File | Settings | File Templates.
*/
public class Cat extends Pet {
public Cat() {
} @Override
public String bark() {
return "miao";
}
}

3.applicationContext.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"> <!-- 构造方法注入示例 -->
<!-- scope参数表示bean的作用域,如下 -->
<!-- singleton,默认值,一个bean只有一个实例 -->
<!-- prototype,每次调用创建一个实例 -->
<!-- request,每次http请求对应一个实例,仅在Spring MVC上下文有效 -->
<!-- session,每个session对应一个实例,仅在Spring MVC上下文有效 -->
<!-- global-session,每个全局session对应一个实例,尽在Portlet上下文有效 -->
<bean id="chineseA" class="com.xxx.bean.Chinese" scope="prototype">
<constructor-arg value="Li Lei"/>
<constructor-arg value="15"/>
<constructor-arg ref="dogA"/>
</bean> <bean id="dogA" class="com.xxx.bean.Dog">
<constructor-arg value="Bobby"/>
</bean> <!-- 工厂方法注入示例 -->
<!-- 如果bena的类型是一个单例模式类 -->
<!-- 那么注入这个类的方法则是使用其工厂方法生成实例 -->
<!--<bean id="staticClass" -->
<!--class="com.xxx.bean.StaticClass"-->
<!--factory-method="getInstance" />--> <!-- 属性注入示例 -->
<!-- 使用p前缀,直接引入属性或属性引用 -->
<bean id="chineseB" class="com.xxx.bean.Chinese" scope="prototype"
p:pet-ref="catA">
<property name="name" value="Han Meimei" />
<property name="age" value="18" />
<!--<property name="pet" ref="catA" />-->
</bean> <bean id="catA" class="com.xxx.bean.Cat"
p:name="Kitty" /> <!-- 集合注入示例 -->
<!-- 集合注入包括List,Set,Map,Properties -->
<!-- 此处以List为例子,其他不详写 -->
<bean id="americanA" class="com.xxx.bean.American" scope="prototype">
<property name="name" value="Michael Johnson" />
<property name="age" value="28" />
<property name="pet" ref="dogB" />
<property name="petList">
<list>
<ref bean="dogA" />
<ref bean="catA" />
</list>
</property>
</bean> <bean id="dogB" class="com.xxx.bean.Dog"
p:name="Cookie" />
</beans>

4.测试代码及结果

package com.xxx.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午1:51
* To change this template use File | Settings | File Templates.
*/
public class ConstructInjectTest {
/**
* Spring的3种应用上下文介绍
* ClassPathXmlApplicationContext-从运行时加载类路径下读取XML配置
* FileSystemXmlApplicationContext-从文件系统读取XML配置文件(绝对路径)
* XmlWebApplicationContext-从Web应用下读取XML配置文件
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
People chinese = (People) ctx.getBean("chineseA");
Pet dog = chinese.getPet();
System.out.println(chinese);
System.out.println(chinese.speak());
System.out.println(dog);
System.out.println(dog.bark());
}
}

I am Li Lei, I'm 15 years old. And I have a pet named Bobby
I can speak Chinese
My name is Bobby
Wang wang

package com.xxx.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午2:13
* To change this template use File | Settings | File Templates.
*/
public class PropInjectTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
People chinese = (People) ctx.getBean("chineseB");
Pet dog = chinese.getPet();
System.out.println(chinese);
System.out.println(chinese.speak());
System.out.println(dog);
System.out.println(dog.bark());
}
}

I am Han Meimei, I'm 18 years old. And I have a pet named Kitty
I can speak Chinese
My name is Kitty
miao

package com.xxx.bean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-18
* Time: 上午2:23
* To change this template use File | Settings | File Templates.
*/
public class CollectionInjectTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
American american = (American) ctx.getBean("americanA");
Pet dog = american.getPet();
System.out.println(american);
System.out.println(american.speak());
System.out.println(dog);
System.out.println(dog.bark());
System.out.println(american.getPetList());
}
}

I am Michael Johnson, I'm 28 years old. And I have a pet named Cookie
I can speak English!
My name is Cookie
Wang wang
[My name is Bobby, My name is Kitty]

自动装配配置

    <!-- 自动装配示例 -->
<!-- autowire属性表示自动装配bean中的所有属性,也可以和手动装配混合使用 -->
<!-- byName表示装配属性名与bean id相同的bean -->
<!-- byType表示装配类型与bean class类型相同的bean -->
<!-- constructor表示按照构造方法装配,装配规则与byType相同 -->
<!-- autodetect表示先使用constructor装配,如果没有与构造方法匹配的参数,在使用byType装配 -->
<!-- 当使用byType有多个符合条件的bean时,可以设置primary=true,表示有多个符合条件bean时使用此bean -->
<!-- 如果不想让某个bean成被装配,可以设置autowire-candidate=false -->
<bean id="autoChinese" class="com.xxx.bean.Chinese"
autowire="byName" >
<property name="name" value="#{chineseA.name}" />
</bean> <!-- 可以在 beans 标签中配置全局自动装配 default-autowire="byType" -->

最新文章

  1. Java中的Atomic包
  2. C++ 取得系统当前时间
  3. ASP.net状态服务器使用
  4. WCF ABC
  5. E/AndroidRuntime(1636): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.***.app.wx.MainActivity} : android.view.InflateException: Binary XML file line #51 :
  6. 从Python爬虫到SAE云和微信公众号:二、新浪SAE上搭建微信服务
  7. Codeforces 450D Jzzhu and Cities [heap优化dij]
  8. Mac下Intellij IDea发布Java Web项目详解五 开始测试
  9. date tod = boost::gregorian::day_clock::local_day(); //当前日期
  10. LeetCode OJ 153. Find Minimum in Rotated Sorted Array
  11. 深入tornado中的http1connection
  12. HTML标题
  13. 【游戏开发】基于VS2017的OpenGL开发环境搭建
  14. .Net Core 编码规范
  15. 16.ajax_case03
  16. Nginx学习笔记(反向代理&amp;搭建集群)
  17. Java程序员如何运用所掌握的技术构建一个完整的业务架构
  18. 函数的有用信息,装饰器 day12
  19. 短视频APP是如何开启你的美好生活的?
  20. egret请求参数

热门文章

  1. Go语言入门之指针的使用
  2. 开发 ASP.NET vNext 初步总结(使用Visual Studio 2015 Preview )
  3. CentOS7.6使用flatpak安装软件
  4. seat
  5. FastReport.Net使用:[9]多栏报表(多列报表)
  6. 【BZOJ 2982】 2982: combination (卢卡斯定理)
  7. JMS 基本可靠性机制 和 事务机制
  8. BZOJ4029 HEOI2015定价
  9. js冒泡处理
  10. 读书笔记_Effective_C++_条款二十六:尽可能延后变量定义式的出现时间