injoy
injoy

Reputation: 4373

How can I inject a String value defined in a xml file into a component class member?

For instance, in the xml file, I have defined some String values:

<bean id="name" class="java.lang.String" >
    <constructor-arg type="java.lang.String" value="SMITH" />
</bean>

While in the component class, how can I inject this into its class member?

@Component
public class Person {
    private String name; // @Autowired doesn't work here
}

How can I inject the String 'name' defined in the xml file into Person? @Autowired doesn't work. Is there any workaround here?

Upvotes: 0

Views: 324

Answers (1)

StanislavL
StanislavL

Reputation: 57381

Try to annotate with @Resource

@Resource(name="name") 
private String name;

Upvotes: 1

Related Questions