基于Java的配置

@Configuration & @Bean Annotations

使用@Configuration注释类表示,Spring IoC容器可以将该类用作bean定义的源。@Bean注释告诉Spring,用@Bean注释的方法将返回一个应该在Spring应用程序上下文中注册为bean的对象。最简单的@Configuration类如下所示:

package com.tutorialspoint;
import org.springframework.context.annotation.*; @Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}

它和以下的XML方式定义的是等价的:

<beans>
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld" />
</beans>

带@Bean的方法名作为bean id注释,他创建并返回实际的bean。一个配置类可以拥有多个Bean的声明。一旦定义了配置类,你可以通过 AnnotationConfigApplicationContex加载并获取

他们。

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class); HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}

也可以获取加载不同的configuration

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();
}

Example

HelloWorldConfig.java

@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}

HelloWorld.java

public class HelloWorld {
private String message; public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}

MainApp.java

public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class); HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}

输出:

Your Message : Hello World!

注入Bean依赖

当@ bean相互依赖时,表示依赖关系就像让一个bean方法调用另一个bean一样简单,如下所示

@Configuration
public class AppConfig {
@Bean
public Foo foo() {
return new Foo(bar());
} @Bean
public Bar bar() {
return new Bar();
}
}

foo bean通过构造函数注入接收到bar的引用

Example

TextEditorConfig.java

@Configuration
public class TextEditorConfig {
@Bean
public TextEditor textEditor(){
return new TextEditor( spellChecker() );
} @Bean
public SpellChecker spellChecker(){
return new SpellChecker( );
}
}

TextEditor.java

public class TextEditor {
private SpellChecker spellChecker; public TextEditor(SpellChecker spellChecker){
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}

SpellChecker.java

public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}

MainApp.java

public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(TextEditorConfig.class); TextEditor te = ctx.getBean(TextEditor.class);
te.spellCheck();
}
}

输出:

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.

@Import注解

@Import注解允许在一个Configuration中导入另外一个配置类。

@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B a() {
return new A();
}
}

这样,只需要加载ConfigB,则可以加载A,B两个配置文件,而不需要一样加载两次.

Lifecycle Callbacks(声明周期回调)

@bean注释支持指定任意的初始化和销毁回调方法,就像Spring XML的init方法和销毁方法。

public class Foo {
public void init() {
// initialization logic
}
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init", destroyMethod = "cleanup" )
public Foo foo() {
return new Foo();
}
}

指定Bean的作用域

默认作用域是singleton,可以通过以下方法重写:

@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Foo foo() {
return new Foo();
}
}

Spring入门学习笔记(2)——基于Java的配置的更多相关文章

  1. Spring入门(8)-基于Java配置而不是XML

    Spring入门(8)-基于Java配置而不是XML 本文介绍如何应用Java配置而不是通过XML配置Spring. 0. 目录 声明一个简单Bean 声明一个复杂Bean 1. 声明一个简单Bean ...

  2. (4.1)Spring MVC执行原理和基于Java的配置过程

    一.Spring MVC执行原理和基于Java配置的配置过程 (一)Spring MVC执行过程,大致为7步. 所有的请求都会经过Spring的一个单例的DispacherServlet. Dispa ...

  3. Spring MVC执行原理和基于Java的配置过程

    一.Spring MVC执行原理和基于Java配置的配置过程 (一)Spring MVC执行过程,大致为7步. 所有的请求都会经过Spring的一个单例的DispacherServlet. Dispa ...

  4. Spring入门学习笔记(1)

    目录 Spring好处 依赖注入 面向面编程(AOP) Spring Framework Core Container Web Miscellaneous 编写第一个程序 IoC容器 Spring B ...

  5. [spring入门学习笔记][spring的IoC原理]

    什么叫IoC 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency ...

  6. [Spring入门学习笔记][创建网站URL]

    设计网站的URL 现代的Web站点都会设计一套拥有明确意义,方便用户记忆的URL,不论是域名还是路径,以天码营为例: http://tianmaying.com/courses表示网站下所有的课程列表 ...

  7. [Spring入门学习笔记][Spring Boot]

    什么是Spring Boot Spring Boot正是在这样的一个背景下被抽象出来的开发框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...

  8. [Spring入门学习笔记][静态资源]

    遗留问题 在上一节课的作业中,我们一定遇到了一点问题——虽然将页面内容正确的返回给了浏览器,但是浏览器显示的样式却是不正确的,这是因为在HTML的\标签中我们这样引入了CSS资源: <link ...

  9. [Spring入门学习笔记][Spring的AOP原理]

    AOP是什么? 面向切面编程 软件工程有一个基本原则叫做“关注点分离”(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年头互联网也 ...

随机推荐

  1. golang第三方库goconfig的使用

    参考地址:​http://studygolang.com/articles/818 详细的解析可以看上面链接,这里只做一点简单介绍 先安装好包,然后导入 import (    "githu ...

  2. P2607 [ZJOI2008]骑士

    题目描述 Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬. 最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火绵延五百里, ...

  3. JAVA开发微信支付-公众号支付/微信浏览器支付(JSAPI)

    写这篇文章的目的有2个,一是自己的项目刚开发完微信支付功能,趁热回个炉温习一下,二也是帮助像我这样对微信支付不熟悉,反复看了多天文档还是一知半解,原理都没摸清,更不要说实现了.本以为网上的微信开发教程 ...

  4. nagios 在nrpe中自定义脚本

    监控第三方端口(22000) #!/bin/bash#author:xiaoweige#check 140 22000 result=`sleep 1|telnet 10.2.1.140 22000| ...

  5. CAN总线实际运用分析问题。

    组态设计   人机交互  上位机  分布式控制系统  下位机  (单片机/PLC)  CAN总线用线缆   连接方式(手牵手,T型)    CAN总线接地(大地) http://bbs.gongkon ...

  6. 【gulp】Gulp的安装和配置 及 系列插件

    注意:要安装俩次gulp(全局和本地):全局安装gulp是为了执行gulp任务,本地安装gulp则是为了调用gulp插件的功能. 之前由大牛帮忙配置的gulp来用.今天时间充裕,就和小伙伴一起动手配置 ...

  7. C库函数重定向问题

    C库函数重定向用户能定义自己的C语言库函数,连接器在连接时自动使用这些新的功能函数.这个过程叫做重定向C语言库函数,如下图所示.举例来说,用户有一个I/O设备(如UART).本来库函数fputc()是 ...

  8. python3通过纯真IP数据库查询IP归属地信息

    在网上看到的别人写的python2的代码,修改成了python3. 把纯真IP数据库文件qqwry.dat放到czip.py同一目录下. #! /usr/bin/env python # -*- co ...

  9. c# 访问postgressql,使用pghelper访问pgsql

    由于工作需要,数据库是postgressql的,本来以为很简单的,结果弄了一晚上,为了总结经验,现将C#连接PGSQL(postgres sql)的资料整理如下. 一.总体思路 1.通过第三方Npgs ...

  10. 英语linux+英语firefox+英语Oracle OEM如何设置成显示日语

    1 linux安装盘挂载,安装日语语言包 2 linux的系统语言设置为日语 3 firefox的 edit-> setting -> contents -> language se ...