【笔记】Spring4框架系列 [ 5 ] 之 注解

基于注释注入@Component @Value @Resource @Autowired @PostConstruct @PreDestroy等

【Student 】

package com.athl.spring;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/*
 与@Component具有相同功能,不同意义的注解还有三个:
    @Repository:注释在Dao实现类上
    @Service:注解在Service实现类上
    @Controller:注解在处理器上(SpringMVC)
 */
@Component("myStudent")
public class Student {

    @Value("张三")
    private String name;

    @Value("22")
    private int age;

    /*域属性的byType自动注入,只需@Autowired*/
    //@Autowired

    /*byName自动注入,需要@Qualifier与@Autowired配合使用*/
    //@Qualifier("mySchool")

    /*@Resource是jdk的注解,版本6及以上版本.*/

    //@Resource/*byType自动注入*/
    @Resource(name="mySchool")/*byName自动注入*/
    private School school;

    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setSchool(School school) {
        this.school = school;
    }

    /*Bean生命周期始末注解*/
    @PostConstruct
    public void afterInit(){
        System.out.println("初始化完毕后");
    }

    @PreDestroy /*需要显示关闭容器*/
    public void preDestroy(){
        System.out.println("销毁之前");
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", school=" + school
                + "]";
    }

}

【School 】

package com.athl.spring;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("mySchool")
public class School {

    @Value("黄埔军校")
    private String name;

    public School() {
    }
    public School(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "School [name=" + name + "]";
    }

}

【applicationContext.xml】

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 扫描com.athl这个包及其子包 -->
    <!-- <context:component-scan base-package="com.athl"/> -->
    <!-- 扫描com.athl的所有子包 -->
    <!-- <context:component-scan base-package="com.athl.*"/> -->

    <context:component-scan base-package="com.athl.spring"/>

    <!-- 注解与xml同时使用 -->
    <!-- 
        注解好处:配置方便,直观;
              缺点:修改需要重新编译代码。
        xml好处:修改无须重新编译。

        同时使用,xml的优先级要高于注解.若要修改修改配置文件即可。
        那么Bean类要有setter或构造器.
     -->
</beans>

【Test】

package com.athl.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void run(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println(ac.getBean("myStudent"));
        /*显示关闭容器*/
        ((ClassPathXmlApplicationContext)ac).close();
    }
}

源码下载:http://download.csdn.net/detail/jul_11th/9741743

谢谢支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值