南昌网站建设一般多少钱一年,免费开源代码网站,南宁建设网,深圳模板一、存储Bean对象
在这篇文章中我介绍了Spring最简单的创建和使用#xff1a;Spring的创建和使用
其中存储Bean对象是这样的#xff1a;
1.1 配置扫描路径
想要成功把对象存到Spring中#xff0c;我们需要配置对象的扫描包路径
这样的话#xff0c;就只有被配置了的包…一、存储Bean对象
在这篇文章中我介绍了Spring最简单的创建和使用Spring的创建和使用
其中存储Bean对象是这样的
1.1 配置扫描路径
想要成功把对象存到Spring中我们需要配置对象的扫描包路径
这样的话就只有被配置了的包中的加了注解的类才会被存储到Spring中
这里的注解是什么下面我们介绍
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contenthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdcontent:component-scan base-packagecom.java.Ethan/content:component-scan/beans1.2 添加注解存储Bean对象 1.3 类注解
1.3.1 Controller控制器存储 1.3.2 Service服务存储 1.3.3 Repository仓库存储 1.3.4 Component组件存储 1.3.5 Configuration配置存储 1.4 关于类注解的疑问
1.4.1 为什么要这么多类注解 1.4.2 类注解之间的关系 1.4.3 Bean默认命名规则 在源码中就有说明
1.5 方法注解Bean
方法注解Bean必须配合类注解才能将对象存储到Spring
1.6 Bean重命名 二、获取Bean对象
获取 bean 对象也叫做对象装配是把对象取出来放到某个类中有时候也叫对象注入。 下⾯我们按照实际开发中的模式将 Service 类注⼊到 Controller 类中。
2.1 属性注入 注意
2.2 Setter注入 2.3 构造方法注入
构造方法注入是 Spring 官方从 4.x 之后推荐的注入方式 注意 只有一个构造方法的时候可以省略注解Autowired