-1

i am really confused with spring annotations. where to use @ Autowired, where class is @ Bean or @ Component,

i understand we cannot use

 Example example=new Example("String"); 

in Spring but how alone

@Autowired
Example example;

will solve the purpose? what about Example Constructor ,how spring will provide String value to Example Constructor?

i went through one of the article but it does not make much sense to me. it would be great if some one can give me just brief and simple explanation.

lesnar
  • 2,400
  • 7
  • 41
  • 72
  • See also http://stackoverflow.com/questions/6739566/anyway-to-autowire-a-bean-that-requires-constructor-arguments – Marvin Jul 29 '15 at 13:25
  • All these questions are answered in [the official documentation](http://docs.spring.io/spring/docs/4.2.0.RC3/spring-framework-reference/htmlsingle/) – kryger Jul 29 '15 at 14:24

3 Answers3

3

Spring doesn't say you can't do Example example = new Example("String"); That is still perfectly legal if Example does not need to be a singleton bean. Where @Autowired and @Bean come into play is when you want to instantiate a class as a singleton. In Spring, any bean you annotate with @Service, @Component or @Repository would get automatically registered as a singleton bean as long as your component scanning is setup correctly. The option of using @Bean allows you to define these singletons without annotating the classes explicitly. Instead you would create a class, annotate it with @Configuration and within that class, define one or more @Bean definitions.

So instead of

@Component
public class MyService {
    public MyService() {}
}

You could have

public class MyService {
    public MyService() {}
}

@Configuration
public class Application {

    @Bean
    public MyService myService() {
        return new MyService();
    }

    @Autowired
    @Bean
    public MyOtherService myOtherService(MyService myService) {
        return new MyOtherService();
    }
}

The trade-off is having your beans defined in one place vs annotating individual classes. I typically use both depending on what I need.

Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68
2

You will first define a bean of type example:

<beans>
    <bean name="example" class="Example">
        <constructor-arg value="String">
    </bean>
</beans>

or in Java code as:

@Bean
public Example example() {
    return new Example("String");
}

Now when you use @Autowired the spring container will inject the bean created above into the parent bean.

6ton
  • 4,174
  • 1
  • 22
  • 37
1

Default constructor + @Component - Annotation is enough to get @Autowired work:

@Component
public class Example {

    public Example(){
        this.str = "string";
    }

}

You should never instantiate a concrete implementation via @Bean declaration. Always do something like this:

public interface MyApiInterface{

    void doSomeOperation();

}

@Component
public class MyApiV1 implements MyApiInterface {

    public void doSomeOperation() {...}

}

And now you can use it in your code:

@Autowired
private MyApiInterface _api; // spring will AUTOmaticaly find the implementation
alex
  • 8,904
  • 6
  • 49
  • 75