spring/java ---->记录和整理用过的注解以及spring装配bean方式
spring注解
@Scope:该注解全限定名称是:org.springframework.context.annotation.Scope。@Scope指定Spring容器如何创建Bean的实例,Singleton(spring默认的创建Bean实例的方式),Prototype,Request,Session,GlobalSession。
@Bean:该注解全限定名称是:org.springframework.context.annotation.Bean。在方法上使用该注解,表示方法返回值是一个Bean。和注解@Configuration使用可实现Java配置。
@ComponentScan:该注解全限定名称是:org.springframework.context.annotation.ComponentScan。自动扫描包名下所有使用了声明式注解的类,并注册为Bean。
@Configuration:该注解全限定名称是:org.springframework.context.annotation.Configuration。声明当前类是一个配置类。和注解@Bean使用可实现Java配置。
@PropertySource:该注解全限定名称是:org.springframework.context.annotation.PropertySource。指定配置文件的位置。
@Primary:该注解全限定名称是:org.springframework.context.annotation.Primary。注解在方法上。当按类型自动装配可能会导致多个候选者,@Primary可以实现优先选择特定的bean。
@Profile:该注解全限定名称是:org.springframework.context.annotation.Profile。注解在类或者方法上。实现在不同情况下实例化不同的Bean。可以实现根据环境的不同使用不同的配置。
@Conditional:该注解全限定名称是:org.springframework.context.annotation.Conditional。实现在满足一定条件的情况下创建一个特定的Bean。
@Async:该注解全限定名称是:org.springframework.scheduling.annotation.Async。可以注解在方法或者类上。表示该方法是异步的。
@EnableAsync:该注解全限定名称是:org.springframework.scheduling.annotation.EnableAsync。开启对异步任务支持。
@Scheduled:该注解全限定名称是:org.springframework.scheduling.annotation.Scheduled。注解在方法上。表示该方法是计划任务。
@EnableScheduling:该注解全限定名称是:org.springframework.scheduling.annotation.EnableScheduling。开启对计划任务支持。
@Value:该注解全限定名称是:org.springframework.beans.factory.annotation.Value。@Value注入各种资源,例如,字符串,配置文件,操作系统属性等。
@Autowired:该注解全限定名称是:org.springframework.beans.factory.annotation.Autowired。注入Bean。可在set方法或者属性上使用。
@Service:该注解全限定名称是:org.springframework.stereotype.Service。声明Bean。业务逻辑层使用。
@Repository:该注解全限定名称是:org.springframework.stereotype.Repository。声明Bean。持久层使用。
@Controller:该注解全限定名称是:org.springframework.stereotype.Controller。声明Bean。控制层使用。
@Component:该注解全限定名称是:org.springframework.stereotype.Component。声明Bean。组件,角色不明确。
@Enable* 开启某一项功能。
@EnableAspectJAutoProxy:开启Spring对AspectJ的支持。
@EnableAsync:开启对异步方法的支持。
@EnableScheduling:开启对计划任务的支持。
@EnableWebMvc:开启对web mvc的配置支持。
@EnableJpaRepositories:该注解全限定名称是:org.springframework.data.jpa.repository.config.EnableJpaRepositories。开启对Spring data Jpa repository的支持。
@EnableTransactionManagement:该注解全限定名称是:org.springframework.transaction.annotation.EnableTransactionManagement。开启注解式事务的支持。
@EnableCaching:开启注解式缓存的支持。
。。。
上面这些注解的共同特点是都是组合注解,都有一个@Import注解,用它来导入配置类
mvc
@EnableWebMvc:该注解全限定名称是:org.springframework.web.servlet.config.annotationEnableWebMvc。开启对web mvc的配置支持。
@RequestMapping:该注解全限定名称是:org.springframework.web.bind.annotation.RequestMapping。注解在类或者方法上。映射web请求路径。
@ResponseBody:该注解全限定名称是:org.springframework.web.bind.annotation.ResponseBody。注解在方法上或者返回值前。将返回的结果放在response中,而不是放在一个页面中。
@RequestBody:该注解全限定名称是:org.springframework.web.bind.annotation.RequestBody。注解在参数前面。将请求的参数放在request中,而不是放在url中。
@PathVariable:该注解全限定名称是:org.springframework.web.bind.annotation.PathVariable。注解在参数前面。获得url路径中的动态参数。
@RequestParam:该注解全限定名称是:org.springframework.web.bind.annotation.RequestParam。注解在参数前面。将url请求中的参数(key=value)赋值给方法中的形式参数。
@Controller:该注解全限定名称是:org.springframework.stereotype.Controller。声明Bean。控制层使用。
@RestController:该注解全限定名称是:org.springframework.web.bind.annotation.RestController。@ResponseBody和@Controller的组合注解。注解在类或者方法上。将返回的结果放在response中,而不是放在一个页面中。
全局处理
@ControllerAdvice:该注解全限定名称是:org.springframework.web.bind.annotation.ControllerAdvice。可以对控制器进行全局配置。
@ExceptionHandler:该注解全限定名称是:org.springframework.web.bind.annotation.ExceptionHandler。用于全局处理控制器的异常。
@ModelAttribute:该注解全限定名称是:org.springframework.web.bind.annotation.ModelAttribute。1、绑定键值对到model对象。2、让全局的RequestMapping获得此处设置的键值对。
test
@ContextConfiguration:该注解全限定名称是:org.springframework.test.context.ContextConfiguration。加载配置ApplicationContext。
@ActiveProfiles:该注解全限定名称是:org.springframework.test.context.ActiveProfiles。确定参与测试时活动的Profile。
@WebAppConfiguration :该注解全限定名称是:org.springframework.test.context.web.WebAppConfiguration。用来声明加载的ApplicationContext是一个WebApplicationContext。
java注解
JSR-250:
@PostConstruct:该注解全限定名称是:javax.annotation.PostConstruct。用于Bean的生命周期的操作。在构造方法执行之后执行。
@PreDestroy:该注解全限定名称是:javax.annotation.PreDestroy。用于Bean的生命周期的操作。在销毁方法执行之前执行。
@Resource:该注解全限定名称是:javax.annotation.Resource。注入Bean。可在set方法或者属性上使用。
JSR-330
@Inject:该注解全限定名称是:javax.inject.Inject。注入Bean。可在set方法或者属性上使用。
test(junit)
@RunWith:该注解全限定名称是:org.junit.runner.RunWith。在Junit环境下提供一个spring测试上下文环境。
@Test:该注解全限定名称是:org.junit.Test。注解在方法上。表示这是需要进行测试的。
@Before:该注解全限定名称是:org.junit.Before。注解在方法上。表示进行测试之前需要做的准备,加载配置文件等。
上面的注解所在的jar:
spring-context-4.3.18.RELEASE.jar
spring-beans-4.3.18.RELEASE.jar
spring-web-4.3.18.RELEASE.jar
spring-webmvc-4.3.18.RELEASE.jar
spring-test-4.3.18.RELEASE.jar
junit-4.12.jar
jsr250-api-1.0.jar
javaee-api-7.0.jar
spring装配方式
1、通过xml进行显式装配:xml文件+java文件
xml配置
<bean id="abc" class="a.b.C"/> <bean id="xyz" class="x.y.Z"> <properpty name="c" ref="abc"/> <!--引用bean abc--> </bean>
java文件:指定set方法
package x.y; import a.b.C; public class Z{ //注入类a.b.C private C c; public void setC(C c){ this.c = c; } }
2、通过注解方式装配bean
xml配置
<context:component-scan base-package="a.b,x.y"/> <!--开启注解扫描--> <bean id="abc" class="a.b.C"/> <bean id="xyz" class="x.y.Z"/> <!--没有引用bean abc-->
java文件
1 package x.y; 2 3 import a.b.C; 4 import org.springframework.beans.factory.annotation.Autowired; 5 public class Z{ //注入类a.b.C 6 7 private C c; 8 9 @Autowired //由@Autowired注入类C,方法级别上的注解??? 10 public void setC(C c){ 11 this.c = c; 12 } 13 14 }
2018-10-19 15:55:18
注解大pk
@Resources和@Autowired
@Resources
@Autowired
spring/java ---->记录和整理用过的注解以及spring装配bean方式的更多相关文章
- Spring常用注解,自动扫描装配Bean
1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...
- Spring学习(17)--- 三种装配Bean方式比较
基于XML配置 基于注解配置 基于Java类配置 Bean定义 <bean id="..." class="..." /> @Compone ...
- Java EE 学习(8):IDEA + maven + spring 搭建 web(4)- 用户管理
转载:Gaussic(一个致力于AI研究却不得不兼顾项目的研究生) 注:在阅读本文前,请先阅读: Java EE 学习(5):IDEA + maven + spring 搭建 web(1) ava E ...
- Java EE 学习(9):IDEA + maven + spring 搭建 web(5)- 博客文章管理
转载:Gaussic(一个致力于AI研究却不得不兼顾项目的研究生) . 注:在阅读本文前,请先阅读: Java EE 学习(5):IDEA + maven + spring 搭建 web(1) Jav ...
- 尚学堂Java面试题整理
博客分类: 经典分享 1. super()与this()的差别? - 6 - 2. 作用域public,protected,private,以及不写时的差别? - 6 - 3. 编程输出例如以 ...
- 我的Spring学习记录(五)
在我的Spring学习记录(四)中使用了注解的方式对前面三篇做了总结.而这次,使用了用户登录及注册来对于本人前面四篇做一个应用案例,希望通过这个来对于我们的Spring的使用有一定的了解. 1. 程序 ...
- java 面试题整理(不定期更新)
一.Java基础 1.Java面向对象的三个特征与含义 三大特征是:封装.继承和多态. 封装是指将某事物的属性和行为包装到对象中,这个对象只对外公布需要公开的属性和行为,而这个公布也是可以有选择性的公 ...
- 《OD面试》Java面试题整理
一.面试考察点 1 主语言本身 2 数据库 3 算法 4 Spring/SpringMVC/MyBatis 5 项目经验 1)项目涉及到的技术点深挖: (1)考察候选人技术深度 (2)看候选人遇到问 ...
- 字节跳动Java研发面试99题(含答案):JVM+Spring+MySQL+线程池+锁
JVM的内存结构 根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 1. Java虚拟机栈:线程私有:每个方法在执行的时候会创建一个栈帧,存储了局部变量表, ...
随机推荐
- VS统计项目代码行数
ctrl + shift + F 输入:b*[^:b#/]+.*$ 选项图如下
- ZBX_NOTSUPPORTED: Cannot obtain filesystem information: [13] Permission denied
zabbix有默认两条自动发现规则,其中一条是自动发现已挂载文件系统,但笔者的三个挂载文件系统中两个监控成功了,一个失败 agentd端挂载情况: 仔细研究sdb1的挂载点,发现它是挂载在xiami用 ...
- JavaWeb & Tomcat
1 JavaWeb概述 Java在服务器端的应用有Servlet,JSP和第三方框架等. Java的Web框架基本都遵循特定的路数:使用Servlet或者Filter拦截请求,使用MVC的思想设计架构 ...
- python之SQLAlchemy组件
介绍 SQLAlchemy 是一个 ORM 框架,可以帮助我们使用面向对象的方式快速实现数据库操作. 组成部分: Engine,框架的引擎 Connection Pooling ,数据库连接池 Dia ...
- python基础之 编码进阶,文件操作和深浅copy
1.编码的进阶 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码.即先将其他编码的字符串解码(decode)成unicode,再从unic ...
- Eclipse Error Reporting Welcome to the Eclipse Error Reporting Service.Do you want to help Eclipse? Enable Disable
在开发的时候,使用Eclipse IDE,提示如下信息, 这是Eclipse的错误报告,如果不想发送,可以关闭掉,关闭方法: 选择Preferences -> General -> Err ...
- Docker 基础 (二)
网络管理 容器网络模式 Docker支持5种网络模式 bridge 默认网络,Docker启动后默认创建一个docker0网桥,默认创建的容器也是添加到这个网桥中 host 容器不会获得一个独立的n ...
- GDScript 格式化字符串
GDScript offers a feature called format strings, which allows reusing text templates to succinctly c ...
- 配置 ROS 的 apt 源
配置 ROS 的 apt 源 ROS的apt源有官方源.国内 USTC 源或新加坡源可供选择, 选择其一就可以了,建议使用国内 USTC 源或新加坡源,安装速度会快很多. 方式一:官方源 $ sudo ...
- nginx的启动、停止、重载配置、验证配置
[1]启动 启动nginx系统方式: (1)命令 nginx -c /usr/local/nginx/conf/nginx.conf 说明:-c 参数指定运行nginx系统的自定义配置文件. 若加:使 ...