从头认识Spring-2.4 基于java的标准注解装配[email protected](3)-通过构造器方法注入

这一章节我们来讨论一下基于java的标准注解装配标签@Inject是怎样通过通过构造器方法注入?

在使用@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_15;

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_15;

import javax.inject.Inject;

public class Chief {
	private Cake cake = null;

	@Inject
	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;
	}

}

这里需要注意的是,虽然我们的cake属性域是赋值为null,但是当spring容器启动时,通过@Inject标签在构造器方法注入cake对象

2.测试类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_15;

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_15/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_15.Cake"
		p:name="blueberryCheeseCake" scope="prototype" />

	<bean id="jack"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_15.Chief"
		p:name="jack" />
</beans>

测试输出:

jack make blueberryCheeseCake

总结:这一章节主要介绍基于java的标准注解装配标签@Inject是怎样通过构造器方法注入。

目录:http://blog.csdn.net/raylee2007/article/details/50611627

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

时间: 2024-08-06 11:58:45

从头认识Spring-2.4 基于java的标准注解装配[email protected](3)-通过构造器方法注入的相关文章

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected](2)-通过set方法或者其它方法注入

这一章节我们来讨论一下基于java的标准注解装配标签@Inject是如何通过通过set方法或者其它方法注入? 在使用@Inject标签之前.我们须要在pom文件中面增加以下的代码: <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected]限定器@Named

这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_16; import javax.inject.Named; @Named("myCake") public class Cake { private String name = ""; public String getName(

从头认识Spring-2.3 注解装配[email&#160;protected](4)-required(1)

这一章节我们来详细讨论一下@autowired里面的参数required. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name) { this

从头认识Spring-2.3 注解装配[email&#160;protected](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

从头认识Spring-2.3 注解装配[email&#160;protected](5)-限定器@Qualifier(1)

这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name

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

基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. [email protected] & @Bean的配合 @Configuration注解,就是说明该类相当于一个spring的xml文件 @Bean, 类似于spring的xml文件中<bean></bean>的部分,但是必须注解在return一个实例的方法上. [email protected] 的name属性,定义bean的Id 默认名称是方法名,图中的'foo

java.lang.IllegalArgumentException: Requested window [email&#160;protected] 异常处理

晕死的错误,改了半天也没想到是这样的原因,基础正要呀... 先看一下警告信息: 07-07 08:32:19.540: WARN/WindowManager(74): Failed looking up window07-07 08:32:19.540: WARN/WindowManager(74): java.lang.IllegalArgumentException:Requested window [email protected] does not exist07-07 08:32:19

【spring cloud】spring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法

spring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法 在spring boot1.x下,使用注解@EnableFeignClients,jar包依赖是: <!-- feign远程调用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</

Java注解用法[email&#160;protected]

Java注解用法[email protected] 参考地址:https://www.cnblogs.com/perfei456/p/8962167.html 1.注解目标 通过 @SuppressWarnings的源码可知,其注解目标为类.字段.函数.函数入参.构造函数和函数的局部变量.建议注解应声明在最接近警告发生的位置 2.抑止警告的关键字 3.代码示例 示例1--抑制单类型的警告: 1 @SuppressWarnings("unchecked") 2 public void a