@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的更多相关文章

  1. 【String注解驱动开发】你了解@PostConstruct注解和@PreDestroy注解吗?

    写在前面 在之前的文章中,我们介绍了如何使用@Bean注解指定初始化和销毁的方法,小伙伴们可以参见<[Spring注解驱动开发]如何使用@Bean注解指定初始化和销毁的方法?看这一篇就够了!!& ...

  2. Spring 学习——Spring JSR注解——@Resoure、@PostConstruct、@PreDestroy、@Inject、@Named

    JSR 定义:JSR是Java Specification Requests的缩写,意思是Java 规范提案.是指向JCP(Java Community Process)提出新增一个标准化技术规范的正 ...

  3. SpringAnnotation注解之@Component,@Repository,@Service,@Controller

    @Component:组件,表示此写上了此注解的bean,作为一个组件存在于容器中.这样的话别的地方就可以使用@Resource这个注解来把这个组件作为一个资源来使用了.初始化bean的名字为类名首字 ...

  4. SpringAnnotation注解之@Autowired

    @Autowired:自动装配,不用在bean里写<property>属性来指定所依赖的属性 1 2 3 4 @Autowired public void setUserDao(UserD ...

  5. SpringAnnotation注解之@Resource

    @Resource:同样也是注入,默认是按byName,byName找不到的话按byType 1 2 3 4 @Resource public void setUserDao(UserDao user ...

  6. 品Spring:对@PostConstruct和@PreDestroy注解的处理方法

    在bean的实例化过程中,也会用到一系列的相关注解. 如@PostConstruct和@PreDestroy用来标记初始化和销毁方法. 平常更多的是侧重于应用,很少会有人去了解它背后发生的事情. 今天 ...

  7. Spring@PostConstruct和@PreDestroy注解详解

    @PostConstruct注解使用 @PostConstructApi使用说明 The PostConstruct annotation is used on a method that needs ...

  8. Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

  9. 14、生命周期-@PostConstruct&@PreDestroy

    14.生命周期-@PostConstruct&@PreDestroy @PostConstruct 在Bean创建完并且属性值赋值完执行 package javax.annotation; i ...

随机推荐

  1. 此博客可能不再更新,往后博文将发布在 GitHub 中

    在 GitHub 上, 可以建立不同的仓库,显示分类可以更明确: 有不同分支,可以打很多次草稿: 用 markdown 语法来书写比较舒服(博客园也可以设置): 最主要的是 GitHub 装逼呀!!! ...

  2. Python学习笔记之Centos6.9安装Python3.6

    0x00 注意 如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!!!! 不要动现有的python2环 ...

  3. Linux学习笔记之Xshell配色方案定制

    点击 Xshell 面板顶部的如下按钮. 点击 Browse 按钮,弹出如下面板,选择 ANSI Colors on Black,然后点击右侧save as 按钮,命名为 zkl.   这里其实就是复 ...

  4. 《Java程序设计》实验1实验报告

    20145318 <Java程序设计>实验1实验报告 实验题目 通过对500个数据进行操作,实现快速排序.选择排序.直接插入排序算法时间复杂度的比较:并在排序数据中快速查找某一数据,给出查 ...

  5. Spring Security核心概念介绍

    Spring Security是一个强大的java应用安全管理库,特别适合用作后台管理系统.这个库涉及的模块和概念有一定的复杂度,而大家平时学习Spring的时候也不会涉及:这里基于官方的参考文档,把 ...

  6. The OAuth 2.0 Authorization Framework: Bearer Token Usage

    https://tools.ietf.org/html/rfc6750 1.2. Terminology Bearer Token A security token with the property ...

  7. hadoop项目实战--ETL--(一)项目分析

    项目描述 一 项目简介 在远程服务器上的数据库中有两张表,user 和order,现需要对表中的数据做分析,将分析后的结果再存到mysql中.两张表的结构如下图所示 现需要分析每一天user和,ode ...

  8. 第七篇:Spark SQL 源码分析之Physical Plan 到 RDD的具体实现

    /** Spark SQL源码分析系列文章*/ 接上一篇文章Spark SQL Catalyst源码分析之Physical Plan,本文将介绍Physical Plan的toRDD的具体实现细节: ...

  9. maven--spring-boot-starter-parent 小结

    Maven中的dependency的scope作用域详解 https://blog.csdn.net/itchiang/article/details/45009057 https://blog.cs ...

  10. Helper Files

    常用帮助类 C#语法糖 Net 通用工具类 Helloweba Front Program Resources jqGrid   Highcharts  jQuery实现的加载页面过渡效果   jQu ...