有熟悉spring的@Configuration 和 @bean 的大侠吗?

风云2015 2014-09-08 04:05:27
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 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.


-----------------------------------
@Configuration标注的这个类 是放在 component-scan 的包下的,spring会自动扫描。我试着拿出来,放在spring不自动扫描的包下,结果报错没有了。但是,却没有实例user生成。

求助大侠,到底是怎么回事。。。。。。。。。。。。。。。。。。。。。






...全文
26541 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
thirdlucky 2016-12-27
  • 打赏
  • 举报
回复
一般情况下,你的工程中,已经包含了同样类型的Bean,你给Bean指定一个名称即可,如 @Bean("test user")
Jimmy_架构师 2016-12-20
  • 打赏
  • 举报
回复
返回的变量 必须是 静态 变量
  • 打赏
  • 举报
回复
被spring管理,类似自动注入(注入源),单利模式
认真的学生 2016-01-20
  • 打赏
  • 举报
回复
@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);

	}

}
我写了一下,这样写是没有问题的
jwbhcuikai 2015-09-22
  • 打赏
  • 举报
回复
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:
亲亲和帮少 2015-04-24
  • 打赏
  • 举报
回复
实例名不能和方法名一个样
dalinaidalin 2015-03-20
  • 打赏
  • 举报
回复
引用 5 楼 u010449283 的回复:
引用 4 楼 sinat_19250161 的回复:
你的User 类有注解吗?应该是这个原因吧
User类没有注解啊,难道User类上要加上 @Bean 这个注解? 也没见资料里提到这点啊
Spring在启动的时候检测到@Bean的时候默认会在容器中注入一个以方法名(你的代码中是user)命名的Bean,而这个Bean用的是和该方法的返回类型一样的类(你的代码中是User)来初始化的。所以你需要在User这个类上加入@Component。希望对你有帮助。
Isenhart 2014-09-16
  • 打赏
  • 举报
回复
个人还是喜欢传统的XML配置,方便
风云2015 2014-09-12
  • 打赏
  • 举报
回复
引用 14 楼 sinat_19250161 的回复:
楼主可以看一下 http://blog.csdn.net/rommel1/article/details/27120059
我就是按照这篇文章做的
sinat_19250161 2014-09-11
  • 打赏
  • 举报
回复
楼主可以看一下 http://blog.csdn.net/rommel1/article/details/27120059
风云2015 2014-09-11
  • 打赏
  • 举报
回复
引用 9 楼 qq_18216323 的回复:
<bean class="com.tan.config.UserConfig" /> class为放置的类的位置
不行
风云2015 2014-09-11
  • 打赏
  • 举报
回复
引用 10 楼 plq1210 的回复:
我猜 @Bean public User user(){ User user = new User(); user.setId(1); user.setUsername("china"); return user; } 不能一个类中new 自己,这样会出问题的。要么new 它的实现类,反正不能new 自己
我不就是这么搞的吗,不行啊
风云2015 2014-09-11
  • 打赏
  • 举报
回复
引用 7 楼 sinat_19250161 的回复:
引用 6 楼 u010449283 的回复:
[quote=引用 4 楼 sinat_19250161 的回复:] 你的User 类有注解吗?应该是这个原因吧
User类上要加什么注解呢,求教
@entity 我也不大清楚 你试试吧[/quote] no 这样不行
优美兴 2014-09-10
  • 打赏
  • 举报
回复
<bean class="com.tan.config.UserConfig" /> class为放置的类的位置
三流角色 2014-09-10
  • 打赏
  • 举报
回复
这种配置我没怎么用,一般用自动装配引入要引用的bean
sinat_19250161 2014-09-10
  • 打赏
  • 举报
回复
引用 6 楼 u010449283 的回复:
引用 4 楼 sinat_19250161 的回复:
你的User 类有注解吗?应该是这个原因吧
User类上要加什么注解呢,求教
@entity 我也不大清楚 你试试吧
三流角色 2014-09-10
  • 打赏
  • 举报
回复
我猜 @Bean public User user(){ User user = new User(); user.setId(1); user.setUsername("china"); return user; } 不能一个类中new 自己,这样会出问题的。要么new 它的实现类,反正不能new 自己
三流角色 2014-09-09
  • 打赏
  • 举报
回复
@bean是注解在类上面的,声明这是个bean,不用去xml配置什么<bean id="xxx" class="xxx"/>了 @configuration也是注解在类上面的,声明这是个配置作用的bean,替代xml配置 参照http://blog.csdn.net/chjttony/article/details/6286144
风云2015 2014-09-09
  • 打赏
  • 举报
回复
引用 4 楼 sinat_19250161 的回复:
你的User 类有注解吗?应该是这个原因吧
User类上要加什么注解呢,求教
风云2015 2014-09-09
  • 打赏
  • 举报
回复
引用 4 楼 sinat_19250161 的回复:
你的User 类有注解吗?应该是这个原因吧
User类没有注解啊,难道User类上要加上 @Bean 这个注解? 也没见资料里提到这点啊
加载更多回复(3)

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧