zoukankan      html  css  js  c++  java
  • 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系列二” 获取本项目源码

     

    您可能还喜欢:

     

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

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

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

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

  • 相关阅读:
    51nod乘积之和
    Dell服务器安装OpenManage(OMSA)
    Nginx反向代理PHP
    搭建haproxy
    108. Convert Sorted Array to Binary Search Tree
    60. Permutation Sequence
    142. Linked List Cycle II
    129. Sum Root to Leaf Numbers
    118. Pascal's Triangle
    26. Remove Duplicates from Sorted Array
  • 原文地址:https://www.cnblogs.com/compassblog/p/8481222.html
Copyright © 2011-2022 走看看