springjavabeans

Beginner confusion about autowiring in Spring


All:

I just begin the Spring study, there is one concept confused me so much: The Autowiring.

The question I need to be clear is:

What kind of thing can be autowired? Is it only other beans?

If I define a class Bar:

public class Bar {
    @Autowired
    public String name;
}

I am thinking what if I put a @Autowired annotation to a member variable like String name; and in bean config XML I wrote:

<bean id="bar" class="com.Bar">
    <property name="name" value="Bar" />
</bean>

So many ways to do same thing in Spring confused me so much. ToT. Is there any good trick to clearly remember and tell diff among different ways?

Thanks


Solution

  • Yes, You can only @Autowired Beans. @Autowired is the way of using Dependency Injection in Spring. It is provided for Beans - components wrote once and used many times. There's no sense using DI on String property and I think You get Exception.