spring-boot-starter-xxx
xxx-spring-boot-starter
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
项目结构

public interface IHelloService {
String sayHello(String name);
}
public class HelloServiceImpl implements IHelloService {
private HelloProperties helloProperties;
public HelloServiceImpl(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
@Override
public String sayHello(String name) {
return helloProperties.getName()+"say hello "+name;
}
}
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
private String name;
private boolean enable;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
}
@Configuration
@EnableConfigurationProperties(value = HelloProperties.class)
public class HelloConfiguration {
@Bean
@ConditionalOnProperty("hello.enable")
public IHelloService helloService(HelloProperties helloProperties){
return new HelloServiceImpl(helloProperties);
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.pitaya.starter.hello.HelloConfiguration
<dependency>
<groupId>org.example</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
application.ymlhello:
name: 笑笑
enable: true
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private IHelloService helloService;
@GetMapping("/say-hello")
public String sayHello(String name){
return helloService.sayHello(name);
}
}
public interface IMyService {
String getName();
}
public class MyServiceImpl implements IMyService {
private MyEnableProperties myEnableProperties;
public MyServiceImpl(MyEnableProperties myEnableProperties) {
this.myEnableProperties = myEnableProperties;
}
@Override
public String getName() {
return myEnableProperties.getName();
}
}
@ConfigurationProperties(prefix = "myenable")
public class MyEnableProperties {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Configuration
@EnableConfigurationProperties(value = MyEnableProperties.class)
public class MyEnableConfiguration {
@Bean
public IMyService myService(MyEnableProperties myEnableProperties){
return new MyServiceImpl(myEnableProperties);
}
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MyEnableConfiguration.class)
public @interface EnableHello {
}
<dependency>
<groupId>org.example</groupId>
<artifactId>myenable-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
@EnableHello
@SpringBootApplication
public class MyEnableExampleApplication {
public static void main(String[] args) {
SpringApplication.run(MyEnableExampleApplication.class,args);
}
}
application.yml
myenable:
name: aa
@SpringBootApplication
public class HelloExampleApplication {
public static void main(String[] args) {
SpringApplication.run(HelloExampleApplication.class,args);
}
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
// resourceLoader参数为空
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
// 将启动类放到Set结构的primarySources中
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 判断当前web环境,结果为Servlet环境
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 将结果存储到Application的属性initializers中,在后续需要的时候从这里读,获取到7个类的实例化
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 将结果存储到Application的属性listeners中,在后续需要的时候从这里读,获取到10个类的实例化
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
//确定当前应用的启动类,就是你的springboot的main函数的入口
this.mainApplicationClass = deduceMainApplicationClass();
}
public ConfigurableApplicationContext run(String... args) {
//下面这两行代码就是为了记录启动停止时间之类的,和主流程没啥关系,所以不理它
StopWatch stopWatch = new StopWatch();
stopWatch.start();
//定义spring的容器,只是定义一个变量而已
ConfigurableApplicationContext context = null;
//创建异常报告器
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
// 主要设置java.awt.headless设置系统参数为true,用于运行xxx的图像处理,也是关系不大
configureHeadlessProperty();
// 创建了SpringApplicationRunListeners对象,里面包括一些监听器,例如EventPublishingRunListener
SpringApplicationRunListeners listeners = getRunListeners(args);
//1.初始化EventPublishingRunListener
//2.将application的11个监听器给当前对象
// 上面只是简单的两个,其实还有一些东西的初始化,debug的时候如果某个对象不知道怎么来的,可以debug看看这些监听器
listeners.starting();
try {
//解析命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//准备环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
// 打印banner图对象,没错,就是控制台的那个spring
Banner printedBanner = printBanner(environment);
// 创建spring环境,子类是AnnotationConfigServletWebServerApplicationContext,这一步也是至关重要,开始和Spring关联起来了
context = createApplicationContext();
//TODO 异常处理,先不管
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
/// 准备Spring上下文
// 1.向Spring注册bean internalConfigurationBeanNameGenerator
// 2.添加数据类型转换器
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// 重点要弄明白的,会调用到spring的refresh()方法里面去,全部的spring启动过程,在这个方法呈现在你的面前
refreshContext(context);
// 空实现
afterRefresh(context, applicationArguments);
// 停止及时,你看到springboot启动完后耗时多久就是它咯
stopWatch.stop();
// log相关,不理会
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
// ApplicationRunner的执行,包括你自己定义的
callRunners(context, applicationArguments);
}catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
好像是挺简单,真的这么简单吗?没点进去吧,然后如果展开进去太多,这里对于不重要的步骤只大概说,对于和自动装配有关系的才会贴出相关代码,方便后续的Debug跟踪。
prepareEnvironment()
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
// 创建环境
ConfigurableEnvironment environment = getOrCreateEnvironment();
//配置环境
configureEnvironment(environment, applicationArguments.getSourceArgs());
ConfigurationPropertySources.attach(environment);
// 告诉所有监听器环境已经准备好,发布事件为ApplicationEnvironmentPreparedEvent,7个监听器会收到事件
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader()).convertEnvironmentIfNecessary(environment,
deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
if (this.addConversionService) {
// 加载所有的类型转换器
ConversionService conversionService = ApplicationConversionService.getSharedInstance();
// 设置数据类型转换器到当前环境 environment.setConversionService((ConfigurableConversionService) conversionService);
}
// 配置properties资源
configurePropertySources(environment, args);
// 将properties加载到环境变量
configureProfiles(environment, args);
}
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
// 在创建SpringApplication对象的时候就已经确定是SERVLET了
switch (this.webApplicationType) {
case SERVLET:
// public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot."+web.servlet.context.AnnotationConfigServletWebServerApplicationContext
// 可以知道Spring上下文就是AnnotationConfigServletWebServerApplicationContext
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
之后会调用一系列的方法,如下

invokebeanFactoryPostProcessors()方法protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//beanFactory, getBeanFactoryPostProcessors()可以获取到以下三个类
//CachingMetadataReaderFactoryPostProcessor
//ConfigurationWarningsPostProcessor
//PropertySourceOrderingPostProcessor
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
}
public void parse(Set<BeanDefinitionHolder> configCandidates) {
for (BeanDefinitionHolder holder : configCandidates) {
BeanDefinition bd = holder.getBeanDefinition();
try {
if (bd instanceof AnnotatedBeanDefinition) {
parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
}
else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
}
else {
parse(bd.getBeanClassName(), holder.getBeanName());
}
}
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
}
}
// 这里需要处理所有延迟处理的@import上的类,例如AutoConfigurationImportSelector,在解析道@import上的类之后,这里正式去调用扫描jar包的spring.factories文件了
this.deferredImportSelectorHandler.process();
}
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
throws IOException {
//加载 compotent 注解的类
if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
// Recursively process any member (nested) classes first
processMemberClasses(configClass, sourceClass);
}
// Process any @PropertySource annotations
// 加载属性文件
for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), PropertySources.class,
org.springframework.context.annotation.PropertySource.class)) {
if (this.environment instanceof ConfigurableEnvironment) {
processPropertySource(propertySource);
}
else {
logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
"]. Reason: Environment must implement ConfigurableEnvironment");
}
}
// Process any @ComponentScan annotations
//先找出类上的@ComponentScan和@ComponentScans注解的所有属性, 解析@ComponentScan和@ComponentScans配置的扫描的包所包含的类,如果没有就从启动类的包查找
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
if (!componentScans.isEmpty() &&
!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
for (AnnotationAttributes componentScan : componentScans) {
// The config class is annotated with @ComponentScan -> perform the scan immediately
Set<BeanDefinitionHolder> scannedBeanDefinitions =
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
// Check the set of scanned definitions for any further config classes and parse recursively if needed
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
if (bdCand == null) {
bdCand = holder.getBeanDefinition();
}
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
parse(bdCand.getBeanClassName(), holder.getBeanName());
}
}
}
}
// Process any @Import annotations
//处理Import注解注册的bean,这一步只会将import注册的bean变为ConfigurationClass,不会变成BeanDefinition
processImports(configClass, sourceClass, getImports(sourceClass), true);
// Process any @ImportResource annotations
//处理@ImportResource注解引入的配置文件
AnnotationAttributes importResource =
AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
if (importResource != null) {
String[] resources = importResource.getStringArray("locations");
Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
for (String resource : resources) {
String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
configClass.addImportedResource(resolvedResource, readerClass);
}
}
// Process individual @Bean methods
//处理加了@Bean注解的方法
Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
for (MethodMetadata methodMetadata : beanMethods) {
configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
}
// Process default methods on interfaces
processInterfaces(configClass, sourceClass);
// Process superclass, if any
if (sourceClass.getMetadata().hasSuperClass()) {
String superclass = sourceClass.getMetadata().getSuperClassName();
if (superclass != null && !superclass.startsWith("java") &&
!this.knownSuperclasses.containsKey(superclass)) {
this.knownSuperclasses.put(superclass, configClass);
// Superclass found, return its annotation metadata and recurse
return sourceClass.getSuperClass();
}
}
// No superclass -> processing is complete
return null;
}
/**
* Register member (nested) classes that happen to be configuration classes themselves.
*/
private void processMemberClasses(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
Collection<SourceClass> memberClasses = sourceClass.getMemberClasses();
if (!memberClasses.isEmpty()) {
List<SourceClass> candidates = new ArrayList<>(memberClasses.size());
for (SourceClass memberClass : memberClasses) {
if (ConfigurationClassUtils.isConfigurationCandidate(memberClass.getMetadata()) &&
!memberClass.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
candidates.add(memberClass);
}
}
OrderComparator.sort(candidates);
for (SourceClass candidate : candidates) {
if (this.importStack.contains(configClass)) {
this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
}
else {
this.importStack.push(configClass);
try {
processConfigurationClass(candidate.asConfigClass(configClass));
}
finally {
this.importStack.pop();
}
}
}
}
}
processImports(configClass, sourceClass, getImports(sourceClass), true);
private Set<SourceClass> getImports(SourceClass sourceClass) throws IOException {
Set<SourceClass> imports = new LinkedHashSet<>();
Set<SourceClass> visited = new LinkedHashSet<>();
// 收集当前类上所有的import载入的class
collectImports(sourceClass, imports, visited);
return imports;
}
AutoConfigurationPackages$Registrar,AutoConfigurationImportSelector,EnableConfigurationPropertiesRegistrar和ConfigurationPropertiesScanRegistrar
private void collectImports(SourceClass sourceClass, Set<SourceClass> imports, Set<SourceClass> visited)
throws IOException {
if (visited.add(sourceClass)) {
for (SourceClass annotation : sourceClass.getAnnotations()) {
String annName = annotation.getMetadata().getClassName();
if (!annName.equals(Import.class.getName())) {
collectImports(annotation, imports, visited);
}
}
imports.addAll(sourceClass.getAnnotationAttributes(Import.class.getName(), "value"));
}
}
this.deferredImportSelectorHandler.process();
public void process() {
List<DeferredImportSelectorHolder> deferredImports = this.deferredImportSelectors;
this.deferredImportSelectors = null;
try {
if (deferredImports != null) {
DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler();
deferredImports.sort(DEFERRED_IMPORT_COMPARATOR);
deferredImports.forEach(handler::register);
handler.processGroupImports();
}
}
finally {
this.deferredImportSelectors = new ArrayList<>();
}
}
public void processGroupImports() {
for (DeferredImportSelectorGrouping grouping : this.groupings.values()) {
//grouping.getImports()就会调用
grouping.getImports().forEach(entry -> {
ConfigurationClass configurationClass = this.configurationClasses.get(
entry.getMetadata());
try {
processImports(configurationClass, asSourceClass(configurationClass),
asSourceClasses(entry.getImportClassName()), false);
}
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to process import candidates for configuration class [" +
configurationClass.getMetadata().getClassName() + "]", ex);
}
});
}
}
public Iterable<Group.Entry> getImports() {
for (DeferredImportSelectorHolder deferredImport : this.deferredImports) {
this.group.process(deferredImport.getConfigurationClass().getMetadata(),
deferredImport.getImportSelector());
}
//调用import的selectImports()方法
return this.group.selectImports();
}
protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata
autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
} else {
AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
// 遍历jar包读取spring.factories文件中的所有的类
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
configurations = this.removeDuplicates(configurations);
Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
//处理一些相关的排除的不需要的类
this.checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = this.filter(configurations, autoConfigurationMetadata);
this.fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
}
}
this.reader.loadBeanDefinitions(configClasses);
最后是方便debug的流程图

Spring到底如何完成自动装配?简单点,应该是以下几个步骤:
1.spring在构造SpringApplication对象的时候确定web应用类型为SERVLET,同时完成监听器等操作,为后续解析自动装配jar包,构建spring环境做准备。
2.执行SpringApplication对象执行run()方法,最重要的两个步骤为准备好Environment环境以及创建spring上下文AnnotationConfigServletWebServerApplicationContext
3.准备好Spring上下文后,开始调用prepareContext将启动类的BeanDefintion创建好,调用refresh()方法执行spring的启动过程,最重要的是在执行到invokeBeanFactoryPostProcessors()方法的时候,会查找所有@Configuration类,并完成相应的BeanDefintion注册工作,以及循环查找启动类上的@Import注解,最终确定到一个非常重要的类AutoConfigurationImportSelector,Springboot调用该类的getCandidateConfigurations()方法去查找jar包中所有的spring.factories文件,并且解析得到这些文件配置的类,排除掉一些不必要加载的后最终调用loadBeanDefintions()方法,完成bean描述信息的注册,随后,这些BeanDefintions会跟随spring一步一步完成实例化,初始化,aware接口调用,BeanPostProcessor的postProcessBeforeInitialization()方法,初始化方法,BeanPostProcessor的postProcessAfterInitialization()方法的生命周期而被Spring管理
好难写,写的手痛
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c