当前位置: 首页 > news >正文

spring注解使用中常见的概念性问题

Spring注解使用中常见的概念性问题

  • @Configuration有什么用?
  • @Configuration和XML有什么区别?哪种好?
  • @Autowired 、 @Inject、@Resource 之间有什么区别?
  • @Value、@PropertySource 和 @Configuration?
  • Spring如何处理带@Configuration @Import的类?
  • @Profile有什么用?

@Configuration有什么用?

  1. @Configuration表明一个类中声明一个和多个@Bean标记的方法,并且这些方法被Spring容器管理用于生成Bean定义以及在运行时这些Bean的服务请求。
  2. 其实相当于原来的声明了多个bean的xml配置文件,而且被@Configuration也相当于一个组件。
  3. 加入@Configuration 注解,表明这就是一个配置类。有一个myBean()的方法并用@Bean 进行注释,返回一个MyBean()的实例,表明这个方法是需要被Spring进行管理的bean。@Bean 如果不指定名称的话,默认使用myBean名称,也就是小写的名称。
@Configuration
public class AppConfig {@Beanpublic MyBean myBean(){return new MyBean();}
}
  1. 可以通过使用 AnnotationConfigApplicationContext 来引导启动这个@Configuration 注解的类(在web项目中,也可以使用AnnotationContextWebApplicationContext或者其他变体来启动)
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
MyBean myBean = ctx.getBean(MyBean.class);
// use myBean ...
  1. 另外可以通过使用XML方式开启基于注解的启动,在/resources 目录下新建 application-context.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"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
><!-- 相当于基于注解的启动类 AnnotationConfigApplicationContext--><context:annotation-config /><bean class="com.linyf.demo.config.MyConfiguration"/>
</beans>

@Configuration和XML有什么区别?哪种好?

@Autowired 、 @Inject、@Resource 之间有什么区别?

  • @Inject: 这是jsr330 的规范,通过AutowiredAnnotationBeanPostProcessor 类实现的依赖注入。位于javax.inject包内,是Java自带的注解。
  • @Autowired: Spring提供的注解,通过AutowiredAnnotationBeanPostProcessor 类实现注入。位于org.springframework.beans.factory.annotation 包内。
  • @Resource: @Resource 是jsr250规范的实现,通过CommonAnnotationBeanPostProcessor 类实现注入。@Resource 一般会指定一个name属性

区别:
@Autowired和@Inject基本是一样的,因为两者都是使用AutowiredAnnotationBeanPostProcessor来处理依赖注入。但是@Resource是个例外,它使用的是CommonAnnotationBeanPostProcessor来处理依赖注入。当然,两者都是BeanPostProcessor。

@Value、@PropertySource 和 @Configuration?

  • @Configuration 可以和@Value 和@PropertySource 一起使用读取外部配置文件,

Spring如何处理带@Configuration @Import的类?

详情可以查看这篇文章:Spring/SpringBoot系列之SpringBoot 源码常用注解【九】

@Profile有什么用?

@Profile: 表示当一个或多个@Value 指定的配置文件处于可用状态时,组件符合注册条件,可以进行注册。

三种设置方式:

  1. 可以通过ConfigurableEnvironment.setActiveProfiles()以编程的方式激活

  2. 可以通过AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME (spring.profiles.active )属性设置为JVM属性

  3. 作为环境变量,或作为web.xml 应用程序的Servlet 上下文参数。也可以通过@ActiveProfiles 注解在集成测试中以声明方式激活配置文件。
    作用域:

  4. 作为类级别的注释在任意类或者直接与@Component 进行关联,包括@Configuration 类

  5. 作为原注解,可以自定义注解

  6. 作为方法的注解作用在任何方法
    注意:

如果一个配置类使用了Profile 标签或者@Profile
作用在任何类中都必须进行启用才会生效,如果@Profile({“p1”,“!p2”}) 标识两个属性,那么p1 是启用状态 而p2
是非启用状态的。
详见: https://blog.csdn.net/fei1234456/article/details/106905054/

http://www.lryc.cn/news/12534.html

相关文章:

  • Module理解及使用
  • ngix 常用配置之 location 匹配规则
  • chatGPT与人形机器人,高泽龙接受中国经营报采访谈二者发展
  • 进程同步——读者-写者问题
  • Android自动化配置
  • Java程序怎么运行?final、static用法小范围类型转大范围数据类型可以吗?
  • 【数据管理】谈谈哈希原理和散列表
  • 浙江工业大学关于2023年MBA考试初试成绩查询及复试阶段说明
  • 08:进阶篇 - CTK 插件元数据
  • 数据结构与算法之数组寻找峰值分而治之
  • Metasploit 使用篇
  • Java岗面试题--Java并发(日积月累,每日三题)
  • Prometheus监控案例之blackbox-exporter
  • Makefile基础使用和实战详解
  • Go基础-变量
  • 【算法】三道算法题目单词拆分,填充每个节点的下一个右侧节点指针以及组合总和
  • 【算法】刷题路线(系统+全面)
  • Fiddler的报文分析
  • Spring 中,有两个 id 相同的 bean,会报错吗
  • Mysql数据库的时间(4)一查询数据库时间注意点
  • 一起学 pixijs(2):修改图形属性
  • LeetCode 121. 买卖股票的最佳时机
  • shell脚本内调用另外一个shell脚本的几种方法
  • Linux C++ 多进程下write写日志问题思考
  • MySQL的四种事务隔离级别
  • 方法区和元空间有什么关系?
  • 2023VNCTF的两道(暂时)
  • JDK版本区别
  • Android 基础知识4-2.8 TableLayout(表格布局)详解
  • SQL代码编码原则和规范