Spring第五篇
在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第五篇的更多相关文章
- Spring第五篇【cglib、手动实现AOP编程】
前言 到目前为止,已经简单学习了Spring的Core模块.也会怎么与Struts2框架进行整合了-.于是我们就开启了Spring的AOP模块了-在讲解AOP模块之前,首先我们来讲解一下cglib代理 ...
- Spring第六篇---AOP
接着Spring第五篇讲 我们今天将叙述以下几个知识点 1 什么是AOP AOP 是一种思想 横向重复 纵向抽取 在软件业,AOP为Aspect Oriented Programming的缩写,意 ...
- Spring Cloud第五篇 | 服务熔断Hystrix
本文是Spring Cloud专栏的第五篇文章,了解前四篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- Spring第六篇【Spring AOP模块】
前言 Spring的第五篇也算是AOP编程的开山篇了,主要讲解了代理模式-..本博文主要讲解Spring的AOP模块:注解方式和XML方式实现AOP编程.切入点表达式.. AOP的概述 Aop: as ...
- 跟我学SpringCloud | 第五篇:熔断监控Hystrix Dashboard和Turbine
SpringCloud系列教程 | 第五篇:熔断监控Hystrix Dashboard和Turbine Springboot: 2.1.6.RELEASE SpringCloud: Greenwich ...
- EnjoyingSoft之Mule ESB开发教程系列第五篇:控制消息的流向-数据路由
目录 1. 使用场景 2. 基于消息头的路由 2.1 使用JSON提交订单的消息 2.2 使用XML提交订单的消息 2.3 使用Choice组件判断订单格式 3. 基于消息内容的路由 4. 其他控制流 ...
- 跟我学SpringCloud | 第十五篇:微服务利剑之APM平台(一)Skywalking
目录 SpringCloud系列教程 | 第十五篇:微服务利剑之APM平台(一)Skywalking 1. Skywalking概述 2. Skywalking主要功能 3. Skywalking主要 ...
- Spring Cloud第九篇 | 分布式服务跟踪Sleuth
本文是Spring Cloud专栏的第九篇文章,了解前八篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...
- 【Python五篇慢慢弹】快速上手学python
快速上手学python 作者:白宁超 2016年10月4日19:59:39 摘要:python语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...
随机推荐
- B+树和LSM存储引擎代表树和B-树
B+树和LSM比较 https://blog.csdn.net/u013928917/article/details/75912045 在关系型数据库mysql中普遍使用B+树作为索引,在实际中 ...
- OpenJudge9278:旅行
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB 描述 转眼毕业了,曾经朝夕相处的同学们不得不都各奔东西,大家都去了不同的城市开始新的生活.在各自城 ...
- 开放群组架构TOGAF
作于一个架构师尤其是企业架构师来说,丰富的理论知识可以帮助他在架构规划及管理过程中站在更高的角度去看待问题,历史发展原因有很多已成体系的架构理论,TOGAF是近年来比较接地气的,受到了政府和银行业的重 ...
- Python collections系列之可命名元组
可命名元组(namedtuple) 根据nametuple可以创建一个包含tuple所有功能以及其他功能的类 1.创建一个坐标类 import collections # 创建类, defaultd ...
- Java基础--枚举Enum
Java中的枚举是一种特殊的类,可以将一组固定常量的集合组成一种类型,使用方便且类型安全.使用enum关键字定义. enum类型父类为Enum,通过Enum.class可见Enum为抽象类,实现了Co ...
- Mybatis 一对一(OneToOne)关系映射__INSERT
今天测试Ibatis的一对一的关联映射时总是出现错误,其中很多的错误都是自己不小心写错的..现把整个Ibatis源代码记录下来,以便以后熟记: 1.数据库脚本: CREATE TABLE t_pers ...
- $route路由
<!DOCTYPE html><html ng-app="AngularApp"> <head> <meta charset=" ...
- python 函数相关定义
1.为什么要使用函数? 减少代码的冗余 2.函数先定义后使用(相当于变量一样先定义后使用) 3.函数的分类: 内置函数:python解释器自带的,直接拿来用就行了 自定义函数:根据自己的需求自己定义的 ...
- Linux系统下Oracle执行SQL脚本后中文出现乱码解决方法
先确认Oracle的字符集,sqlplus登录Oracle后执行语句: [sql] select userenv('language') from dual; 返回值例如:AMERICAN_AME ...
- IDEA中Git实战
工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...