Spring系列之五:使用注解代替xml配置

Spring因为使用容器来构造对象,一种是在配置文件中配置xml来获得对象,一种是基于注解的方式来构造对象

先用流程的来表现Spring是如何用注解的方式获得对象的:

1:Spring加载配置文件,表明是要用的注解的方式构造对象,在配置文件中要写出你在哪个包下面进行扫描

2:在包下进行扫描,看是否有使用注解标签的(@Component)。有的话就放入容器中

3:创建对象的时候,获得容器,调用容器中的对象

下面来进行详细的讲解:

首先我们要在配置文件中表明你要使用注解方式来构造对象

下面给出配置文件源代码:

<?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">
    

    <context:component-scan base-package="com.lydetails.ssm.Test">
    </context:component-scan>


</beans>

可以看出,用context标签来使用注解,后面跟着一个base-package属性,表示你需要在哪个包目录下进行扫描,来获得注解

现在我们在Test包下进行构建类,先建立一个User类,源代码如下: (测试的时候可以把里面的car类删除)

package com.lydetails.ssm.Test;

import org.springframework.stereotype.Component;

@Component("user")
public class User {
    private String name;
    private Integer age;
    private Car car;

    public User() {
        System.out.println("User构造函数");
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

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

    public Car getCar() {
        return car;
    }

    public User(Car car,Integer name, Integer age) {
        this.name = name+"";
        this.age = age;
        this.car = car;
    }
}

@Component("user")

我们用Component表明把这个类写成注解,注解的名字为user.


下面进行测试获得容器吗,然后获得容器中的对象:

@Test //使用注解构造
public void func6() {
   //加载配置文件,获得容器
    ApplicationContext apl = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    //调用容器的对象
    User user = (User) apl.getBean("user");
    System.out.println(user);
}

测试结果:



这样一个注解的基本的使用就完成了


进阶:使用注解完成单例模式或者多例模式

在Component下使用一个注解即可,里面的名字为singleton

@Component("user")

@Scope("singleton") 


下面给出测试代码和结果:

@Test //使用注解构造,测试单例模式
public void func7() {
    //加载配置文件,获得容器
    ApplicationContext apl = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    //调用容器的对象
    User user1 = (User) apl.getBean("user");
    User user2 = (User) apl.getBean("user");
    System.out.println(user1==user2);
}

结果如下:


进阶之依赖注入:

    因为一个类中可能有另外一个类,这种依赖也简单,将两个类都表明是注解,并在其中一个类中表明另外一个类的来处:

    下面给出两个类,进行测试,User和car:

package com.lydetails.ssm.Test;

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

@Component("car")
public class Car {
    @Value("奔驰")
    private String name;
    private String color;

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

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

    public Car() {
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                '}';
    }
}
package com.lydetails.ssm.Test;

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

import javax.annotation.Resource;

@Component("user")
public class User {
    @Value("奥巴马")
    private String name;
    private Integer age;
    @Resource(name = "car")
    private Car car;

    public User() {
        System.out.println("User构造函数");
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

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

    public Car getCar() {
        return car;
    }

    public User(Car car,Integer name, Integer age) {
        this.name = name+"";
        this.age = age;
        this.car = car;
    }
}

    在User中使用@Resource(name = "car")来表示依赖关系,其中还有自动装配的方式 @Autowired+
    @Qualifier("car")组合起来是不推荐使用的()

    @Value表示赋值

测试函数为:

@Test //使用注解构造,测试依赖关系
public void func8() {
    //加载配置文件,获得容器
    ApplicationContext apl = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    //调用容器的对象
    User user = (User) apl.getBean("user");
    System.out.println(user.toString());
}

结果为:


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值