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);

    }
}

 

打印结果:

 

posted @ 2018-12-15 16:59  杜大帅-Rebirth  阅读(759)  评论(0编辑  收藏  举报