@Bean
public User user(){
User user = new User();
user.setId(1);
user.setUsername("china");
return user;
}
}
报错如下:
Error creating bean with name 'user' defined in class path resource [com/xxx/test/ConfigurationTest.class]: No matching factory method found: factory bean 'configurationTest'; factory method 'user()'. Check that a method with the specified name exists and that it is non-static.
spring的@Configuration 和 @bean 注解,替代xml配置的。看资料很简单,很奇怪一启动tomcat服务器就报错。 源码如下: @Configuration public class ConfigurationTest{ @Bean public User user(){ User user = new User(); user.setId(1); user.setUsername("china"); return user; } } 报错如下: Error creatin
@Configuration
@SpringBootApplication
@RestController
public class ConfigurationTest {
public Person person;
@Bean
@RequestMapping("/ConfigurationTest/")
public Person getPerson() {
return new Person();
}
public void setPerson(Person person) {
this.person = person;
}
public static void main(String[] args) {
SpringApplication.run(ConfigurationTest.class, args);
}
}
To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory. By default, the bean name will be the same as the method name (see bean naming for details on how to customize this behavior). The following is a simple example of a @Bean method declaration:
我猜 @Bean
public User user(){
User user = new User();
user.setId(1);
user.setUsername("china");
return user;
}
不能一个类中new 自己,这样会出问题的。要么new 它的实现类,反正不能new 自己
我猜 @Bean
public User user(){
User user = new User();
user.setId(1);
user.setUsername("china");
return user;
}
不能一个类中new 自己,这样会出问题的。要么new 它的实现类,反正不能new 自己