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语言俨然不算新技术,七八年前甚至更早已有很多人研习,只是没有现在流行罢了.之所以当下如此盛行,我想肯定是多 ...
随机推荐
- PHP数据结构之实现单链表
学习PHP中,学习完语法,开始尝试实现数据结构,今天实现单链表 <?php class node //节点的数据结构 { public $id; public $name; public $ne ...
- 转: 全局变量报错:UnboundLocalError: local variable 'l' referenced before assignment
http://blog.csdn.net/my2010sam/article/details/17735159
- Python的交互模式和命令行模式
Pyhton的交互模式 在终端输入Python3命令就会进入家Python的交互模式,在交互模式下,输入一行代码,回车,就会执行这行代码. Python的命令行模式 在终端输入Python3 1.py ...
- 浅析BMP位图文件结构(含Demo)
浅析BMP位图文件结构(含Demo) 作者:一点一滴的Beer http://beer.cnblogs.com/ 关于BMP位图格式在网上可以找到比较详细的相关文档,有兴趣的可以搜索标题为“BMP ...
- 感知机学习算法Java实现
感知机学习算法Java实现. Perceptron类用于实现感知机, 其中的perceptronOriginal()方法用于实现感知机学习算法的原始形式: perceptronAnother()方法用 ...
- 检测一个DLL文件是x64还是x86
对于一个DLL,我们如何判定其是32位的还是64位的,或者是any cpu的platform? Visual Studio提供了一个很好的工具:corflags,这个是内嵌到Developer Com ...
- Spring Boot 集成Swagger2生成RESTful API文档
Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...
- Java基础--ThreadLocal
Java中的ThreadLocal 可以看做以线程标识为key的Map,在多线程开发中使用非常方便. 示例 class ThreadEnv { // 用匿名内部类覆盖ThreadLocal的initi ...
- svn Can't revert without reverting children 解决方案
EMZ3.0 qrh$ svn commit -m ""svn: E155010: Commit failed (details follow):svn: E155010: '/U ...
- Regexp:目录
ylbtech-Regexp:目录 1.返回顶部 1. http://www.runoob.com/regexp/regexp-tutorial.html 2. 2.返回顶部 3.返回顶部 4 ...