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

posted @ 2021-07-13 11:06  那些年的事儿  阅读(97)  评论(0编辑  收藏  举报