zoukankan      html  css  js  c++  java
  • Spring深入浅出(九),注解,@Autowired/@Resource,及其它(@Component/@Repository/@Service/@Controller)

    在 Spring 中,尽管可以使用 XML 配置文件实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。
    Java 从 JDK 5.0 以后,提供了 Annotation(注解)功能,Spring 2.5 版本开始也提供了对 Annotation 技术的全面支持,我们可以使用注解来配置依赖注入。

    一、先重温一遍配置文件的方式

    1. 创建主业务类

    package com.clzhang.spring.demo;
    
    public class TextEditor {
        private SpellChecker spellChecker;
        private String name;
    
        public TextEditor(SpellChecker spellChecker, String name) {
            this.spellChecker = spellChecker;
            this.name = name;
        }
    
        public SpellChecker getSpellChecker() {
            return spellChecker;
        }
    
        public String getName() {
            return name;
        }
    
        public void spellCheck() {
            System.out.println("Inside TextEditor,Name : " + name);
            spellChecker.checkSpelling();
        }
    }

    2. 创建业务逻辑实现类 

    package com.clzhang.spring.demo;
    
    public class SpellChecker {
        public SpellChecker() {
            System.out.println("Inside SpellChecker constructor.");
        }
    
        public void checkSpelling() {
            System.out.println("Inside checkSpelling.");
        }
    }

    3. 创建主程序 

    package com.clzhang.spring.demo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainApp {
       public static void main(String[] args) {
          ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Beans.xml");
          TextEditor te = (TextEditor) context.getBean("textEditor");
          te.spellCheck();
       }
    }

    4.1. 配置文件传统写法

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor">
          <constructor-arg ref="spellChecker"/>
          <constructor-arg value="Generic Text Editor"/>
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
       </bean>
    </beans>

    4.2 配置文件自动装配写法 

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
       <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor" autowire="constructor">
          <constructor-arg value="Generic Text Editor"/>
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
       </bean>
    </beans>

    5. 运行

    Inside SpellChecker constructor.
    Inside TextEditor,Name : Generic Text Editor
    Inside checkSpelling.

    二、使用@Autowired的方式

    1. 修改主业务类如下

    package com.clzhang.spring.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;   
    
    public class TextEditor {
        private SpellChecker spellChecker;
        private String name;
        
        @Autowired 
        public TextEditor(SpellChecker spellChecker, String name) {
            this.spellChecker = spellChecker;
            this.name = name;
        }
    
        public SpellChecker getSpellChecker() {
            return spellChecker;
        }
    
        public String getName() {
            return name;
        }
    
        public void spellCheck() {
            System.out.println("Inside TextEditor,Name : " + name);
            spellChecker.checkSpelling();
        }
    }

    2. 配置文件修改如下

    <?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-3.0.xsd
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
       <context:annotation-config/>
      
       <bean id="textEditor" class="com.clzhang.spring.demo.TextEditor">
          <constructor-arg value="Generic Text Editor"/>
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.clzhang.spring.demo.SpellChecker">
       </bean>
    </beans>

    Spring 默认不使用注解装配 Bean,因此需要在配置文件中添加 <context:annotation-config/>,启用注解。

    3. 运行

    Inside SpellChecker constructor.
    Inside TextEditor,Name : Generic Text Editor
    Inside checkSpelling.

    三、@Resource注解

    其作用与@Autowired一样,区别在于@Autowired默认按照Bean类型装配,而@Resource默认按照Bean实例名称进行装配。

    四、@Component/@Repository/@Service/@Controller注解

    1. @Component,可以使用此注解描述Spring中的Bean,它是一个泛化的概念,仅表示一个组件(Bean),并且可以作用在任何层次。

    2. @Repository,用于将数据访问层(DAO层)的类标识为Spring中的Bean,等同于@Component。

    3. @Service,用于将业务层(Service层)的类标识为Spring中的Bean,等同于@Component。

    4. @Controller,用于将控制层(Controller层)的类标识为Spring中的Bean,等同于@Component。

    五、使用注解之后,可以在配置文件中通知Spring扫描Bean文件

          <!-- 指定需要扫描的包,使注解生效 -->
          <context:component-scan base-package="com.itheima.jdk" />
          <context:component-scan base-package="com.itheima.aspectj.annotation" />

    这样,就不用再对Bean进行配置了。

    本文参考:

    http://c.biancheng.net/spring/config-autowiring.html

    https://www.w3cschool.cn/wkspring/rw2h1mmj.html

    https://blog.csdn.net/l1212xiao/article/details/80424064

  • 相关阅读:
    严重: Parse error in application web.xml file at jndi:/localhost/ipws/WEBINF/web.xml java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml
    Failed to install .apk on device 'emulator5554': timeout解决方法
    java.lang.NoClassDefFoundError:org.jsoup.Jsoup
    Conversion to Dalvik format failed: Unable to execute dex:解决方法
    apache Digest: generating secret for digest authentication ...
    Description Resource Path Location Type Project has no default.properties file! Edit the project properties to set one.
    android service随机自启动
    MVC3 安装部署
    EF 4.3 CodeBased 数据迁移演练
    SQL Server 2008开启sa账户
  • 原文地址:https://www.cnblogs.com/nayitian/p/15005532.html
Copyright © 2011-2022 走看看