从头认识Spring-2.4 基于java的标准注解装配-@Inject(2)-通过set方法或者其它方法注入
这一章节我们来讨论一下基于java的标准注解装配标签@Inject是如何通过通过set方法或者其它方法注入?
在使用@Inject标签之前。我们须要在pom文件中面增加以下的代码:
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
上面是j2ee里面标准的inject标签依赖。
1.domain
蛋糕类:(不变)
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_14;
public class Cake {
private String name = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
厨师类:
通过set方法注入
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_14;
import javax.inject.Inject;
public class Chief {
private Cake cake = null;
public Cake getCake() {
return cake;
}
@Inject
public void setCake(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;
}
}
通过其它方法注入:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_14;
import javax.inject.Inject;
public class Chief {
private Cake cake = null;
public Cake getCake() {
return cake;
}
@Inject
public void injectCake(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;
}
}
这里须要注意的是。尽管我们的cake属性域是赋值为null。可是当spring容器启动时,通过@Inject标签在set方法或者其它方法的地方注入cake对象
2.測试类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_14; 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_14/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_14.Cake"
p:name="blueberryCheeseCake" scope="prototype" /> <bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_14.Chief"
p:name="jack" />
</beans>
測试输出:
jack make blueberryCheeseCake
总结:这一章节主要介绍基于java的标准注解装配标签@Inject是如何通过set方法或者其它方法注入。
文件夹:http://blog.csdn.net/raylee2007/article/details/50611627
从头认识Spring-2.4 基于java的标准注解装配-@Inject(2)-通过set方法或者其它方法注入的更多相关文章
- 从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named
这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_s ...
- Spring @Bean注解 (基于java的容器注解)
基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...
- Spring IOC之 使用JSR 330标准注解
从Spring 3.0开始,Spring提供了对 JSR 330标准注解的支持.这些注解可以喝Spring注解一样被扫描到.你只需要将相关的Jar包加入到你的classpath中即可. 注意:如果你使 ...
- Spring IOC之基于JAVA的配置
基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...
- Spring(八)之基于Java配置
基于 Java 的配置 到目前为止,你已经看到如何使用 XML 配置文件来配置 Spring bean.如果你熟悉使用 XML 配置,那么我会说,不需要再学习如何进行基于 Java 的配置是,因为你要 ...
- Spring基于Java的JSR-250注解
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-jsr250-annot ...
- Spring MVC 5 + Thymeleaf 基于Java配置和注解配置
Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...
- Spring学习十一----------Bean的配置之基于Java的容器注解@Bean
© 版权声明:本文为博主原创文章,转载请注明出处 @Bean -@Bean标识一个用于配置和初始化一个由SpringIOC容器管理的新对象的方法,类似于XML配置文件的<bean/> -可 ...
- 【Java】利用注解和反射实现一个"低配版"的依赖注入
在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...
随机推荐
- poj--3620--Avoid The Lakes(dfs)
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Subm ...
- poj2823/hdu3415 - 数据结构 单调队列
poj2823 题目链接 长度为N的数组,求宽度k的滑动窗口在数组上滑动时窗口内的最大值或最小值 如果用单调队列做,求最小值时,队列应该严格递增的.所以插入时,队尾大于等于插入值的元素都应被舍弃,因为 ...
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest(第六场)
A Plague Inc Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants ...
- [USACO08DEC]拍头Patting Heads 水题
类似素数筛,暴力可过,不需要太多的优化 Code: #include<cstdio> #include<algorithm> #include<string> us ...
- ArchLinux 音乐播放客户端ncmpcpp和服务端mpd的配置
Ncmcpp是一个mpd客户端,它提供了很多方便的操作 MPD是一个服务器-客户端架构的音频播放器.功能包括音频播放, 播放列表管理和音乐库维护,所有功能占用的资源都很少. --取自 wiki.arc ...
- java编程思想--学习心得
学习Java编程思想,需要了解语言特性,对于各种名词,能够借助项目代码,解释其含义,不借助搜索工具,明白其在什么样场景下使用,会带来什么样的问题,能否避免这类问题. 学习的过程,与软件开发相同,一样是 ...
- python note #1
To record my process of studying python and to practice my English meanwhile, I'd like to start writ ...
- ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题
原文:ASP.NET WEB API微信支付通知接口,返回xml数据,微信服务器不识别问题 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/MrTra ...
- 题解 P1179 【数字统计】
嚯嚯嚯,这道题很显然是削弱版的51nod P1042. 那么显然我们需要使用数位DP解题. 思路大致是这样的: 对于每一个数字,考虑三种影响关系: 1. 它对低位的影响 2. 它对高位的影响 3. 高 ...
- 以Append方式打开文件,设置偏移量无效
#include<stdio.h> int main() { FILE * fd = fopen("btoo1.c", "ab+"); fpos_t ...