注解在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 ...
随机推荐
- Solr的原理及使用
1.Solr的简介Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过 ...
- DNSPod 修改NS 服务器?
其实我几乎没在国内注册过域名,更没想过用国内的DNS 服务,DNSPod 也是属于听说过名字的地步而已,但是正好在腾讯云注册了一个cn 域名,又觉得对DNSPod 的DNS 服务不是特别满意,所以想把 ...
- 阿里面试竟如此轻松,2招带你过关斩将拿下offer
在找工作之前首先是要认清一个问题,虽然这个问题比较俗,但是很现实,就是为什么追求高工资? 这个问题我想不用说大家心里也清楚.大部分人都不是当前城市的本地人,说好听了叫来上班,说的不好听其实叫“外来务工 ...
- Less 预处理笔记
1. less 简介 1. less是CSS的预编译器,可以扩展CSS语言(当然也兼容CSS),可以定义变量.混合.函数等等,让CSS代码更易维护和扩展 2. less与传统写法相比: less后缀为 ...
- react 有没有类似vue中watch这样的api?
就是 当组件里state 里的数据发生变化可以监听到这个数据的变化 当数据发生变化的时候做一些事情 比如ajax请求 ?初学react 用vue的时候会用watch 和computed 去监听数据发生 ...
- Python采集CSDN博客排行榜数据
文章目录 前言 网络爬虫 搜索引擎 爬虫应用 谨防违法 爬虫实战 网页分析 编写代码 运行效果 反爬技术 前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知 ...
- 挂载磁盘不成功显示mount: /mnt: wrong fs type, bad option, bad superblock..............
[23:25:32 root@8 ~]#mount /dev/sdb2 /mntmount: /mnt: wrong fs type, bad option, bad superblock on /d ...
- B - The Staircases (dp)
One curious child has a set of N little bricks. From these bricks he builds different staircases. St ...
- 在Apache服务器上安装SSL证书
参考:链接 前提条件 1.您的Apache服务器上已经开启了443端口(HTTPS服务的默认端口) // 开通443端口 firewall-cmd --zone=public --add-port=4 ...
- 【小程序】---- 封装Echarts公共组件,遍历图表实现多个饼图
一.问题描述: 在小程序的项目中,封装公共的饼图组件,并在需要的页面引入使用.要求一个页面中有多个饼图,动态渲染不同的数据. 二.效果实现: 1. 查看——小程序使用Echarts的方式 2. 封装饼 ...