1. 程式人生 > >spring學習(四)使用註解代替xml配置

spring學習(四)使用註解代替xml配置

用的是IDEA的maven工程,pom.xml檔案導包依賴省略

一、書寫要匯入容器的實體類

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; @Component("car") //其他註解方式見User類 public class Car { //屬性值註解在set方法上,但是也可以直接註解在屬性上面 @Value("蘭博基尼") private String carName; @Value("藍色") private String color; public String getCarName() { return carName; } public void
setCarName(String carName) { this.carName = carName; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car{" + "carName='" + carName + '\'' + ", color='" + color + '\'' + '}'; } }
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("user")
//@Service("user")  在service層用
//@Controller("user")  在controller層用
//@Repository("user")  在dao層用
//指定物件的作用範圍
@Scope("singleton")
public class User {

    private String name;
    private Integer age;

    //@Autowired //自動裝配
    //問題:如果匹配多個型別一致的物件.將無法選擇具體注入哪一個物件.
    //@Qualifier("car2")//使用@Qualifier註解告訴spring容器自動裝配哪個名稱的物件

    @Resource(name = "car")//手動注入,指定注入哪個名稱的物件
    private Car car;

    public String getName() {
        return name;
    }

    @Value("小米")
    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    @Value("12")
    public void setAge(Integer age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

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

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

二、書寫配置檔案(該例子用的是IDEA的maven工程,配置applicationContext.xml檔案寫在resources資料夾中)

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

    <!-- 指定掃描cn.itcast.bean報下的所有類中的註解.
         注意:掃描包時.會掃描指定報下的所有子孫包
     -->
    <context:component-scan base-package="dyh.bean"></context:component-scan>

</beans>

三、測試

import dyh.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo {

    @Test
    public void fun(){
        //建立容器
        ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml");
        //從容器中獲取值
        User user = (User) ct.getBean("user");
        //打印出來值
        System.out.println(user);

    }
}

 

列印結果: