从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入
这一章节我们来讨论一下注解装配的@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
从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入的更多相关文章
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(1)
这一章节我们来具体讨论一下@autowired里面的參数required. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spri ...
- 如何在 spring 中启动注解装配?
默认情况下,Spring 容器中未打开注解装配.因此,要使用基于注解装配,我们 必须通过配置 <context:annotation-config/> 元素在 Spring 配置文件 中启 ...
- 分析spring事务@Transactional注解在同一个类中的方法之间调用不生效的原因及解决方案
问题: 在Spring管理的项目中,方法A使用了Transactional注解,试图实现事务性.但当同一个class中的方法B调用方法A时,会发现方法A中的异常不再导致回滚,也即事务失效了. 当这个方 ...
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)
这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...
- 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)
这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_n ...
- 模拟实现Spring中的注解装配
本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...
- Spring MVC与注解相关的一些配置的方法
在spring mvc中,注解是需要通过配置文件去开启的,一般简单的项目可分为两个配置文件,这里姑且叫做spring-mvc.xml与spring-context.xml.其中spring-mvc.x ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- Spring方法注入的使用与实现原理
一.前言 这几天为了更详细地了解Spring,我开始阅读Spring的官方文档.说实话,之前很少阅读官方文档,就算是读,也是读别人翻译好的.但是最近由于准备春招,需要了解很多知识点的细节,网上几乎 ...
随机推荐
- jvm分析内存泄露
首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 - 导航条 - 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 ...
- CountDownLatch使用场景及分析 并发测试
原文:https://www.cnblogs.com/bqcoder/p/6089101.html CountDownLatch使用场景及分析 JDk1.5提供了一个非常有用的包,Concurre ...
- C++与Flash的交互
研究Flash嵌入游戏中的可行性....... 渲染问题已解决 事件响应已解决 下面是C++与Flash AS的交互, 以MFC为例: 1. 新建一个MFC Dialog程序 2. 添加一个Flash ...
- NSLog 输出格式集合
• %@ 对象 • %d, %i 整数 • %u 无符整形 • %f 浮点/双字 • %x, %X 二进制整数 • %o 八进制整数 • %zu size_t • %p 指针 • %e 浮 ...
- 二十四种设计模式:单例模式(Singleton Pattern)
单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...
- Qunit的使用
1.新建一个html页面,如下 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht ...
- [转载]抓包工具Charles乱码解决办法
FROM: http://blog.csdn.net/huanghanqian/article/details/52973651 安装 SSL 证书 Mac 端 首先去 http://www.char ...
- 如何使用 awk 按模式筛选文本或字符串
在筛选文本时,有时你可能想根据某个给定的条件或使用一个可被匹配的特定模式,去标记某个文件或数行字符串中的某几行.使用 awk 来完成这个任务是非常容易的,这也正是 awk 中可能对你有所帮助的几个功能 ...
- Android adb shell学习心得(四)
1.awk中的sub sub为替换函数.形式如 sub(/1/,"",$2) 将第二个变量的第一个1替换为空,若将sub替换为gsub.则替换全部的1. 2.awk中的-F 分隔符 ...
- Android常用传感器用法一览(3)
Android 开发包标准有8个传感器: Sensor.TYPE_ACCELEROMETER o 加速度计 (X, Y, Z) m/s2 Sensor.TYPE_GYROSCOPE o 陀螺仪 (X, ...