spring之註解構造IOC

1、註釋配置相對於 XML 配置具備不少的優點:java

 一、他能夠充分利用java反射機制獲取類結構信息。這些信息能夠有效減小配置的工做,如使用JPA註釋配置ORM映射時,咱們就不須要指定的PO的屬性名、類信息,若是關係表字段和PO屬性名,類型都一致,甚至無需編寫任務屬性的映射信息,由於這些信息均可以經過java反射機制獲取。程序員

二、註釋和java代碼位於一個文件中,而XML配置採用獨立的配置文件,大多數配置信息在程序開發完成後都不會調整,若是配置信息和java代碼放在一塊兒,有助於加強程序的內聚性。而採用獨立的XML配置文件,程序員在編寫一個功能的時候,每每須要在程序文件和配置文件中不停切換,而這種思惟上的不連貫會下降開發效率。spring

三、在不少狀況下,註釋配置比XML配置更受歡迎,所以可使用註釋進行Bean定義和依賴注入的內容。app

四、用註解來向spring容器註冊bean。須要在applicationContext.xml中註冊<context:component-scan base-package=」pagkage1[,pagkage2,…,pagkageN]」/>。ide

2、案例測試

一、建立實體類this

Student類spa

package cn.happy.entity;

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;

import javax.annotation.Resource;

//使用component,默認使用類名首字母小寫做爲spring的對象
@Component("student") //此處括號中的內容也能夠省略
public class Student {
    @Value("lucy")
    private String name;
    @Value("18")
    private int age;
    //@Resource(name="car")   //JDK的註解。括號中的能夠省略   注入時兩種:先baName,不寫時,默認會在內存中找car同名的對象。
   //另外一種註解:
     @Autowired
     @Qualifier("car")
    private Car car;  //當爲car2時,會按byType找,只有一個同類型的bean才能注入成功
    //當有一個littleCar去繼承Car時,會報錯。


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", car=" + car +
                '}';
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public  void getData(){
        System.out.println(name);
    }
}

 Car類component

package cn.happy.entity;

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

@Component("car")
public class Car {
    @Value("蘭博基尼")
    private String brand;
    @Value("紫色")
    private String color;

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                '}';
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

  二、編寫xml配置文件xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
    <context:component-scan base-package="cn.happy.entity"></context:component-scan>
</beans>

  三、編寫測試類

package cn.happy.test01;

import cn.happy.entity.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ZJtest {
    @Test
    public void  testZJ(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student)ctx.getBean("student");
        System.out.println(stu);
    }
}
相關文章
相關標籤/搜索