[刘阳Java]_Spring常用注解介绍_第6讲
Spring的注解是在Spring2.5的版本中引入的,目的简化XML配置。在企业开发过程中使用注解的频率非常高,但是学习注解的前提是大家一定要对Spring基于XML配置要熟悉,这是我个人建议,因为在Spring2.0的版本时候是没有出现注解的使用
1. Spring常用注解如下
- @Component
- @Autowired
- @Qualifier
- @Scope
- @Controller
- @Service
- @Repository
2. 使用Spring注解的时候一定关注Spring框架需要加入的包【很重要】,我们这里以spring4.0.3版本为例来介绍
- common-logging.jar
- spring-core-4.0.3.jar
- spring-context-4.0.3.jar
- spring-beans-4.0.3.jar
- spring-expression-4.0.3.jar
- spring-aop-4.0.3.jar,【此jar包在使用<context:component-scan/>需要导入此包】
3. @Component注解
- @Component主要用于将一个Java类注入到Spring框架中,它相当于XML配置文件中的<bean id=”xxx” class=”xxx”/>
- 当使用了Spring注解后,我们需要在配置文件中添加<context:component-scan/>来扫描添加了注解的类,这样子声明注解的类才能起作用
- Bean实例的名称默认是Bean类的首字母小写,其他部分不变。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.gxa.spring.day02"></context:component-scan> </beans>
package com.gxa.spring.day02; import org.springframework.stereotype.Component; @Component
public class StudentAnnotation {
}
package com.gxa.spring.test; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gxa.spring.day02.StudentAnnotation; public class Test02 { @Test
public void m06() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
StudentAnnotation studentAnnotation = context.getBean("studentAnnotation", StudentAnnotation.class);
System.out.println(studentAnnotation.hashCode());
} }
4. 除了@Component注解,Spring容器提供了3个功能和@Component注解等同。它们分别是用于对Dao,Service及Web层的Controller进行注解
- @Repository:用于对Dao实现类注解
- @Service:用于对Service实现类注解
- @Controller:用于对Controller实现类注解
5. @Autowired注解
- Spring2.5引入了@Autowired注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作(DI依赖注入工作)
- Spring通过@Autowired注解实现Bean之间的依赖关系
package com.gxa.spring.day02; import org.springframework.stereotype.Component; @Component
public class TeacherAnnotation {
public void getTeacherName() {
System.out.println("===刘老师好===");
}
}
package com.gxa.spring.day02; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class StudentAnnotation { /**
* 通过Autowired来自动装备Student所依赖的对象
* 无需创建setter方法
* 无需编写XML配置文件中<property/>
*/
@Autowired
private TeacherAnnotation teacherAnnotation; public void showTeacher() {
teacherAnnotation.getTeacherName();
}
}
- @Autowired(required = false),告诉 Spring:在找不到匹配 Bean 时也不报错
package com.gxa.spring.day02; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class StudentAnnotation { /**
* 注意TeamAnnotation没有加入@Component
* 当StudentAnnotation依赖TeamAnnotation会报错
* 但是如果加入@Autowired(required=false)就不会报错
*/
@Autowired(required=false)
private TeamAnnotation teamAnnotation; public void showTeacher() {
teacherAnnotation.getTeacherName();
}
}
6. @Qualifier指定注入Bean的名称
- 如果容器中有一个以上匹配的Bean时,则可以通过@Qualifier注解限定注入的Bean名称
package com.gxaedu.bean; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; @Component
public class Computer {
@Autowired
@Qualifier("storage01")
private Storage storage;
public void m01() {
storage.show();
}
}
package com.gxaedu.bean;
import org.springframework.stereotype.Component;
@Component("storage01")
public class Storage { //此类是在com.gxaedu.bean下的Storage
public void show() {
System.out.println("东芝硬盘");
}
}
package com.gxaedu.test; import org.springframework.stereotype.Component; @Component
public class Storage { //此类是在com.gxaedu.test下的Storage }
7. @Qualifier为方法指定注入Bean名称
package com.gxa.spring.day02; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; @Component
public class StudentAnnotation { private CourseAnnotation courseAnnotation; /**
* @Autowired来自动装备构造方法里面的对象
* @Qualifier来指定依赖对象的bean
* @paramcourseAnnotation
*/
@Autowired
public StudentAnnotation(@Qualifier("course") CourseAnnotation courseAnnotation) {
this.courseAnnotation = courseAnnotation;
} public void showCourse() {
System.out.println(courseAnnotation.hashCode());
}
}
8. @Scope注解来显式指定Bean作用范围
@Scope("prototype")
@Component
public class Car {
}
[刘阳Java]_Spring常用注解介绍_第6讲的更多相关文章
- [刘阳Java]_Spring相关配置介绍_第5讲
这一节我们介绍一下Spring框架的相关常用配置 Spring依赖注入的两种方式(构造方法注入和setter方式注入) p-namespace方式配置 properties属性文件配置方式 集合对象配 ...
- [刘阳Java]_Spring AOP注解详细介绍_第8讲
这节内容非常关键,我们会比较详细地介绍Spring AOP注解的使用 1. 要使用Spring AOP注解,必须满足如下的事项 导入Aspectj的jar.Spring3.0-AOP.jar.aopa ...
- [刘阳Java]_MyBatis_实体关系映射_第8讲
MyBatis既然是一个ORM框架,则它也有像Hibernate那样的一对多,多对多,多对一的实体关系映射功能.下面我们就来介绍一下如何使用MyBatis的实体关系映射 1.MyBatis实体关系映射 ...
- [刘阳Java]_SpringMVC访问静态资源_第9讲
有些时候我们在使用SpringMVC的时候造成无法访问静态资源文件(如:html,js,css,image等等).其主要的原因出在web.xml文件我们设置SpringMVC前端控制器的映射路径 &l ...
- [刘阳Java]_BeanNameViewResolver视图解析器_第8讲
BeanNameViewResolver:这个视图解析器跟XmlViewResolver有点类似,也是通过把返回的逻辑视图名称去匹配定义好的视图bean对象.它要求视图bean对象都定义在Spring ...
- [刘阳Java]_ResourceBundleViewResolver视图解析器_第7讲
ResourceBundleViewResolver是根据proterties文件来找对应的视图来解析"逻辑视图".该properties文件默认是放在classpath路径下的v ...
- [刘阳Java]_InternalResourceViewResolver视图解析器_第6讲
SpringMVC在处理器方法中通常返回的是逻辑视图,如何定位到真正的页面,就需要通过视图解析器 InternalResourceViewResolver是SpringMVC中比较常用视图解析器. 网 ...
- [刘阳Java]_Spring AOP基于XML配置介绍_第9讲
基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...
- [刘阳Java]_Spring AOP入门_第7讲
AOP技术个人认为是能够完善(改善)面向对象编程OOP.为什么这么说,我们得先从AOP的概念说起,然后通过一段简单的例子加以佐证.这样子大家就可以慢慢地了解AOP 1. AOP概念 AOP为Aspec ...
随机推荐
- JVM-栈帧之局部变量表
1.栈帧的内部结构 每个栈帧中存储着: 局部变量表(Local Variables) 操作数栈(Operand Stack)(或表达式栈) 动态链接(Dynamic Linking)(或指向运行时常量 ...
- mybatis——解决属性名和数据库字段名不一致问题
首先说一下,我的数据库名字叫mybatis,里边有一张user表,表中有三个字段,id.name.pwd:然后实体类中对应三个属性id.name.password(id和name跟数据库字段名一致,但 ...
- 保存数据到csv文件报错:Permission denied: './train_data.csv'
如果你此前已经输出,创建了文件,很有可能是你打开了此文件,导致写入不进去报错,关掉文件重新运行程序即可!
- P1522 [USACO2.4]牛的旅行 Cow Tours(Floyd)
题目描述 Farmer John 的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer Joh ...
- MySQL的启动选项和系统变量该如何配置?
MySQL的配置信息可以通过两种方式实现,一种是命令行形式,在启动MySQL服务时后边带上相关配置参数,此种方式会在MySQL重启后失效.另外一种是通过写入配置文件,如my.cnf,启动或者重启MyS ...
- 详解apollo的设计与使用
简介 apollo 是一款由携程团队开发的配置中心,可以实现配置的集中管理.分环境管理.即时生效等等.在这篇博客中,我们可以了解到: 为什么使用配置中心 如何设计一个配置中心 apollo 是如何设计 ...
- 「10.13」毛一琛(meet in the middle)·毛二琛(DP)·毛三琛(二分+随机化???)
A. 毛一琛 考虑到直接枚举的话时间复杂度很高,我们运用$meet\ in\ the\ middle$的思想 一般这种思想看似主要用在搜索这类算法中 发现直接枚举时间复杂度过高考虑枚举一半另一半通过其 ...
- Django(69)最好用的过滤器插件Django-filter
前言 如果需要满足前端各种筛选条件查询,我们使用drf自带的会比较麻烦,比如查询书名中包含"国"字,日期大于"2020-1-1"等等诸如此类的请求,Djan ...
- oracle中job无法正常运行,如何排查
1.生产环境Oracle中的job无法正常运行 select * from dba_jobs_running;(查看正在运行的job) 2.select * from dba_jobs(查看job历史 ...
- 支持向量机(SVM)之硬阈值
支持向量机 ( support vector machine, SVM ) 是使用超平面来对给定的 p 维向量进行分类的非概率二元线性分类器. 一.超平面 ( hyperplane ) 在一个p维的输 ...