从 Spring 2.5 开始就可使用注解来配置依赖注入。而不是采用 XML 来描述一个 bean 连线,你可使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类自己。java
<context:annotation-config/>spring
@Required | bean类的 setter 方法(bean必须写这个属性)ide |
@Autowired | 注解能够应用到 bean 属性的 setter 方法(bytype),非 setter 方法,构造函数(自动调用构造函数并匹配参数)和属性(能够省略setter方法)。 |
@Qualifier | 经过指定确切的将被连线的 bean,@Autowired 和 @Qualifier 注解能够用来删除混乱。函数 |
JSR-250 | Spring 支持 JSR-250 的基础的注解,其中包括了 @Resource,@PostConstruct 和 @PreDestroy 注解。ui |
@Required 注释应用于 bean 属性的 setter 方法,它代表受影响的 bean 属性在配置时必须放在 XML 配置文件中,不然容器就会抛出一个 BeanInitializationException 异常。下面显示的是一个使用 @Required 注释的示例。this
@Required public void setAge(Integer age) { this.age = age; }
@Required 注释应用于 bean 属性的 setter 方法,它代表受影响的 bean 属性在配置时必须放在 XML 配置文件中,不然容器就会抛出一个 BeanInitializationException 异常。spa
@Autowired 注释对在哪里和如何完成自动链接提供了更多的细微的控制。prototype
@Autowired 注释能够在 setter 方法中被用于自动链接 beancode
当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中视图执行 byType 自动链接。xml
@Autowired public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; }
你能够在属性中使用 @Autowired 注释来除去 setter 方法。当使用 为自动链接属性传递的时候,Spring 会将这些传递过来的值或者引用自动分配给那些属性。因此利用在属性中 @Autowired 的用法,你的TextEditor.java 文件将变成以下所示:
package com.tutorialspoint; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { @Autowired private SpellChecker spellChecker; public TextEditor() { System.out.println("Inside TextEditor constructor." ); } public SpellChecker getSpellChecker( ){ return spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); } }
<bean id="textEditor" class="com.tutorialspoint.TextEditor"> </bean> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean>
你也能够在构造函数中使用 @Autowired。一个构造函数 @Autowired 说明当建立 bean 时,即便在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动链接。让咱们检查一下下面的示例。
即便bean配置是没说明使用的是构造方法,可是构造函数也会自动链接,进行匹配参数注入
默认状况下,@Autowired 注释意味着依赖是必须的,它相似于 @Required 注释,然而,你可使用 @Autowired 的 (required=false) 选项关闭默认行为。
即便你不为 age 属性传递任何参数,下面的示例也会成功运行,可是对于 name 属性则须要一个参数。你能够本身尝试一下这个示例,由于除了只有 Student.java 文件被修改之外,它和 @Required 注释示例是类似的。
@Autowired(required=false) public void setAge(Integer age) { this.age = age; } @Autowired public void setName(String name) { this.name = name; }
可能会有这样一种状况,当你建立多个具备相同类型的 bean 时,而且想要用一个属性只为它们其中的一个进行装配,在这种状况下,你可使用 @Qualifier 注释和 @Autowired 注释经过指定哪个真正的 bean 将会被装配来消除混乱。下面显示的是使用 @Qualifier 注释的一个示例。
@Autowired @Qualifier("student1") private Student student;
到目前为止,你已经看到如何使用 XML 配置文件来配置 Spring bean。若是你熟悉使用 XML 配置。
基于 Java 的配置选项,可使你在不用配置 XML 的状况下编写大多数的 Spring,可是一些有帮助的基于 Java 的注解,解释以下:
带有 @Configuration 的注解类表示这个类可使用 Spring IoC 容器做为 bean 定义的来源。@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。最简单可行的 @Configuration 类以下所示:
@Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } }
带有 @Bean 注解的方法名称做为 bean 的 ID,建立并返回实际的 bean。配置类能够声明多个 @Bean。一旦定义了配置类,你就可使用 AnnotationConfigApplicationContext 来加载并把他们提供给 Spring 容器,以下所示:
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class); HelloWorld helloWorld = ctx.getBean(HelloWorld.class); helloWorld.setMessage("Hello World!"); helloWorld.getMessage(); }
你能够加载各类配置类,以下所示:
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class, OtherConfig.class); ctx.register(AdditionalConfig.class); ctx.refresh(); MyService myService = ctx.getBean(MyService.class); myService.doStuff(); }
@import 注解容许从另外一个配置类中加载 @Bean 定义。考虑 ConfigA 类,以下所示:
@Configurationpublic class ConfigA { @Bean public A a() { return new A(); } }
能够在另外一个 Bean 声明中导入上述 Bean 声明,以下所示:
@Configuration@Import(ConfigA.class)public class ConfigB { @Bean public B a() { return new A(); } }
如今,当实例化上下文时,不须要同时指定 ConfigA.class 和 ConfigB.class,只有 ConfigB 类须要提供,以下所示:
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class); // now both beans A and B will be available... A a = ctx.getBean(A.class); B b = ctx.getBean(B.class); }
@Bean 注解支持指定任意的初始化和销毁的回调方法,就像在 bean 元素中 Spring 的 XML 的初始化方法和销毁方法的属性:
@Configuration public class AppConfig { @Bean(initMethod = "init", destroyMethod = "cleanup" ) public Foo foo() { return new Foo(); }
指定 Bean 的范围:
默认范围是单实例,可是你能够重写带有 @Scope 注解的该方法,以下所示:
@Configuration public class AppConfig { @Bean @Scope("prototype") public Foo foo() { return new Foo(); } }