@Configuration 标注在类上,启动 Spring 会自动扫描@Configuration注解的类,将其注册到IOC容器并实例化bean对象。如果在@Configuration注解的类中使用@Bean注解某个类对象的方法,Spring也会自动将注解了@Bean的方法注册到IOC容器,并进行实例化。
@Configuration 注解本质上是个 @Component 注解,所以被 @Configuration 标注的类会被注册到IOC,且可以被 @ComponentScan 注解扫描到。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
/**
* 存入到Spring IOC容器中的ID
*/
@AliasFor(annotation = Component.class)
String value() default "";
/**
* 表示被@Configuration注解的类是否被代理,以及配置类中被@Bean注解的方法生成的Bean
* 在IOC容器中是否为单例对象
*
* true:full全局模式(默认)
* false:lite轻量级模式
*
* full全局模式,被@Configuration注解的配置类会被代理(CGLIB实现),配置类中被@Bean
* 注解的方法生成的Bean在IOC容器中是单例模式。也就是说,无论调用多少次被@Bean标注的
* 方法,返回的都是同一个bean对象。
*
* lite轻量级模式,被@Configuration注解的配置类不会被代理,配置类中被@Bean注解的方法
* 生成的Bean在IOC容器中也不是单例模式。也就是说,每次调用被@Bean注解标注的方法时,都会
* 返回一个新的Bean对象。
*
* @since 5.2(Spring 5.2版本加入)
*/
boolean proxyBeanMethods() default true;
/**
* 表示使用@Bean注解标注的方法是否需要唯一的方法名。
*
* true:使用@Bean注解标注的方法具有唯一方法名称,且方法名称不会重叠
* false:使用@Bean注解标注的方法不唯一,存在重叠风险
*
* 默认为true。
*
* @since 6.0(Spring 6.0版本加入)
*/
boolean enforceUniqueMethods() default true;
}
当某个类被@Configuration注解标注时,说明这个类是配置类。可以在这个类中,使用@Bean注解,向IOC容器中注入Bean对象;也可以使用 @Autowrite、@Resource、@Inject等注解来注入所需要的Bean对象。
另外,在使用 AnnotationConfigApplicationContext 类创建IOC容器事,需要注意两点:
@Configuration 可以省略,但是如果省略 @Configuration ,每次调用配置类中被 @Bean 标注的方法时,都会返回不同的 Bean 实例对象。@Configuration 不可以省略。两种构造方法如下:
/**
* Create a new AnnotationConfigApplicationContext, deriving bean definitions
* from the given component classes and automatically refreshing the context.
* @param componentClasses one or more component classes — for example,
* {@link Configuration @Configuration} classes
*/
public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
this();
register(componentClasses);
refresh();
}
/**
* Create a new AnnotationConfigApplicationContext, scanning for components
* in the given packages, registering bean definitions for those components,
* and automatically refreshing the context.
* @param basePackages the packages to scan for component classes
*/
public AnnotationConfigApplicationContext(String... basePackages) {
this();
scan(basePackages);
refresh();
}
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Configuration
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
public class ConfigurationAnnotationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationAnnotationTest.class);
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigurationAnnotationConfig.class);
ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
Person person1 = config.person();
Person person2 = config.person();
LOGGER.info("person1 是否等于 person2 ===>> {}", (person1 == person2));
}
}
在之前提到,proxyBeanMethods配置表示用 @Bean 注解的方法在IOC容器中是否为单例对象,默认为true。
默认情况下,打印出结果如下:
person1 是否等于 person2 ===>> true
修改一下proxyBeanMethods的值为false:
@Configuration(proxyBeanMethods = false)
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
打印结果如下:
person1 是否等于 person2 ===>> false
从输出结果可以看出,当@Configuration中的proxyBeanMethods属性为false时,每次调用@Configuration注解标注类中被@Bean标注的方法时,都会返回不同的Bean实例对象。
调用AnnotationConfigApplicationContext类的构造方法传入配置类的Class对象创建IOC容器时,可以省略配置类上的@Configuration注解,如下:
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
输出结果:
person1 是否等于 person2 ===>> false
可以看到,若省略配置类上的@Configuration注解,则每次调用配置类中被@Bean注解标注的方法时,都会返回不同的Bean实例对象,与@Configuration中设置proxyBeanMethods的属性为false的效果相同。
调用AnnotationConfigApplicationContext类的构造方法传入包名创建IOC容器时,不能省略配置类上的@Configuration注解:
public class ConfigurationAnnotationConfig {
@Bean
public Person person(){
return new Person();
}
}
执行函数:
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("io.binghe.spring.annotation.chapter01.configuration");
ConfigurationAnnotationConfig config = context.getBean(ConfigurationAnnotationConfig.class);
Person person1 = config.person();
Person person2 = config.person();
LOGGER.info("person1 是否等于 person2 ===>> {}", (person1 == person2));
}
此时运行main方法,会发生报错:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.binghe.spring.annotation.chapter01.configuration.config.ConfigurationAnnotationConfig' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1148)
at io.binghe.spring.annotation.chapter01.configuration.ConfigurationAnnotationTest.main(ConfigurationAnnotationTest.java:36)
添加上@Configuration注解则程序执行正常。
Spring在 BeanFactory 的基础上提供一些具体容器的实现。AnnotationConfigApplicationContext就是一个用来管理注解 Bean 的容器。如下结构图:

从图中可以看到,AnnotationConfigApplicationContext继承GenericApplicationContext(通用应用上下文),而GenericApplicationContext又实现了BeanDefinitionRegistry接口,所以可以通过AnnotationConfigApplicationContext实例类注册BeanDefintion,然后调用refresh()方法来初始化上下文。AnnotationConfigApplicationContext还继承了AbstractApplicationContext,而AbstractApplicationContext提供了ApplicationContext的抽象实现
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在使用ruby1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und
一、引擎主循环UE版本:4.27一、引擎主循环的位置:Launch.cpp:GuardedMain函数二、、GuardedMain函数执行逻辑:1、EnginePreInit:加载大多数模块int32ErrorLevel=EnginePreInit(CmdLine);PreInit模块加载顺序:模块加载过程:(1)注册模块中定义的UObject,同时为每个类构造一个类默认对象(CDO,记录类的默认状态,作为模板用于子类实例创建)(2)调用模块的StartUpModule方法2、FEngineLoop::Init()1、检查Engine的配置文件找出使用了哪一个GameEngine类(UGame
转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev
我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("
这是我的网络应用:classFront我是这样开始的(请不要建议使用Rack):Front.start!这是我的Puma配置对象,我不知道如何传递给它:require'puma/configuration'Puma::Configuration.new({log_requests:true,debug:true})说真的,怎么样? 最佳答案 配置与您运行的方式紧密相关puma服务器。运行的标准方式puma-pumaCLI命令。为了配置puma配置文件config/puma.rb或config/puma/.rb应该提供(参见examp
我想用Nokogiri解析HTML页面。页面的一部分有一个表,它没有使用任何特定的ID。是否可以提取如下内容:Today,3,455,34Today,1,1300,3664Today,10,100000,3444,Yesterday,3454,5656,3Yesterday,3545,1000,10Yesterday,3411,36223,15来自这个HTML:TodayYesterdayQntySizeLengthLengthSizeQnty345534345456563113003664354510001010100000344434113622315