0
votes

I have this Java code:

public class GreetingServiceImpl implements IGreetingService {

@Resource(name="abc")
private String anotherMsg = null;

What should be the spring-config.xml? The following xml is giving error:

Error creating bean with name 'abc' defined in class path resource [greetingConfig.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'anotherMsg' of bean class [java.lang.String]: Bean property 'anotherMsg' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

This is the bean configuration in spring-config.xml:

<bean id="abc" class="java.lang.String"> 
    <property name="anotherMsg" value="testing @Resource..."/>
</bean>
1

1 Answers

2
votes

This bean declaration

<bean id="abc" class="java.lang.String"> 
    <property name="anotherMsg" value="testing @Resource..."/>
</bean>

is for a bean of type String. The type String does not have a property called anotherMsg. Your class, GreetingServiceImpl has a property called anotherMsg (assuming it has the appropriate getters and setters).

If you really want to create a String bean, which you'll inject into a GreetingServiceImpl bean, use constructor arguments

<bean id="abc" class="java.lang.String"> 
    <constructor-arg type="java.lang.String" value="testing @Resource..."/>
</bean>

You don't typically create String beans though. Use a property resolution instead to inject String values. Look into @Value and PropertySourcesPlaceholderConfigurer.