zoukankan      html  css  js  c++  java
  • Spring-Bean之基于注解(Annotation)装配解析

    1、概述

    在Spring中,尽管使用XML配置文件可以实现Bean的装配工作,但如果XML配置的bean非常的多,XML配置文件代码将变得非常臃肿,不利于代码的后期维护。

    自从JDK 1.5之后,Java提供了注解(Annotation)功能,与此同时,Spring 3也提供了Annotation的全面支持,如注解装配Bean、注解实现AspectJ AOP编程等。

    在Spring 3中,常用的注解如下:

    ①定义Bean注解

    • @Compenent:定义Spring中的Bean,可以表示任何层的Bean(Controller、Service、Repository),这是一个泛化的注解。

    • @Repository:功能与Compenent完全一样,用于注解Repository层的Bean。

    • @Service:功能与Compenent完全一样,用于注解Service层的Bean。

    • @Controller:功能与Compenent完全一样,用于注解Controller层的Bean。(如SpringMVC)

    ②装配Bean注解

    • @Autowired:用于装配Bean的属性。可标注在成员变量、成员变量的setter方法、构造函数上。@Autowired默认按照Bean的类型进行装配。若有多个相同类型的Bean实例,将抛出BeanCreationException。

    • @Resource:与@Autowired功能相同,不同的是@Resource默认按照Bean的名称进行装配。@Resource中有两个重要的属性:name、type。name用于指定装配Bean的实例名称,type用于指定装配Bean的类型。若name、type属性都没有被指定,@Resource先按实例名称进行装配(默认实例名称除了类名首字母小写,其他部分完全一样,如Book类的默认实例名称为book)。若找不到默认的实例名称的Bean实例,就按类型进行装配;若有多个相同类型的Bean的实例,将抛出BeanCreationException异常。

    • @Qualifier:与@Autowired注解配合使用,会默认的按Bean类型装配修改为按Bean的实例名称装配。Bean的实例名称由@Qualifier注解参数决定。

    笔者个人经验:尽量使用@Autowired。

    2、实例分析

    ①创建项目

    笔者的项目大概是这样的:

    这里写图片描述

    创建一个source folder:config

    这里写图片描述

    然后导包:

    这里写图片描述

    ②分析代码

    创建applicationContext.xml

    <?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-3.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
        <context:component-scan base-package="com.hanlin.spring"></context:component-scan>
    </beans>

    这段代码:<context:component-scan base-package="com.hanlin.spring"></context:component-scan>表示扫描com.hanlin.spring包下的注解。

    定义Book类:

    import org.springframework.stereotype.Component;
    
    /**
     * @author Hanlin Wang
     */
    
    @Component
    public class Book {
        public void show(){
            System.out.println("原--始--书");
        }
    }

    注:这里的@Compenent与@Compenent(“book”)与@Compenent(value=”book”)完全一样。

    定义Book类的子类:English、Math

    English:

    import org.springframework.stereotype.Component;
    
    /**
     * @author Hanlin Wang
     */
    
    @Component
    public class English extends Book{
    
        @Override
        public void show() {
            // TODO Auto-generated method stub
            System.out.println("英语书");
        }
    
    }

    Math:

    import org.springframework.stereotype.Component;
    
    /**
     * @author Hanlin Wang
     */
    
    @Component
    public class Math extends Book{
    
        @Override
        public void show() {
            // TODO Auto-generated method stub
            System.out.println("数学书");
        }
    
    }

    创建Box类,成员变量:Book类型的book

    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    /**
     * @author Hanlin Wang
     */
    
    @Component
    public class Box {
        @Autowired
        private Book book;
    
        public void showBox(){
            this.book.show();
        }
    }

    创建主程序类:SpringBeans

    public class SpringBeans {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    
            Box box = (Box) context.getBean("box");
    
            box.showBox();
        }
    }

    运行结果:

    二月 26, 2017 3:18:51 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26d7ad36: startup date [Sun Feb 26 15:18:51 CST 2017]; root of context hierarchy
    二月 26, 2017 3:18:51 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    二月 26, 2017 3:18:51 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@371ee2ea: defining beans [book,box,english,math,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
    原--始--书
  • 相关阅读:
    css !import
    算法
    java web 运动前端
    oauth 2
    js 优化
    js 代码优化 (写的可以)
    2015/08/17 《有两条均线,你应该注意》
    佳能相机操作 EDSDK 教程 C# 版本
    2015/8/14——了2000股,是否正确呢——明天待验证?
    系统架构师考试——程序计数器 PC, 指令寄存器IR、状态寄存器SR、通用寄存器GR
  • 原文地址:https://www.cnblogs.com/wanxi/p/6476207.html
Copyright © 2011-2022 走看看