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

最新文章

  1. ExtJs控件属性配置详细
  2. 慕课网-Java入门第一季-6-7 使用 Arrays 类操作 Java 中的数组
  3. web farm 讨论引出
  4. nusoap
  5. [Effective Java]第二章 创建和销毁对象
  6. Core Java Volume I — 4.5. Method Parameters
  7. xampp
  8. Ubuntu全盘备份与恢复,亲自总结,实测可靠
  9. python中模块的__all__详细使用
  10. mongodb导入导出数据
  11. Unity Lighting(一)光照练习
  12. 初识DOM(文档对象模型)
  13. linux 使用随笔
  14. CF86D
  15. 乐观锁的一种实现方式——CAS
  16. 转载: Centos7 升级python3,解决升级后不兼容问题
  17. 基于C#的超市收银管理系统
  18. RabbitMQ解决分布式事务
  19. Halum UVA - 11478 差分约束
  20. jQ中css()和addClass的区别之优先级

热门文章

  1. [iptables] 如何用iptables管理桥接模式下的设备
  2. Spring中的事务操作
  3. [js]函数的上级作用域,他的上级作用域就是谁,跟函数在哪执行的没什么关系.
  4. gitlab重置root的密码
  5. word之个人设置
  6. 【2】Kali之情报搜集技术
  7. javascript:void(0) 和 href=&quot;#&quot;的区别
  8. nodejs笔记之搭建服务器
  9. Html img 标签
  10. Python if条件判断