0

so I am reading about the Dependency Injection for the first time. I think I have figured it out and I have understood an example written for PHP. Though, I was reading this JAVA tutorial and it insists on adding annotations. If I am going to offer the the class its dependency externally using the constructor for example, then what why are the annotations required? Also, I am reading on the Spring framework, which also states you need annotations. How do the annotations fit in? Any information is appreciated.

  • 1
    Can you post some code example of how DI should look like in Java without annotation? – xiaofeng.li Dec 26 '17 at 23:59
  • Not sure if this is it but, I know that annotations can be detected using reflection. And if you load in a jar you can iterate through all of the classes and there methods checking to see if they have a specific annotation. – vandench Dec 27 '17 at 00:02
  • Do you understand what annotations, generally, are for? (And since 4.3, Spring _doesn't_ require an annotation for constructor injection, although you still have to somehow tell it that you want a bean of a certain class.) – chrylis -cautiouslyoptimistic- Dec 27 '17 at 00:02
  • Spring doesn't require annotations. You can also use XML. Did you bother to read the Spring Framework **documentation**, e.g. [Chapter 1.9. Annotation-based container configuration](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-annotation-config)? – Andreas Dec 27 '17 at 00:13
  • Annotations are required if the framework requires them. Some do (I think JSF requires annotations). Some have annotations as an option (Spring for example) and some are older and don't have any options for annotations at all. Annotations are generally considered a better option these days than XML configuration files (which result in "XML Hell" for Java apps) but that also depends on your project requirements and the requirements of your business, staff experience, etc. – markspace Dec 27 '17 at 00:42

1 Answers1

2

what why are the annotations required?

This is up to you, whether you want an XML configuration or annotations. Spring uses annotations as an alternative to XML for declarative configuration.

Let's take your example,you want to pass dependency through constructor.
Department object has dependency on Employee object for its creation.
Employee object has two attributes id and name.

  1. By using annotation how Can you do it?

    @Configuration
    @ComponentScan("com.example.spring")
    public class Config {
    
        @Bean
    public Employee employee() {
        return new Employee("1", "John");
    }
    }
    

Now creating Department object:

@Component
public class Department {

    @Autowired
    public Department (Employee employee) {
        this.employee= employee;

    }
}
  1. By using XML:

    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <bean id="department" class="com.example.spring.Department">
        <constructor-arg index="0" ref="employee"/>
    </bean>
    
    <bean id="employee" class="com.example.spring.Employee">
        <constructor-arg index="0" value="1"/>
        <constructor-arg index="1" value="John"/>
    </bean>
    

  2. By using Java: You can do something like

     Employee employee=new Employee("1","John");   
     Department dept=new Department(employee);
    

Point is, It's up-to you how you want to do things.

Have a look at this question Xml configuration versus Annotation based configuration

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45