老师,不知道为啥,这里会报错,启动不了
来源:2-5 @Import 的两种用法
MasonM
2020-06-03 17:46:18
package com.mason.csleevejava;
import com.mason.csleevejava.sample.HeroConfiguration;
import com.mason.csleevejava.sample.ISkill;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@ComponentScan
public class LOLApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new SpringApplicationBuilder(LOLApplication.class).web(WebApplicationType.NONE).run(args);
ISkill iSkill = (ISkill) context.getBean("irelia");
iSkill.r();
}
}package com.mason.csleevejava.sample;
import com.mason.csleevejava.sample.hero.Diana;
import com.mason.csleevejava.sample.hero.Irelia;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HeroConfiguration {
// @Bean
// @ConditionalOnMissingBean(name = "mysql")
public ISkill diana() {
return new Diana("戴安娜", 18);
}
@Bean
public ISkill Irelia() {
return new Irelia("刀妹", 18);
}
}2020-06-03 17:44:34.211 INFO 15536 --- [ restartedMain] com.mason.csleevejava.LOLApplication : Starting LOLApplication on DESKTOP-4EDPDJH with PID 15536 (D:\Mason\sleeveProj\c-sleeve-java\target\classes started by 59692 in D:\Mason\sleeveProj\c-sleeve-java) 2020-06-03 17:44:34.213 INFO 15536 --- [ restartedMain] com.mason.csleevejava.LOLApplication : No active profile set, falling back to default profiles: default 2020-06-03 17:44:34.246 INFO 15536 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable Irelia init 2020-06-03 17:44:35.089 INFO 15536 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2020-06-03 17:44:35.120 INFO 15536 --- [ restartedMain] com.mason.csleevejava.LOLApplication : Started LOLApplication in 1.225 seconds (JVM running for 1.715) Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'irelia' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:805) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1278) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) at com.mason.csleevejava.LOLApplication.main(LOLApplication.java:17) ... 5 more
1回答
MasonM
提问者
2020-06-03
我知道了,因为@Bean的方法名Irelia写错了
相似问题