这一章节我们来讨论一下注解装配的@autowired是如何通过set方法或者其它方法注入?

1.domain

蛋糕类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_8;

public class Cake {

	private String name = "";

	public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

厨师类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_8;

import org.springframework.beans.factory.annotation.Autowired;

public class Chief {
private Cake cake = null; @Autowired
public Chief(Cake cake) {
this.cake = cake;
} private String name = ""; public String getName() {
return name;
} public Cake makeOneCake() {
System.out.println(getName() + " make " + cake.getName());
return cake;
} public void setName(String name) {
this.name = name;
} }

这里须要注意的是,这里通过@Autowired标签在构造器方法注入cake对象

2.測试类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_8;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_8/ApplicationContext-test.xml" })
public class ChiefTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testChief() {
Chief jack = applicationContext.getBean(Chief.class);
jack.makeOneCake();
}
}

3.配置文件:(重点)

<?

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="blueberryCheeseCake"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_8.Cake"
p:name="blueberryCheeseCake" scope="prototype" /> <bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_8.Chief"
p:name="jack" />
</beans>

尽管我们上面这里定义了两个bean。可是jack那个bean不再须要通过<property/>标签来注入blueberryCheeseCake这个bean,而是通过Chief里面的@Autowired标签来完毕注入。

測试输出:

jack make blueberryCheeseCake

总结:这一章节主要介绍@autowired是如何通过构造器方法注入。

文件夹:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入的更多相关文章

  1. 从头认识Spring-2.3 注解装配-@autowired(4)-required(1)

    这一章节我们来具体讨论一下@autowired里面的參数required. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spri ...

  2. 如何在 spring 中启动注解装配?

    默认情况下,Spring 容器中未打开注解装配.因此,要使用基于注解装配,我们 必须通过配置 <context:annotation-config/> 元素在 Spring 配置文件 中启 ...

  3. 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案

    问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...

  4. 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)

    这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...

  5. 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)

    这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_n ...

  6. 模拟实现Spring中的注解装配

    本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...

  7. Spring MVC与注解相关的一些配置的方法

    在spring mvc中,注解是需要通过配置文件去开启的,一般简单的项目可分为两个配置文件,这里姑且叫做spring-mvc.xml与spring-context.xml.其中spring-mvc.x ...

  8. Spring学习笔记--使用注解装配

    使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...

  9. Spring方法注入的使用与实现原理

    一.前言   这几天为了更详细地了解Spring,我开始阅读Spring的官方文档.说实话,之前很少阅读官方文档,就算是读,也是读别人翻译好的.但是最近由于准备春招,需要了解很多知识点的细节,网上几乎 ...

随机推荐

  1. 【JSP EL】<c:if> <c:foreach >EL表达式 获取list长度/不用循环,EL在List中直接获取第一项的内容/EL获取Map的键,Map的值

    1.EL表达式 获取list长度 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  2. java常用命令行

    1.javac(编译java源文件) javac是用来编译.java文件的. 例子: package com.fjassa.domain;  public class Human.public cla ...

  3. iOS:详解MJRefresh刷新加载更多数据的第三方库

    原文链接:http://www.ios122.com/2015/08/mjrefresh/ 简介 MJRefresh这个第三方库是李明杰老师的杰作,这个框架帮助我们程序员减轻了超级多的麻烦,节约了开发 ...

  4. 同步和异步 阻塞和非阻塞 IO多路复用和select总结

    同步和异步的概念 同步是指用户线程发起IO请求后,需要等待或者轮询内核IO操作完成后才能继续执行: 异步是指用户线程发起IO请求后仍继续执行,当内核IO操作完成后会通知用户线程或者调用用户线程注册的回 ...

  5. Android 开发之 ---- bootloader (LK)

    LK是什么 LK 是 Little Kernel 它是 appsbl (Applications ARM Boot Loader)流程代码  ,little kernel 是小内核小操作系统. LK ...

  6. Add Two Numbers(from leetcode python 链表)

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  7. 如何使用 awk 的 ‘next’ 命令

    导读 在 awk 系列文章中,我们来看一下next 命令 ,它告诉 awk 跳过你所提供的所有剩下的模式和表达式,直接处理下一个输入行.next 命令帮助你阻止运行命令执行过程中多余的步骤. 要明白它 ...

  8. C# 中的"yield"与 "yield break"使用

    yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能.举例说明 usi ...

  9. [ES6] 07. Default Value for function param

    Normally, we can set default value for function param: //Here use "Hello" as default param ...

  10. jQuery选取表单元素

    表单元素选择器 选择器                    说明 :button                 <button>元素和type属性值为button的<input& ...