You can't autowire in a String value like you have done.
By writing your XML bean declaration, what you have done is created a singleton instance of Bar
with the name field set to the value of 'Bar'.
If you wanted to then use this Bar class elsewhere in your application (say in a class called Foo) you could do this:
public class Foo {
@Autowired
private Bar bar;
}
Spring would then inject your instance of Bar
into your Foo
class.
However because Bar
is a singleton by default, no matter how many instances of Bar
you autowired into different classes, you would have the same instance of Bar
in all of them. So if you try and modify name in one class, it will change for all the other classes.
As a general rule of thumb, if your class has any mutable data (data that will be changed and updated), then your class is not something that you should be declaring for autowiring (because there will only ever be one instance of it inside your Spring application). It works better for things like DAO's and service classes. In answer to your first question, yes you can only autowire instances of classes (beans).
If you need to get a String/Integer/Long/Array/Mutable Data Type value from somewhere, configure a properties file, and read up how to use the org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
class with the @Value
annotation.