Spring完全注解开发
注解的好处:如果管理很多的Bean,要求这些Bean都配置在applocationContext.xml文件中。用了注解之后,就不需要在xml文件中配置了,Spring提供了几个辅助类会自动扫描和装配这些Bean。所以大大减少了xml文件的体积,Spring会根据配置去扫描某些包里面的类,得到类的方法或注解,不同的注解会进行不同的操作。
@Configuration
这个注解一般的来说,用在类上面,加上这个注解的类可以成为一个spring的xml配置文件,使用的是java代码的配置
//配置Spring装载类
//Configurable 指定此类为Spring配置类。作为配置类,替代XML配置文件
//ComponentScan 指定Bean范围(注解范围),扫描装载
@Configurable
@ComponentScan(basePackages = {"com.minelsh"})
public class springConfig {
}
创建对象的注解
Spring中作用在作用类上的注解有@Component注解和三个衍生注解:@Responsity @Service和@Controller
三个衍生注解的说明:
@Controller:通常用于Controller类,也就是控制层(MVC)。
@Service:通常用于Service类,也就是服务层。
@Repository:通常用于DAO类,也就是持久层。
当注解在作用类上时,表明这些类似交给Spring容器进行管理的。
@Component注解的使用
@Component("conversionImpl")
//其实默认的spring中的Bean id 为 conversionImpl(首字母小写)
public class ConversionImpl implements Conversion {
}
案例1:
不指定bean的名称,默认为类名首写小写字母
@Component
public class University {
to do sthing...
}
获取bean方式:
ApplicationContext ctx = new
ApplicationContext context = new AnnotationConfigApplicationContext(Bean配置类);
University ust = (University) ctx.getBean("university");
案列2:
指定bean的名称
@Component("university1")
public class University {
to do sthing...
}
获取bean方式:
ApplicationContext ctx = new
ApplicationContext context = new AnnotationConfigApplicationContext(Bean配置类);
University ust = (University) ctx.getBean("university1");
在构造器上的注解
@Autowired :根据类型(ByType)来自动匹配(核心注解)
这个注解可以用于属性,setter方法,还有构造器上,这个注解用于注入依赖的对象。当再一个属性上加上@Autowired注解,有时可能要指定一些额外的值,Spring然后会自动的将值赋给这个属性。
案例1:作用在构造器上
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
案列2:使用在setter方法上
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
案列3:使用在域属性上
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
private MovieCatalog movieCatalog;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
案列4:使用在任意方法名和产生的普通方法上
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
案列5:使用在域属性数组上
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
// ...
}
案列6:使用在集合类型上
public class MovieRecommender {
private Set<MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
@Qualifier
这个注解和@Autowired一起使用,当想对注入的过程做更多的控制,@Qualifier可以帮助指定做更详细的配置。一般在两个或多个bean是相同的类型,spring在注入的时候会出现混乱
例如有个接口叫做Hellointerface,然后两个bean都实现了Hellointerface接口
@Component
public class Bean1 implements HelloInterface {
//
}
@Component
public class Bean2 implements HelloInterface {
//
}
如果只使用@Autowired注解,Spring就不知道到底要注入哪一个bean。解决办法就是加上@Qualifier注解
@Component
public class BeanA {
@Autowired
@Qualifier("bean2")
private HelloInterface dependency;
...
@Resource:根据类型和名称(ByType & ByName)来自动匹配(核心注解)
@Resource和@Autowired注解都是用来实现依赖注入的。只是@AutoWried按by type自动注入,而@Resource默认按byName自动注入。
@Resource有两个重要属性,分别是name和type
spring将name属性解析为bean的名字,而type属性则被解析为bean的类型。所以如果使用name属性,这使用ByName的自动注入策略,如果使用Type类型则使用ByTyoe的自动注入策略。如果都没有指定,则听过反射机制使用ByName自动注入策略。
案列1:使用@Resource注解
@Resource(name="bucket")
private String bucketName;
@Resource(name="style")
private String styleName;
翻译为xml模式来看
<bean name="bucket" class="java.lang.String">
<constructor-arg value="${oos.bucketName}"/>
</bean> <bean name="style" class="java.lang.String">
<constructor-arg value="${oos.styleName}"/>
</bean>
Spring完全注解开发的更多相关文章
- Spring _day02_IoC注解开发入门
1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...
- spring原始注解开发-01
我们使用xml-Bean标签的配置方式和注解做对比理解 1.创建UserDao接口以及UserDao的实现类UserDaoImpl(接口代码省略) public class UserDaoImpl i ...
- Spring使用注解开发及使用java类进行配置bean
Spring使用注解开发 说明 在spring4之后,想要使用注解形式,必须得要引入aop的包 在配置文件当中,还得要引入一个context约束 <?xml version="1.0& ...
- Spring基于注解开发异常
基于注解开发: 一开始:用的jar包: 百度查到: 导入aop包: 没用 有的说: Spring版本和jdk版本不匹配 于是我换成了4.0版本 导入的jar包: 还是报错. 解决办法:添加spring ...
- spring——使用注解开发
注意:spring4之后,使用注解开发需要导入AOP包org.springframework:spring-aop:5.2.5.RELEASE以及context约束,增加注解的支持 <?xml ...
- Spring MVC注解开发入门
注解式开发初步 常用的两个注解: @Controller:是SpringMVC中最常用的注解,它可以帮助定义当前类为一个Spring管理的bean,同时指定该类是一个控制器,可以用来接受请求.标识当前 ...
- Spring原始注解开发-02
使用@Repository.@Service.@Controller注解配置,使其更加清晰属于哪一层,因为我是模拟的web层,所有没有使用@Controller注解,后面结合web开发会使用到 1.创 ...
- spring @Validated 注解开发中使用group分组校验
之前知道spring支持JSR校验,在自己定义的bean中加入@NotNull,@NotBlank,@Length等之类的校验用于处理前台传递过来的request请求,避免在写多余的代码去处理. 但是 ...
- Spring基于注解开发的注解使用之AOP(部分源代码分析)
AOP底层实现动态代理 1.导入spring-aop包依赖 <!--aopV1--> <dependency> <groupId>org.springframewo ...
- Spring MVC 注解开发详解
@Controller控制器定义 1.Controller是单利模式,被多个线程请求共享,因此设计成无序状态. 2.通过@controller标注即可将class定义为一个controller类.为使 ...
随机推荐
- 开源的键鼠共享工具「GitHub 热点速览」
十一长假回来,我的手放在落灰的键盘上都有些陌生了,红轴竟敲出了青轴般的响声,仿佛在诉说对假期结束的不甘. 假期回归的首更,让我们看看又有什么好玩的开源项目冲上了开源热榜.一套键盘和鼠标控制多台电脑的工 ...
- spring上 -基于Xml配置bean笔记
4,Spring 内容 7,快速入门 需求:通过 Spring 的方式[配置文件], 获取 JavaBean: Monster 的对象, 并给该的对象属性赋值, 输出该对象信息. 代码结构: lib ...
- Java日期时间API系列28-----Jdk8中java.time包中的新的日期时间API类,计算节假日和二十四节气。
1.节日信息计算代码 package com.xkzhangsan.time.holiday; import java.time.DayOfWeek; import java.time.LocalDa ...
- WebAssembly C++开发环境搭建
WebAssembly 开发环境搭建 简介 WebAssembly 是一种新的编码方式,可以在现代的网络浏览器中运行 - 它是一种低级的类汇编语言,具有紧凑的二进制格式,可以接近原生的性能运行,并为诸 ...
- 墨天轮访谈 | 阿里云捷熙:AnalyticDB,人人可用的数据分析服务
分享嘉宾:李婧玮(捷熙) 阿里云数据库资深产品经理 整理:墨天轮社区 导读 大家好,我是来自阿里云的捷熙.AnalyticDB是融合数据库.大数据技术于一体的云原生企业级数据仓库平台,今天我为大家带来 ...
- 一文彻底搞定Redis与MySQL的数据同步
Redis 和 MySQL 一致性问题是企业级应用中常见的挑战之一,特别是在高并发.高可用的场景下.由于 Redis 是内存型数据库,具备极高的读写速度,而 MySQL 作为持久化数据库,通常用于数据 ...
- Einfuehrung in die Kuenstliche Intelligenz学习笔记
1.Uninformed Search 1.1 State Space of a Problem 1.2 depth of the search tree and fringe of the sear ...
- 我用Replicate训练了个纹身Flux AI LORA模型,分享下经验
# 我用Replicate训练了个纹身AI模型,分享下经验 ## 起因 最近一直在研究AI辅助设计,正好我对纹身设计特别感兴趣.经过一段时间摸索,用Replicate平台训练了一个还不错的纹身设计模型 ...
- 了解 Uniswap V2(DEX)
Uniswap V2 是一个基于以太坊的去中心化交易所(DEX),它通过流动性池和自动化做市商(AMM)模型来实现去中心化的代币交换.以下是 Uniswap V2 的核心概念: 1. 自动化做市商(A ...
- 微信小程序原生AI运动(动作)检测识别解决方案
前几年受疫情影响,人员流动受限,反而让"AI运动"概念风靡一时.空前火爆.目前已经在AI运动锻炼.体育教学.线上运动主题活动等场景中,成功得到了应用,并获得了广大互联网用户的认可. ...