SpringAnnotation注解之@PreDestroy,@PostConstruct,@Scope
@Scope的使用很简单,直接在类上加上就行
@PostConstruct:相当于xml配置方式的init-method方法
@PreDestroy:相当于xml配置方式的destroy-method方法
例如:userService
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.fz.annotation.service;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resource;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import com.fz.annotation.dao.UserDao;import com.fz.xml.entity.User;@Service("userService")@Scope("prototype")public class UserService { private UserDao userDao; public void userAdd(){ User user = new User(); user.setUsername("zhangsan"); user.setPassword("zhangsan"); userDao.userAdd(user); } public UserDao getUserDao() { return userDao; } @Resource(name="userDaoImpl") public void setUserDao(UserDao userDao) { this.userDao = userDao; } @PostConstruct public void init(){ System.out.println("init..."); } @PreDestroy public void destroy(){ System.out.println("destroy..."); }} |
applicationContext.xml配置很简单:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version="1.0" encoding="UTF-8"?> xsi:schemaLocation="http://www.springframework.org/schema/beans <context:annotation-config/> <context:component-scan base-package="com.fz.annotation"></context:component-scan> </beans> |
测试代码:
|
1
2
3
4
5
6
7
8
9
|
@Testpublic void getProperties(){ ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_annotation.xml"); UserService userService = (UserService) ctx.getBean("userService"); UserService userService1 = (UserService) ctx.getBean("userService"); userService.userAdd(); System.out.println(userService == userService1); ctx.destroy();} |
结果如下:

可以看出init还是执行了两次,两个对象也不是同一个对象,但是destroy同样还是不执行了。。。。
SpringAnnotation注解之@PreDestroy,@PostConstruct,@Scope的更多相关文章
- 【String注解驱动开发】你了解@PostConstruct注解和@PreDestroy注解吗?
写在前面 在之前的文章中,我们介绍了如何使用@Bean注解指定初始化和销毁的方法,小伙伴们可以参见<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!& ...
- Spring 学习——Spring JSR注解——@Resoure、@PostConstruct、@PreDestroy、@Inject、@Named
JSR 定义:JSR是Java Specification Requests的缩写,意思是Java 规范提案.是指向JCP(Java Community Process)提出新增一个标准化技术规范的正 ...
- SpringAnnotation注解之@Component,@Repository,@Service,@Controller
@Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...
- SpringAnnotation注解之@Autowired
@Autowired:自动装配,不用在bean里写<property>属性来指定所依赖的属性 1 2 3 4 @Autowired public void setUserDao(UserD ...
- SpringAnnotation注解之@Resource
@Resource:同样也是注入,默认是按byName,byName找不到的话按byType 1 2 3 4 @Resource public void setUserDao(UserDao user ...
- 品Spring:对@PostConstruct和@PreDestroy注解的处理方法
在bean的实例化过程中,也会用到一系列的相关注解. 如@PostConstruct和@PreDestroy用来标记初始化和销毁方法. 平常更多的是侧重于应用,很少会有人去了解它背后发生的事情. 今天 ...
- Spring@PostConstruct和@PreDestroy注解详解
@PostConstruct注解使用 @PostConstructApi使用说明 The PostConstruct annotation is used on a method that needs ...
- Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例
实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...
- 14、生命周期-@PostConstruct&@PreDestroy
14.生命周期-@PostConstruct&@PreDestroy @PostConstruct 在Bean创建完并且属性值赋值完执行 package javax.annotation; i ...
随机推荐
- Spring MVC 复习笔记03
1. @RequestMapping 1). url映射 定义controller方法对应的url,进行处理器映射使用. 2). 窄化请求映射 3). 限制http请求方法 出于安全性考虑,对htt ...
- centos7 安装 gitolite (git服务器)
gitolite简介 轻量级git服务器程序,解决了git权限管理的问题.(git是一个分布式版本控制系统,就是说每个人作为客户端的同时又是服务器)项目GitHub地址:https://github. ...
- 关于axios
简介 axios是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中 主要是用于向后台发起请求的,还有在请求中做更多是可控功能. 特点 从浏览器中创建 XMLHttpRe ...
- python之yield
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author:wadeson '''def foo(): print("-------------- ...
- 20145302张薇《Java程序设计》实验五报告
20145302张薇 实验五:Java网络编程及安全 实验内容 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验要求 基于Java Socket实现安全传输 基于TCP实现 ...
- Jquery8 基础事件
学习要点: 1.绑定事件 2.简写事件 3.复合事件 JavaScript 有一个非常重要的功能,就是事件驱动.当页面完全加载后,用户通过鼠标或键盘触发页面中绑定事件的元素即可触发.jQuery 为开 ...
- excel日期插件
效果图 Private Sub DTPicker1_Click() ActiveCell.Value = DTPicker1.Value DTPicker1.Visible = False End S ...
- ASP.NET Web API Claims Authorization with ASP.NET Identity 2.1 Part 5 (by TAISEER)
https://www.cnblogs.com/KimmyLee/p/6430474.html https://www.cnblogs.com/rocketRobin/p/9077523.html h ...
- 6.scala中的包
版权申明:转载请注明出处. 文章来源:http://bigdataer.net/?p=287 排版乱?请移步原文获得更好的阅读体验 1.基础特性 scala中的包和java中的包类似,都是用来在大型工 ...
- HDU 3594 Cactus(仙人掌问题)
http://acm.hdu.edu.cn/showproblem.php?pid=3594 题意: 一个有向图,判断是否强连通和每条边只在一个环中. 思路: 仙人掌问题. 用Tarjan算法判断强连 ...