Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
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" -->
Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配的更多相关文章
- Spring 学习笔记(五)—— Bean之间的关系、作用域、自动装配
继承 Spring提供了配置信息的继承机制,可以通过为<bean>元素指定parent值重用已有的<bean>元素的配置信息. <?xml version="1 ...
- spring的基于XML方式的属性注入
1.掌握spring的属性注入的方法: 1.1构造方法注入普通值---------<constructor-arg>标签的使用 首先新建一个类 package spring.day1.de ...
- Spring(3.2.3) - Beans(2): 属性注入 & 构造注入
依赖注入是指程序运行过程中们如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...
- Spring - bean的autowire属性(自动装配)
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式.但是对于大型项目,假设有一个bean A被多个bean引用注 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring Bean的配置及常用属性
作为 Spring 核心机制的依赖注入,改变了传统的编程习惯,对组件的实例化不再由应用程序完成,转而交由 Spring 容器完成,在需要时注入应用程序中,从而对组件之间依赖关系进行了解耦.这一切都离不 ...
- Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析
一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
Person类中的各种属性写法如下: package com.swift.person; import java.util.Arrays; import java.util.List; import ...
- spring框架对于实体类复杂属性注入xml文件的配置
spring框架是javaWeb项目中至关重要的一个框架,大多web 项目在工作层次上分为持久层.服务层.控制层.持久层(dao.mapper)用于连接数据库,完成项目与数据库中数据的传递:服务层(s ...
- 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式
Spring的属性注入: 构造方法的属性注入 set方法的属性注入
随机推荐
- oracle 12C安装问题
1. 先弄好c$ share的问题 2. 测试一下 c$ share 是否成功. 方法是在cmd里打net use \\localhost\c$ 失败会是这样子...: 系统错误53 The ne ...
- AOSP编译
AOSP编译 1 编译环境Win10系统安装VMplayer14,主机16G内存,I3-4170 ,虚拟机ubuntu-16.04.1-desktop-amd64,12G内存. 2 软件安装sudo ...
- JQuery中$.ajax()方法参数详解 ASP.NET jquery ajax传递参数
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- Java 深入浅出String
String String是一个被final修饰的类,直接继承于Object,同时也实现了charsequence接口,String被声明为final也就不可以被继承了.由于String的方法比较多, ...
- 洛谷P2704 [NOI2001]炮兵阵地 [状压DP]
题目传送门 炮兵阵地 题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图 ...
- python笔记五:IO与文件
1.python IO: Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘: 1)raw_input([prompt]) 函数从标准输入读取一个行,并返回一个字符串 2 ...
- XV6操作系统代码阅读心得(一):启动加载、中断与系统调用
XV6操作系统是MIT 6.828课程中使用的教学操作系统,是在现代硬件上对Unix V6系统的重写.XV6总共只有一万多行,非常适合初学者用于学习和实践操作系统相关知识. MIT 6.828的课程网 ...
- 深度理解python中的元类
本文转自:(英文版)https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python (翻译版) http:// ...
- BZOJ 4566 JZYZOJ 1547 [haoi2016T5]找相同子串 后缀数组 并查集
http://172.20.6.3/Problem_Show.asp?id=1547 http://www.lydsy.com/JudgeOnline/problem.php?id=4566 单纯后缀 ...
- JZYZOJ1622 [usaco2009]工作安排 贪心
和p1123智力大冲浪一样,可以用优先队列写... 每一秒可以做一个工作....因为n个任务只要在限制之前完成就行,所以时间不冲突的话肯定越早做完越好..所以最多的时间是n,当然限定的完成时间中最 ...