-2

I want to understand how the annotation works. I have this piece of code which I have used in a simple Spring project.

@Resource(name="dataSource")
private DataSource dataSouce;

The dataSource I have defined in an XML config file:

<!-- The Apache DBCP implementation of DataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:tcp://localhost/~/test"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>

Other snippet from spring xml config file

    <context:annotation-config /> <!-- This enables the annotation's actions, else annotations don't do their work. -->
    <context:component-scan base-package="com.example.dao.jdbc.impl"/> <!-- This is for component scan -->
    <bean id="jdbcOperImpl" class="com.example.dao.jdbc.impl.JdbcOperImpl"/>

As I understand the Resource annotations comes from javax.annotation.Resource. I looked its source code, and I notices the annotation is defined by JDK SE, and it is just a simple definition of an annotation. How does this do the injection? Does Spring Framework use this annotation and does Injection? How is @Resource annotation and Spring framework related?

kryger
  • 11,746
  • 8
  • 41
  • 60
CuriousMind
  • 6,665
  • 12
  • 49
  • 95
  • Possible duplicate of these two: [@Resource vs @Autowired](http://stackoverflow.com/q/4093504/476716) and [What is dependency injection?](http://stackoverflow.com/q/130794/476716) – OrangeDog Apr 25 '16 at 11:45
  • What exactly are you asking? There's a good chance this question is either too broad or a duplicate. – OrangeDog Apr 25 '16 at 11:45
  • *"Do Spring Framework use this annotation and does Injection?"* Yes, most if not all annotations are just simple markers that carry information for units that process them. – kryger Apr 25 '16 at 11:49
  • @OrangeDog: This question isn't duplicate of the question that you have hinted at. I am asking how does Resource inject the dependency. I looked at the source code of Resouce annotation and there is nothing special in that code which might inject the dependency. I was asking what is the relation between Resouce and the spring framework when it comes injecting the dependency (how exactly these two work together for injecting the dependency) – CuriousMind Apr 25 '16 at 11:57
  • Then it's a duplicate of [How does dependency injection work?](http://stackoverflow.com/q/3725515/476716) – OrangeDog Apr 25 '16 at 12:25
  • @OrangeDog I would say no again! I am specific to Resouce annotation and Spring here. The other SO are "how" DI works. I would see if I can do some more research of my own. I thank you for your help. – CuriousMind Apr 25 '16 at 12:29
  • 1
    That question is Spring-specific and exactly what you're asking. As the first question I suggested says, `Resource` works just the same in this respect as `Autowired` and `Inject`. – OrangeDog Apr 25 '16 at 12:44

1 Answers1

2

Actually Spring supports two types of annotations which are Spring based annotations and Java based annotations. For dependencies you can use @Autowired that completely spring annotations.

@Resource and @Inject are the standard java based annotations. For more clarity see this link http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/

Roman Puchkovskiy
  • 9,798
  • 4
  • 25
  • 51
eswara
  • 95
  • 7