-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.

2

3 Answers 3

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.

3
  • this is not the idea of spring. You should never use new {ConcreteImplementation} in your code.
    – devops
    Jul 29, 2015 at 13:48
  • Autowired on a method works similar to constructor, but I prefer constructor since your singleton bean will only be instantiated if the required dependencies are met. Method injection is less reliable I believe. Jul 29, 2015 at 14:48
  • @dit this was just an example. Jul 29, 2015 at 14:50
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.

2
  • i can use this @Bean in same class where i am using @Autowire ?
    – lesnar
    Jul 29, 2015 at 13:24
  • @Tajinder yes I think that should be ok - you can easily try it out and check if it works
    – 6ton
    Jul 29, 2015 at 14:41
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
2
  • and what does @ Autowired on some method represents ?
    – lesnar
    Jul 29, 2015 at 14:10
  • 1
    @Tajinder the same. @Autowired means "find automaticaly implementation and create it via new"
    – devops
    Jul 30, 2015 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.