• @Resource 使用,修饰setter方法,将目标对象注入到依赖的Bean。说白了,就是你在一个User类中 比如要调用 Vip类,你用了注解,就不需要每次实例化来调用这个Vip类然后再User类中使用,只要标志上注解,Spring框架会自动调用。

  1. 入口文件,仍然是通过bean.xml文件,调用一个User类,并执行userInfo方法:


    1. package com.spring.main;  

    2.  

    3. import org.springframework.context.ApplicationContext;  

    4. import org.springframework.context.support.ClassPathXmlApplicationContext;  

    5.  

    6. import com.spring.user.User;  

    7.  

    8. public class Index {  

    9.      

    10.    public static void main(String args[]) {  

    11.        ApplicationContext Factory = null;  

    12.        Factory = new ClassPathXmlApplicationContext("bean.xml");  

    13.        User User = (User)Factory.getBean("User");  

    14.        User.setUsername("zhuli");  

    15.        User.setAge(100);  

    16.        User.setPhone(138581025);  

    17.        User.userInfo();  

    18.    }  

    19. }  



  2. User类和Vip类,User类中定义了一个private私有的vip对象,通过@Resource注解的方式,直接将Vip实例化对象赋给User类中的vip对象:


    1. //User 类  

    2. package com.spring.user;  

    3. import javax.annotation.Resource;  

    4.  

    5.  

    6. public class User {  

    7.      

    8.    private String username;  

    9.      

    10.    private int age;  

    11.      

    12.    private int phone;  

    13.      

    14.    private Vip vip;  

    15.      

    16.    @Resource  

    17.    public void setVip(Vip vip) {  

    18.        this.vip = vip;  

    19.    }  

    20.  

    21.      

    22.    public void setUsername(String username) {  

    23.        this.username = username;  

    24.    }  

    25.      

    26.    public String getUsername() {  

    27.        return username;  

    28.    }  

    29.      

    30.    public void setAge(int age) {  

    31.        this.age = age;  

    32.    }  

    33.      

    34.    public int getAge() {  

    35.        return age;  

    36.    }  

    37.      

    38.    public void setPhone(int phone) {  

    39.        this.phone = phone;  

    40.    }  

    41.      

    42.    public int getPhone() {  

    43.        return phone;  

    44.    }  

    45.      

    46.      

    47.    public void userInfo() {  

    48.        System.out.print("用户姓名:" + this.username + "\r\n"  

    49.    + "用户年龄" + this.age + "\r\n" + "用户电话:" + this.phone);  

    50.        this.vip.say();  

    51.    }  

    52.      

    53.    public void init() {  

    54.        System.out.printf("init a class");  

    55.    }  

    56.      

    57.    public void destroy() {  

    58.        System.out.print("destory a class");  

    59.    }  

    60.      

    61. }  

    62.  

    63.  

    64. //Vip类  

    65. package com.spring.user;  

    66.  

    67. public class Vip {  

    68.    public void say() {  

    69.        System.out.print("I am Vip");  

    70.    }  

    71. }  




  3. bean配置文件,beans的头部必须添加
    xmlns:context="http://www.springframework.org/schema/context"
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    <context:annotation-config/>

    如果bean上面没有这些参数,注解@Resource将无法执行。


    1. <?xml version="1.0" encoding="UTF-8"?>  

    2. <beans    

    3.    xmlns="http://www.springframework.org/schema/beans"    

    4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    

    5.    xmlns:context="http://www.springframework.org/schema/context"  

    6.    xsi:schemaLocation="http://www.springframework.org/schema/beans  

    7. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  

    8. http://www.springframework.org/schema/context  

    9. http://www.springframework.org/schema/context/spring-context-2.5.xsd">  

    10.      <context:annotation-config/>  

    11.    <bean id="User" class="com.spring.user.User" ></bean>  

    12.    <bean name="vip" id="vip" class="com.spring.user.Vip" ></bean>  

    13. </beans>  



  • @Autowired 自动装载使用,基本和上面的差不多,Autowired会使用比较多,xml配置如上例。


  1. //直接在变量对象上面使用即可  

  2.       @Autowired  

  3. private Vip vip;  


  • @PostConstruct和@PreDestroy,相当于<bean>元素中的init-method和destroy-method。相当于类初始化和类销毁前会执行的方法。


  1. @PostConstruct  

  2. public void init() { //初始化的时候运行  

  3.    System.out.printf("init a class");  

  4. }  

  5. @PreDestroy  

  6. public void destroy() { //类销毁的时候运行  

  7.    System.out.print("destory a class");  

  8. }  


java教程,自学编程,青软培训