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语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...
随机推荐
- 树莓派(Linux)与镜像源
树莓派学习笔记--修改树莓派软件源 1. linux 镜像源文件 >> vim /etc/apt/sources.list 可在树莓派官网 http://www.raspbian.org/ ...
- mysql索引攻略
本设计和优化专题转自博客园的Mysql的设计和优化专题 Explain优化查询检测 所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后 ...
- RabbitMQ和Kafka可靠性
RabbitMQ和Kafka可靠性 https://www.cnblogs.com/haolujun/p/9641840.html 我们通过前文知道,RabbitMQ的队列分为master queue ...
- LeetCode Target Sum
原题链接在这里:https://leetcode.com/problems/target-sum/description/ 题目: You are given a list of non-negati ...
- 爬虫利器 Puppeteer
http://wintersmilesb101.online/2017/03/24/use-phantomjs-dynamic/ 一起学爬虫 Node.js 爬虫篇(三)使用 PhantomJS ...
- oracle获得当前时间,精确到毫秒并指定精确位数
oracle获得当前时间的,精确到毫秒 可以指定精确豪秒的位数 select to_char(systimestamp, 'yyyymmdd hh24:mi:ss.ff ') from dual; ...
- Dubbo与Zookeeper
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...
- CEF源码编译
CEF的构造说明:https://bitbucket.org/chromiumembedded/cef/wiki/BranchesAndBuilding chromium的源码地址:https://c ...
- clone对象的克隆
用一句简单的话来说就是浅拷贝,只是对指针的拷贝,拷贝后两个指针指向同一个内存空间,深拷贝不但对指针进行拷贝,而且对指针指向的内容进行拷贝,经深拷贝后的指针是指向两个不同地址的指针. 等多 http:/ ...
- jdk中那些常见的类不能被继承的
对于java中的类,如果是使用final修饰的话,那么这个类就不能够被继承,因为jdk的开发者认为,有一些最基本的类没要必要对开发者开放,如果用户 继承时操作有误,很可能引入很多问题.为了防止用户对基 ...