File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Autowired for properies in 3.1

 
Oscar G. Rodriguez
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm learning Spring from the 3.1 spring_tutorial.PDF, and in @Autowired on Properties section we can read



I've understood that it works without the setter method, then I've tried





without/with setter method, but always throws an exception

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


If I use the @Autowired(required=false), the msg property is injected if it's defined in the xml, and I have no exception if I comment it, but only with the setter method.

When I comment the setter method I obtain

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'msg' of bean class [BeanTest]: Bean property 'msg' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?


It only works with defined bean objects but not with String? Why it works with required=false? It's needed the setter method?

I'm wrong or it is an issue?

Thanks.
 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use XML to inject a property, you need a setter method.

If you @Autowired a property, you don't need a setter method.

- k

-------------------------------------------------------------------------------------------------------------
[SpringSource Certified Spring Professional - Practice Tests]
 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oscar G. Rodriguez wrote:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


If you have below,



where 'required' attribute of @Autowired is defaulted 'true', you must have a exactly 1 Spring bean in the context whose type is String.

Otherwise, you will get the exception above.


If you have below,



you must have a 0 or 1 Spring bean in the context whose type is String.

- k

-------------------------------------------------------------------------------------------------------------
[SpringSource Certified Spring Professional - Practice Tests]
 
Oscar G. Rodriguez
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks a lot.

I understand that i was mixing both ways, XML and annotations.

If I want to inject a String property by @Autowire in property (only for study purpose) I need to create a String Bean like



And when I defined the required=false it worked because I was mixing both at same time, the annotation way didn't inject anything but not fail, and the XML did.

My confussion came because I didn't see a clear way to create a String bean, and I supposed that I had to use the property tag.
 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic