Spring框架系列(二)之Bean的注解管理

微信公众号:compassblog

欢迎关注、转发,互相学习,共同进步!

有任何问题,请后台留言联系!

1、Spring中的两种容器

在系列(一)中我们已经知道,Spring 是管理对象的容器,其中有2种基本的容器,一种是 ApplicationContext,另一种是 BeanFactory (这种在开发中已经不再使用)。两种容器的区别如下:

  • ApplicationContext:这种容器在启动加载 applicationContext.xml 配置文件时就会创建

  • BeanFactory:这种容器在 getBean 的时候才会创建生成类的实例

2、Spring中Bean 的注解管理方式实例

(1)、新建项目,引入相关 jar 包,如下图所示:

(2)、在 src 下新建配置文件,开启使用注解代理配置,代码如下:

applicationContext.xml

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

<!-- 指定扫描com.spring.bean报下的所有类中的注解.
     扫描包时,会扫描指定包下的所有子包
 -->
<context:component-scan base-package="com.spring.bean"></context:component-scan>
</beans>
复制代码

(3)、将对象注册到容器,并通过注解给属性注入值,代码如下:

Student.java

package com.spring.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Component("stu")
    @Service("stu") // service层
    @Controller("stu") // web层
    @Repository("stu")// dao层
public class Student {
    //基本属性
    private String name;

    @Value("22")
    private int age;

    @Autowired  //自动装配,如果匹配多个类型一致的对象,将无法选择具体注入哪一个对象
    private Book book;

    //setter和getter方法
    public String getName() {
        return name;
    }

    @Value("孔乙己")
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }

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

}
复制代码

Book.java

package com.spring.bean;

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

@Component("book")
public class Book {
    //基本属性
    @Value("Java开发")
    private String name;

    @Value("68")
    private double price;

    //setter和getter方法
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [name=" + name + ", price=" + price + "]";
    }

}
复制代码

(4)、书写测试类,代码如下:

TestDemo.java

package com.spring.test;

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

import com.spring.bean.Student;

public class TestDemo {
    @Test
    public void fun1(){

        //1 创建容器对象
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2 向容器获取stu对象
        Student stu1 = (Student) ac.getBean("stu");

        //3 打印stu对象
        System.out.println(stu1.toString());

    }

}
复制代码

(5)、使用 JUnit4 进行测试,结果如下图所示:

3、Spring 中 Bean 管理的常用注解

(1)、 @Component 组件:作用在类上

Spring中提供 @Component 的三个衍生注解,功能目前是一致的,这三个注解是为了让标注类本身的用途更加清晰。

  • @Controller : web 层

  • @Service : service 层

  • @Repository : dao 层

(2)、属性注入的注解:使用注解注入的方式,可以不用提供 set 方法

  • @Value :用于注入普通类型

  • @Autowired :自动装配: 默认按类型进行装配,按名称注入
    注意:如果匹配多个类型一致的对象,将无法选择具体注入哪一个对象

  • @Qualifier :强制使用名称注入,告诉spring容器自动装配哪个名称的对象

  • @Resource :手动注入,指定注入哪个名称的对象,相当于 @Autowired 和 @Qualifier 一起使用

(3)、Bean 作用范围的注解

  • @Scope:
    singleton :单例
    prototype :多例

(4)、Bean 生命周期配置的注解

  • @PostConstruct :相当于 init-method方法

  • @PreDestroy :相当于 destroy-method方法

4、Spring 中 Bean 管理方式的比较


基于XML的配置基于注解的配置
Bean 定义<bean id=" " class=" "/>@Component 或者其衍生类
Bean 名称通过 id 或 name 制定@Component(" ")
Bean 注入<property> 属性或者或者 p 命名空间@Autowired 或者 @Qualifier 或者 @Resource
Bean 作用范围scope 范围属性@Scope
Bean 声明过程初始化 init-methode 和销毁 destroy-method初始化 @PostConstruct 和销毁 @PreDestroy


  • 基于XML的配置:结构清晰,但开发不便

  • 基于注解的配置:属性注入容易,开发方便


关注微信公众号compassblog,后台回复 “Spring系列二” 获取本项目源码


您可能还喜欢:


本系列后期仍会持续更新,欢迎关注!


如果你认为这篇文章有用,欢迎转发分享给你的好友!

本号文章可以任意转载,转载请注明出处!


扫码关注微信公众号,了解更多



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值