1、@Autowired

  • Autowired是用在JavaBean中的注解,通过byType形式,用来给指定的字段或方法注入所需的外部资源
  • Autowired注解来指定自动装配,可以修饰setter方法、普通方法、实例变量和构造器等
  • 当用来标注setter方法时,默认采用byType自动装配策略
@component
public class Chinese implments Person {
@Autowried
public void setAxe(Axe axe) {
this.axe = axe;
}
}
  • 修饰带多个参数的普通方法时,Spring会自动到容器中寻找类型匹配的Bean,如果恰好为每个参数都找到一个类型匹配的Bean,Spring会自动以这些Bean作为参数来调用该方法
@component
public class Chinese implments Person { @Autowried
public void setAxe(Axe axe, Dog dog) {
this.axe = axe;
this.dog = dog
}
...
}
  • 也可以用于修饰构造器和实例变量
  • 修饰一个实例变量时,Spring将会把容器中与该实例变量匹配的Bean设置为该实例变量的值
@component
public class Chinese implments Person {
@Autowried
private Axe axe;
@Autowried
public Chinese(Axe axe, Dog dog) {
this.axe = axe;
this.dog = dog
}
...
}
  • 也可以用于修饰数组类型的成员变量,Spring会自动搜索容器中的所有Axe实例,并以这些Axe实例作为数组元素来创建数组,最后将该数组付给axes
@Component
public class Chinese implements Person {
@Autowired
private Axe[] axes;
...
}
  • 与此类似的是,它也可以标注集合类型的实例变量,或者标注形参类型的集合方法,Spring对这种集合属性、集合形参的处理与上面对数组类型的处理是完全相同的
@Component
public class Chinese implements Person { private Set<Axe> axes;
@Autowired
public void setAxes(Set<Axe> axes) {
this.axes = axes;
}
...
}
  • 对于集合类型的参数而言,程序代码中必须是泛型,如上代码所示,程序指定了该方法的参数书Set类型,这表明Spring会自动搜索容器中的所有Axe实例,并将这些实例注入到axes实例变量中。如果程序没有使用泛型来指明集合元素的类型,则spring将会不知所措
public class BaseDaoImpl<T> implements BaseDaoImpl<T> {
public void save(T e) {
System.out.println("程序保存对象:" + e);
}
}

2、@Component

  • 标注一个普通的Spring Bean类

3、@Controller

  • 标注一个控制器组件类

4、@Service

  • 标注一个业务逻辑组件类

5、@Repository

  • 标注一个DAO组件类

6、@Scope

  • 指定Bean实例的作用域,默认是singleton,当采用零配置方式来管理Bean实例是,可使用@Scope Annotation,只要在该Annotation中提供作用域的名称即可

7、@Resource

  • 为目标Bean指定协作者Bean
@Component
public class Chinese implements Person {
private Axe axe; @Resource(name="stoneAxe")
public void setAxe(Axe axe) {
this.axe = axe;
}
}

8、@PostConstruct 和@PreDestroy

  • 定制声明周期行为
  • PostConstruct指定初始化行为
  • PreDestroy指定销毁行为

9、@DependsOn

  • 可以修饰Bean类或方法,使用时可以指定一个字符串数组作为参数,每个数组元素对应于一个强制初始化的Bean
@DependsOn({"steelAxe","abc"})
@Component
pulic class Chinese implements Person {
...
}

10、@Lazy

  • 修饰Spring Bean类用于指定该Bean的预初始化行为,使用时可以指定一个boolen类型的value值,该属性决定是否要预初始化该Bean
@Lazy(true)
@component
public class Chinese implements Person { }

11、@Qualifier

  • 允许根据Bean的id来执行自动装配
@Component
pulic class Chinese implements Person {
@Autowired
@Qualifier("steelAxe")
private Axe axe;
public void setAxe(Axe axe) {
this.axe = axe;
} public void useAxe() {
System.out.println(axe.chop());
}
}
  • 上面的注解指定了axe实例变量将使用自动装配,且精确指定了被装配的Bean实例名称是steelAxe
  • 如果使用@Autowired和@Quelifier实现精确的自动装配,还不如直接使用@Resource注解执行依赖注入

Spring core注解的更多相关文章

  1. spring @condition 注解

    spring @condition注解是用来在不同条件下注入不同实现的 demo如下: package com.foreveross.service.weixin.test.condition; im ...

  2. Spring JSR-250注解

    Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...

  3. Spring _day02_IoC注解开发入门

    1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...

  4. spring+mybatise注解实现

    spring+mybatise注解实现 spring.jpa.database=MYSQL spring.datasource.type=com.alibaba.druid.pool.DruidDat ...

  5. Spring的注解@Qualifier

    近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...

  6. spring @Required注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-required-ann ...

  7. Spring常用注解式开发

    1.组件注册@Configuration.@Bean给容器中注册组件. 注解,@Configuration告诉Spring这是一个配置类,相当于bean.xml配置文件. 注解,@Bean给Sprin ...

  8. Spring使用注解实现AOP

    一.AspectJ概述 AspectJ是一个面向切面的框架,它扩展了Java语言.定义了AOP语法,能够在编译期提供代码的织入,它提供了一个专门的编译期用来生成遵守字节编码规范的Class文件. @A ...

  9. spring基于注解的事务控制

    pom配置: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

随机推荐

  1. JSF通过超链接传递参数到控制层

    JSF通过超链接传输数据到控制层可以分为三个步骤: 1.首先将数据加入到超链接中.通过<h:link>标签加入一个超链接,然后加入<f:param>标签加入参数.示例代码如下: ...

  2. java代码输出质因数

    package com.badu; import java.util.Scanner; //分解质因数问题: //从键盘输一个数, //首先最小质因数为2 //n不能被2整除时, //n能被2整除时, ...

  3. ManualResetEvent 用法

    第一.简单介绍 ManualResetEvent 允许线程通过发信号互相通信.通常,此通信涉及一个线程在其他线程进行之前必须完成的任务.当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时, ...

  4. .netcore 在linux 上运行 Helllo World 例子

    要想在linux上运行netcore ,需要在linux 安装netcore SKD 下面分两步完成, 首先我的操作系统是 Ubuntu 14.04 ,不同版本可以参考 官网给出的连接 https:/ ...

  5. [转]在 Windows 操作系统中的已知安全标识符(Sid security identifiers)

    安全标识符 (SID) 是用于标识安全主体或安全组在 Windows 操作系统中的可变长度的唯一值.常用 Sid 的 Sid 标识普通用户的一组或通用组.跨所有操作系统,它们的值保持不变. 此信息可用 ...

  6. jhipster初接触

    在Windows7部署之前把几个依赖下了 jdk:1.80 Maven :3.3.9 git:2.14.1 npm:唯一要注意的就是配置一个阿里的镜像,不然慢的你崩溃 Yeoman: npm inst ...

  7. 类型:.net;问题:ASP.NET路由;结果:ASP.NET 路由 .NET Framework 4

    ASP.NET 路由 .NET Framework 4   更新:2007 年 11 月 ASP.NET 路由使您可以使用不必映射到网站中特定文件的 URL.由于 URL 不必映射到文件,所以可以在 ...

  8. 问题:只能在执行 Render() 的过程中调用 RegisterForEventValidation;结果:只能在执行 Render() 的过程中调用 RegisterForEventValidation

    只能在执行 Render() 的过程中调用 RegisterForEventValidation 当在导出Execl或Word的时候,会发生只能在执行 Render() 的过程中调用 Register ...

  9. oracle时间段查询-从00:00:00开始

    之所以记录一下这篇博文,是因为前段时间搞的一个查询发现要从00:00:00这个时间段开始,必须要通过拼接字符串. <select id="queryApplyProgressList& ...

  10. Developer tools

    20. Developer tools Spring Boot includes an additional set of tools that can make the application de ...