从头认识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的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...
随机推荐
- ES聚合底层机制-bucket深的话采用广度优先更好,而如果是年度统计还是深度优先好
见原文,仅仅摘录部分:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_preventing_combinatorial_exp ...
- hdoj--1237--简单计算器(栈模拟)
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Session原理、安全以及最基本的Express和Redis实现
Session原理.安全以及最基本的Express和Redis实现 https://segmentfault.com/a/1190000002630691
- django 笔记8 url模板 自定义函数 simple_tag
感谢alex老师~ 知识点: URL - 两个没见 url>路由系统> 函数或类 > 返回字符串 Form表单提交: 提交 >url>函数或类中的方法 -.. HttpR ...
- 11.字符,字符常见开发,_itoa函数
各种字符所占字节 wchar_t wch = L'我'; //占4个字节 char ch;//占1个字节 printf("%d\n", sizeof("A")) ...
- BZOJ 1391 网络流
vis[0]没有清零查一年- //By SiriusRen #include <cstdio> #include <cstring> #include <algorith ...
- 插入记录INSERT(二十五)
插入记录INSERT 我们先来看第一个操作:INSERT 实际上在mysql当中一共存在着3种不同的insert语句,我们先来看第一种.它的语法结构如下: 一.插入记录 INSERT [INTO] t ...
- zookeeper伪分布安装配置
1.下载路径为:http://mirrors.cnnic.cn/apache/zookeeper/stable/ 2.安装: 第一步 解压zookeeper压缩包: 进入 zookeeper安装目录 ...
- Android仿微信进度弹出框的实现方法
MainActivity: package com.ruru.dialogproject; import android.app.Activity; import android.os.Bundle; ...
- [转] -- html5手机网站自适应需要加的meta标签
webapp开发初期,会碰到在pc端开发好的页面在移动端显示过大的问题,这里需要在html head中加入meta标签来控制缩放 <meta name=" viewport" ...