my dog learned polymorphism*
The moose likes Spring and the fly likes @Autowired for properies in 3.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "@Autowired for properies in 3.1" Watch "@Autowired for properies in 3.1" New topic
Author

@Autowired for properies in 3.1

Oscar G. Rodriguez
Greenhorn

Joined: Nov 05, 2011
Posts: 15
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

Joined: Aug 06, 2012
Posts: 122

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

Joined: Aug 06, 2012
Posts: 122

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

Joined: Nov 05, 2011
Posts: 15
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

Joined: Aug 06, 2012
Posts: 122

You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: @Autowired for properies in 3.1
 
Similar Threads
Simple Spring Batch
NoSuchBeanDefinitionException using spring and mybatis
Spring 3 and Hibernate
Exception org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type
spring wont inject the entitymanager