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
|
@Test public 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 ...
随机推荐
- SSH整合不错的博客
https://blog.csdn.net/struggling_rong/article/details/63153833?locationNum=9&fps=1 好好看看看哦
- 关于Log4Net的使用和配置
1. 添加log4net.dll引用 2.在添加引用的那层的 AssemblyInfo.cs 注册 : [assembly: log4net.Config.XmlConfigura ...
- nginx 代理服务器配置双向证书验证
生成证书链 用脚本生成一个根证书, 一个中间证书(intermediate), 三个客户端证书. 脚本来源于(有修改)https://stackoverflow.com/que... 中间证书的域名为 ...
- 20145328 《Java程序设计》第4周学习总结
20145328 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 继承也符合DRY(Don't Repeat Yourself)原则 ISA与O ...
- LeetCode (226):Invert Binary Tree 递归实现
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- react-native中使用自定义的字体图标iconfont
iconfont图标库下载 可在 http://www.iconfont.cn 下载 下载完成后的目录中有字体文件: iconfont.ttf 拷贝字体文件 Android: 在 Android/ap ...
- POJ_1159 Palindrome (线性动态规划+滚动数组)
题意是说,给定一个字符串,问至少还需要插入多少个字符才能使得该字符串成为回文字符串. 这道题一开始做的时候用了一个简单的动态规划,开了一个5000*5000的数组,用递归形式实现,代码如下: 其中d[ ...
- SEM关键词的三种分类方式
关键词分类是为了使sem账户搭建结构清晰便于管理关键词.基于对需求人群的深入分析,每个账户都有其独特的分类方式,比如招商加盟行业更多的是地域分类,品牌类企业通常用词性分类即可,而冷门行业用人群分类比较 ...
- CentOS 7 Nginx安装配置
1.添加Nginx源 yum install epel-release 2.安装Nginx yum install nginx 3.启动Nginx systemctl start nginx //配置 ...
- 新一代调试王者Console
随着JS在Web前端中能做的事情越来越多,责任越来越大,而地位也越来越重要.传统的alert调试方式已经渐渐不能满足前端开发的种种场景.而且alert调试方式弹出的调试信息,那个窗口着实不太美观,而且 ...