注解在Spring中的运用(对象获取、对象单例/多例、值的注入、初始化/销毁方法、获取容器)
1、注解的方式获取对象
(1)导包:

(2)书写配置文件(要保证已经导入了约束):
<?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:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="pers.zhb.domain"></context:component-scan>
</beans>
配置文件的核心就一句话,它的作用是:扫描pers.zhb.domain包下的所有类的注解。
(3)创建Student对象,并在里面添加注解:
import org.springframework.stereotype.Component;
@Component("student")
public class Student {
private String snum;
private String sname;
private String sex;
private Course course;
public Student(String snum, String sname, String sex, Course course) {
this.snum = snum;
this.sname = sname;
this.sex = sex;
this.course = course;
}
public Course getCourse() {
return course;
} public void setCourse(Course course) {
this.course = course;
} public Student(){
System.out.println("Student对象创建了!");
}
public String getSnum() {
return snum;
} public void setSnum(String snum) {
this.snum = snum;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"snum='" + snum + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", course=" + course +
'}';
}
}
该注解中的参数student相当于<bean>里面的name,通过student可以获取到Student对象。
(4)创建测试类:
public class Test {
public void test1(){
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
Student student =(Student)applicationContext.getBean("student");
student.setSname("zhai");
System.out.println(student);
}
public static void main(String[] args){
Test test=new Test();
test.test1();
}
}

(5)注解的四种方式:

第二个可以用于Service层对象的创建,第三个web层,第四个dao层。这四个注解只是名字不同而已,功能是相同的。
2、对象的单例 / 多例
(1)单例对象

测试:
Student student1 =(Student)applicationContext.getBean("student");
Student student2 =(Student)applicationContext.getBean("student");
System.out.println(student1==student2);
返回结果为true,说明创建的是同一个对象。在spring容器中只存在一个bean的实例,bean以单里的形式存在
(2)多例对象

测试:
Student student1 =(Student)applicationContext.getBean("student");
Student student2 =(Student)applicationContext.getBean("student");
System.out.println(student1==student2);

返回的结果为false,创建的是两个不同的对象。每次调用getBean()的时候都会返回一个新的实例
3、值的注入
(1)在属性处赋值:

此种方式破坏了封装性,不推荐,因此set方式更优。
(2)在set方法处赋值:

(3)注入引用数据类型的数据:
方式一:
创建Course对象,并将对象引入到容器:


但是,这种方式有一个弊端,就是在有多个对象的情况下,将无法选择具体选择哪一个。
方式二:

指定注入哪一个对象。

4、引入配置文件创建Spring容器
用此方法需要导入jar包:

ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
创建容器的代码可以用注解代替:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
采用这种方式只需要在类中写一次,因此,不需要在每一个方法中都写获取容器的代码,可以减少代码量。
注解在Spring中的运用(对象获取、对象单例/多例、值的注入、初始化/销毁方法、获取容器)的更多相关文章
- Jquery获取下拉选择节点名称值赋给textbox文本框 获取 父节点的栏目名称编号
<label for="parentNode" style="float:left" >父级栏目:</label> <select ...
- request.getParameterMap() 获取表单提交的键值对 并且 也能获取动态表单的key
Map<String,String[]> map = request.getParameterMap();Set<String> keys = map.keySet(); 获取 ...
- iframe中使用模态框提交表单后,iframe加载父页面的解决方法
在iframe中使用模态框提交表单后,会出现iframe加载整个父页面的问题,如下图: 解决方法: 在form表单中添加target属性 _parent 这个属性会使目标文档载入父窗口或者包含来超链接 ...
- jQuery—获取表单标签的数据值
获取设置input标签的值 <input class="form-control" type="text" id="username" ...
- spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法
在quartz中配置了一个方法运行时会连续调用两次,是因为加载两次,只需在tomcat的server.xml中修改配置 <Host name="www.xx.cn" appB ...
- jQuery获取表单各元素的值
radio值获取 $("input[type='radio']:checked").val(); 2,设置指定的项为当前选中项 $("input[type='radio' ...
- asp.net 获取表单中控件的值
原文:https://blog.csdn.net/happymagic/article/details/8480235 C# 后台获取前台 input 文本框值.(都是以控件的Name来获取) s ...
- HTMLFormElement获取表单里面所有的值然后以json形式返回
function HTMLFormElement(){ this.init(); return this.json; } HTMLFormElement.prototype.init = functi ...
- Spring 中的事务操作、注解、以及 XML 配置
事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...
随机推荐
- 第1篇 Scrum 冲刺博客
每个成员在Alpha阶段认领的任务 钟智锋 内容 工时 客户端请求对局 1h 客户端请求移动/使用锦囊/技能/进化 1h 把对局信息转换成可传播的信息 2h 客户端通信 3h 编写博客和文档 1h 岑 ...
- Pytorch中torch.load()中出现AttributeError: Can't get attribute
原因:保存下来的模型和参数不能在没有类定义时直接使用. Pytorch使用Pickle来处理保存/加载模型,这个问题实际上是Pickle的问题,而不是Pytorch. 解决方法也非常简单,只需显式地导 ...
- Redis秒杀系统架构设计-微信抢红包
导读 前二天我写了一篇,Redis高级项目实战(点我直达),SpringBoot整合Redis附源码(点我直达),今天我们来做一下Redis秒杀系统的设计.当然啦,Redis基础知识还不过关的,先去加 ...
- 给你的Swagger文档换套附魔皮肤吧
前言 相信无论是前端或是后端的程序员对Swagger都不怎么陌生,没有用过应该也听说过 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 简 ...
- Spring Security 入门学习--数据库认证和授权
首先是使用的SpringBoot框架 基础需要的pom以来如下,基础的springboot项目的创建就不一一赘述了. <!--spring web--> <dependency> ...
- 用Python的Pandas和Matplotlib绘制股票KDJ指标线
我最近出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中给出了MACD,KDJ ...
- Combine 框架,从0到1 —— 3.使用 Subscriber 控制发布速度
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 3.使用 Subscriber 控制发布速度. 内容概览 前言 在发布者生产元素时消耗它们 使 ...
- Spring实战第4版PDF下载含源码
下载链接 扫描右侧公告中二维码,回复[spring实战]即可获取所有链接. 读者评价 看了一半后在做评论,物流速度挺快,正版行货,只是运输过程有点印记,但是想必大家和你关注内容,spring 4必之3 ...
- 力扣leetcode 435. 无重叠区间 - 贪心
非常经典的区间贪心思想 -- 详见博文: 贪心思想之区间贪心 本题给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠. 注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] ...
- vue 在模板template中变量和字符串拼接
例子: :post-action="'/api/v1/reportPage/'+this.selectedPagerId+'/saveimg/'"