这一章节我们来讨论一下基于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

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

从头认识Spring-2.4 基于java的标准注解装配-@Inject(2)-通过set方法或者其它方法注入的更多相关文章

  1. 从头认识Spring-2.4 基于java的标准注解装配-@Inject-限定器@Named

    这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_s ...

  2. Spring @Bean注解 (基于java的容器注解)

    基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...

  3. Spring IOC之 使用JSR 330标准注解

    从Spring 3.0开始,Spring提供了对 JSR 330标准注解的支持.这些注解可以喝Spring注解一样被扫描到.你只需要将相关的Jar包加入到你的classpath中即可. 注意:如果你使 ...

  4. Spring IOC之基于JAVA的配置

    基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...

  5. Spring(八)之基于Java配置

    基于 Java 的配置 到目前为止,你已经看到如何使用 XML 配置文件来配置 Spring bean.如果你熟悉使用 XML 配置,那么我会说,不需要再学习如何进行基于 Java 的配置是,因为你要 ...

  6. Spring基于Java的JSR-250注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-jsr250-annot ...

  7. Spring MVC 5 + Thymeleaf 基于Java配置和注解配置

    Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...

  8. Spring学习十一----------Bean的配置之基于Java的容器注解@Bean

    © 版权声明:本文为博主原创文章,转载请注明出处 @Bean -@Bean标识一个用于配置和初始化一个由SpringIOC容器管理的新对象的方法,类似于XML配置文件的<bean/> -可 ...

  9. 【Java】利用注解和反射实现一个"低配版"的依赖注入

    在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...

随机推荐

  1. hdoj--5562--Clarke and food(模拟)

    Clarke and food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. AppManager类,管理Activity和App

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); / ...

  3. Xor Sum 2(位运算)

    D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...

  4. hibernate 不与数据库对应的注解

    @Transient          此注解必须写到get方法上

  5. caioj 1078 动态规划入门(非常规DP2:不重叠线段)(状态定义问题)

    我一开始想的是前i个区间的最大值 显然对于当前的区间,有不选和选两种情况 如果不选的话,就继承f[i-1] 如果选的话,找离当前区间最近的区间取最优 f[i] = max(f[i-1, f[j] + ...

  6. 读 Paxos 到 ZooKeeper ¥ 50大洋

    一  初识 ZooKeeper              高效且可靠的分布式协调服务.解决分布式一致性问题             统一命名服务.配置管理服务.分布式锁服务.      使用: 比如配 ...

  7. 数据迁移工具kettle简单上手

    近期做了不少数据迁移工作,无一例外都是kettle做的,对于这些工具,我认为.够用就好,不用做特别多的研究(当然.除非你是这款工具的忠实粉丝,我相信这种没几个).kettle也不例外.在我看来就是不同 ...

  8. volatile的含义

    从词面上来讲.volatile的意思是易变的,也就是说.在程序执行的过程中,有一些变量可能会被莫名其妙的改变,而优化器为了节约时间.有时候不会重读这个变量的真实值,而是去读在寄存器的备份,这种话,这个 ...

  9. 七牛用户搭建c# sdk的图文讲解

    Qiniu 七牛问题解答 问题描写叙述:非常多客户属于小白类型. 可是请不要随便喷七牛的文档站.由于须要一点http的专业知识才干了解七牛的api文档.如今我给大家弄个c# sdk的搭建步骤 问题解决 ...

  10. nios sgdma(Scatter-Gather dma)示例

    在 Quartus7.2之后的版本中,除了原有的基于avalon-mm总线的DMA之外,还增加了Scatter-Gather DMA这种基于avalon-ST流总线的DMA IP核,它更适合与大量数据 ...