在Spring第四篇中 我们主要介绍了set get的注入方式

在Spring第五篇中 我们主要介绍使用注解配置Spring 主要分为两个步骤

1 导包的同时引入新得约束 导包如下

1.1 重写注解代理配置文件 代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
<context:component-scan base-package="cn.lijun.bean"></context:component-scan>
</beans>

1.2 在上一篇的基础上 建立bean包 并且建立User和Phoe两个类,并且生成相关的get set 方法

1.3 将对象注册到容器

代码如下

@Component("user")    同时为了便于开发  也有@Service("user")   @Controller("user")    @Repository("user")

1.4 Demo测试类

代码如下

package cn.lijun.Demo;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.lijun.bean.User;

public class Demo {
@Test
public void fun1(){
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User)ac.getBean("user");
User u1 = (User)ac.getBean("user");
System.out.println(u1==u);
}
}

2 值类型注入

通过set方法赋值  代码如下

package cn.lijun.bean;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("user")
@Scope(scopeName="singleton")
public class User {
private String name;
private Integer age;
@Resource(name="phone")
private Phoe phone;
public String getName() {
return name;
}
@Value("lijun")
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Phoe getPhone() {
return phone;
}
public void setPhone(Phoe phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", phone=" + phone + "]";
}

}

package cn.lijun.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("phone")
public class Phoe {
private String name;
private String color;
public String getName() {
return name;
}
@Value("小米9")
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
@Value("珀金黑")
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Phoe [name=" + name + ", color=" + color + "]";
}

}

package cn.lijun.Demo;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.lijun.bean.User;

public class Demo {
@Test
public void fun1(){
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User)ac.getBean("user");
//User u1 = (User)ac.getBean("user");
System.out.println(u);
//System.out.println(u1==u);
}
}

运行结果如下

User [name=lijun, age=null, phone=Phoe [name=小米9, color=珀金黑]]

注意 当给引用类型赋值时 需要先把该引用类型交给spring管理,如上面例子中Phone类 需要先@Component("phone")
然后在User中指定    @Resource(name="phone")   在Phone类中再进行赋值。

Spring第五篇的更多相关文章

  1. Spring第五篇【cglib、手动实现AOP编程】

    前言 到目前为止,已经简单学习了Spring的Core模块.也会怎么与Struts2框架进行整合了-.于是我们就开启了Spring的AOP模块了-在讲解AOP模块之前,首先我们来讲解一下cglib代理 ...

  2. Spring第六篇---AOP

    接着Spring第五篇讲 我们今天将叙述以下几个知识点 1 什么是AOP AOP 是一种思想  横向重复  纵向抽取 在软件业,AOP为Aspect Oriented Programming的缩写,意 ...

  3. Spring Cloud第五篇 | 服务熔断Hystrix

    ​ 本文是Spring Cloud专栏的第五篇文章,了解前四篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  4. Spring第六篇【Spring AOP模块】

    前言 Spring的第五篇也算是AOP编程的开山篇了,主要讲解了代理模式-..本博文主要讲解Spring的AOP模块:注解方式和XML方式实现AOP编程.切入点表达式.. AOP的概述 Aop: as ...

  5. 跟我学SpringCloud | 第五篇:熔断监控Hystrix Dashboard和Turbine

    SpringCloud系列教程 | 第五篇:熔断监控Hystrix Dashboard和Turbine Springboot: 2.1.6.RELEASE SpringCloud: Greenwich ...

  6. EnjoyingSoft之Mule ESB开发教程系列第五篇:控制消息的流向-数据路由

    目录 1. 使用场景 2. 基于消息头的路由 2.1 使用JSON提交订单的消息 2.2 使用XML提交订单的消息 2.3 使用Choice组件判断订单格式 3. 基于消息内容的路由 4. 其他控制流 ...

  7. 跟我学SpringCloud | 第十五篇:微服务利剑之APM平台(一)Skywalking

    目录 SpringCloud系列教程 | 第十五篇:微服务利剑之APM平台(一)Skywalking 1. Skywalking概述 2. Skywalking主要功能 3. Skywalking主要 ...

  8. Spring Cloud第九篇 | 分布式服务跟踪Sleuth

    ​ ​本文是Spring Cloud专栏的第九篇文章,了解前八篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  9. 【Python五篇慢慢弹】快速上手学python

    快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...

随机推荐

  1. 使用PHP配置文件

    /**************************************************************************************** * 使用PHP配置文 ...

  2. sklearn实践_普通线性回归

    import pandas as pd import numpy as np import matplotlib.pyplot as plt data = pd.read_csv(r"C:\ ...

  3. docker容器与宿主机之间内容拷贝

    来自:http://blog.csdn.net/yangzhenping/article/details/43667785 常用的方式有3种: 从容器内拷贝文件到主机上 docker cp <c ...

  4. BZOJ3075,LG3082 [USACO13MAR]项链Necklace

    题意 Bessie the cow has arranged a string of N rocks, each containing a single letter of the alphabet, ...

  5. GO语言list剖析

    GO语言list剖析 本节内容 使用方法 list提供的方法 源码剖析 1. 使用方法 在GO语言的标准库中,提供了一个container包,这个包中提供了三种数据类型,就是heap,list和rin ...

  6. GO语言heap剖析及利用heap实现优先级队列

    GO语言heap剖析 本节内容 heap使用 heap提供的方法 heap源码剖析 利用heap实现优先级队列 1. heap使用 在go语言的标准库container中,实现了三中数据类型:heap ...

  7. centos安装yum源

    网易(163)yum源是国内最好的yum源之一 ,无论是速度还是软件版本,都非常的不错,将yum源设置为163yum,可以提升软件包安装和更新的速度,同时避免一些常见软件版本无法找到.具体设置方法如下 ...

  8. YARN的ACL

    修改完了资源池的权限之后,发现无法查看日志了.报错: User [dr.who] is not authorized to view the logs for... 即使把资源池的权限设置为了*(所有 ...

  9. BZOJ1370:[Baltic2003]团伙

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...

  10. Zabbix通过SNMPv2监控DELL服务器的硬件信息

    (一)zabbix监控DELL服务器 (1)简述:监控DELL服务器硬件一般有两种途径:1.操作系统上安装OMSA,编写脚本调用omreport命令进行监控(需要在操作系统上安装比较麻烦):2.使用i ...