美文网首页
111、【JavaEE】【Spring】Spring 注解开发(

111、【JavaEE】【Spring】Spring 注解开发(

作者: yscyber | 来源:发表于2021-10-25 15:09 被阅读0次

1、概述

  • 这部分的注解,是使用后完全可以替换 XML 核心配置文件的注解。之前的讲述的注解中(https://www.jianshu.com/p/e4c719f34a5bhttps://www.jianshu.com/p/1d7311fa8e40),仍然使用 XML 核心配置文件src/main/resources/applicationContext.xml,并在配置文件中配置开启注解扫描这一关键步骤。

  • 虽然说,这些注解能够彻底不需要 XML 配置文件。但是,一切根据实际情况(团队内的开发规范等)。

2、注解

Spring-23

3、@Configuration

  • 一旦某个类使用了该注解,相当于这个类会有和之前的applicationContext.xml有相同的作用。
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {
    
}

4、@Bean

  • Spring 框架中的 XML 核心配置文件的一个重要作用是,指定哪些类的对象交由 Spring 容器(IoC 容器)进行管理。在@Configuration的类中,使用@Bean注解去实现 XML 中的<bean ······/>的作用。

  • 规则是:在使用@Configuration注解的类中,一个方法上使用@Bean注解,其返回值交由 Spring 容器管理。

import com.yscyber.spring.four.repo.OrderRepo;
import com.yscyber.spring.four.repo.impl.OrderRepoImpl;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {

    @Bean
    OrderRepo orderRepo() {
        return new OrderRepoImpl();
    }

}

此时,这个返回值就交由 Spring 容器管理,默认 id 为该方法名(因为没使用@Bean注解中的属性)。

  • @Bean注解中可以指定 id
import com.yscyber.spring.four.repo.OrderRepo;
import com.yscyber.spring.four.repo.impl.OrderRepoImpl;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {

    @Bean("orderRepo")
    OrderRepo orderRepo() {
        return new OrderRepoImpl();
    }

}
  • 获取 Spring 容器(IoC 容器)时,使用的类是org.springframework.context.annotation.AnnotationConfigApplicationContext,不同于 XML 配置文件org.springframework.context.support.ClassPathXmlApplicationContext
    @Test
    public void test1() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);

        OrderRepo orderRepo = (OrderRepo) applicationContext.getBean("orderRepo");

        orderRepo.aMethod();
    }

5、@ComponentScan

  • 开启注解扫描,相当于 XML 配置文件中的<context:component-scan base-package="······"/>
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"······"})
public class SpringConfig {

}

6、@PropertySource

  • 加载.properties文件,相当于 XML 配置文件中的<context:property-placeholder location="······"/>
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = {"classpath:jdbc.properties"})
public class SpringConfig {

}

7、@Import

  • 导入其他配置类,实现类似于“配置文件模块化”,注意该注解仅限配置类。
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataSourceConfig {

}
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({DataSourceConfig.class})
public class SpringConfig {

}

8、@ImportResource

  • 导入配置文件
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource(locations = {"classpath:applicationContext.xml"})
public class SpringConfig {

}

9、配置类中的方法的参数可以进行依赖注入

  • 注入时,对象可以来自同一配置类(同一配置类下的一个方法的返回值),也可以来自另一个配置类,前提是需@Import引入。
Spring-24

相关文章

  • 111、【JavaEE】【Spring】Spring 注解开发(

    1、概述 这部分的注解,是使用后完全可以替换 XML 核心配置文件的注解。之前的讲述的注解中(https://ww...

  • spring02

    Spring相关注解 Spring注解开发 集成Spring测试框架 重点:重点掌握Spring相关注解。@Con...

  • 109、【JavaEE】【Spring】Spring 注解开发(

    1、概述 Spring 是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替 XM...

  • 110、【JavaEE】【Spring】Spring 注解开发(

    @Value 情形一: 情形二:注入来自.properties文件的字符串 @Scope

  • DAY05-Sping02

    一、 DI 注解 Spring的@Autowired JavaEE的@Resource 都是取代注入bean, 贴...

  • spring mvc 父子容器

    看本文之前首先需了解 spring注解驱动开发。可以看另外一篇文章spring注解,本文是在spring注解开发的...

  • SpringBoot

    一、Spring Boot简介 1.1、Spring Boot的特点 SpringBoot,新一代JavaEE开发...

  • Spring快速入门详解

    前言 Spring技术是JavaEE开发必备技能,企业开发技术选型命中率>90%。Spring具有简化开发,降低企...

  • Spring的学习笔记

    一、Spring的概述 1.1、Spring是什么 Spring是一个开源的轻量级JavaEE开发应用框架,其目的...

  • 第2章:spring 依赖

    第2章:spring 依赖 标签(空格分隔): JavaEE开发的颠覆者SpringBoot实战 spring中声...

网友评论

      本文标题:111、【JavaEE】【Spring】Spring 注解开发(

      本文链接:https://www.haomeiwen.com/subject/uiqraltx.html