[转]spring学习笔记:annotation的使用3

摘自wjt276的笔记
---------------------  

一、        @Scope@PostConstruct@PreDestroy

@Scope对应于xml配置文件的scope属性

@PostConstruct对应于xml配置文件中的init-method属性

@PreDestroy对于应于xml配置文件中的destroy-method属性

例如如下:

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Component;

@Component(value="userService")

@Scope("singleton")

public class UserService {

    @PostConstruct

    public void init(){System.out.println("现在开始初始化UserService");}

    private UserDao userDao = new UserDaoImpl();

    public UserDao getUserDao() {return userDao;}

    @Resource

    public void setUserDao(UserDao userDao) {this.userDao = userDao;}

    public void add(User u){ userDao.save(u);}

    @PreDestroy

    public void destroy(){System.out.println("destory"); }

 

 

二、        注解对应的jar

1@Autowired           org.springframework.beans.factory.annotation.Autowired;

2@Qualifier             org.springframework.beans.factory.annotation.Qualifier;

3@Componet           org.springframework.stereotype.Component;

4@Resource             javax.annotation.Resource;

5@Scope                  org.springframework.context.annotation.Scope;

6@PostConstruct     javax.annotation.PreDestroy;

7@PreDestroy          javax.annotation.PreDestroy;


原文地址:https://www.cnblogs.com/redcoatjk/p/3562366.html