-1

Based on the example being followed here : https://dzone.com/articles/autowiring-in-spring

import org.springframework.stereotype.Component;

@Component
public class Department {
    private String deptName;
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
}

The @Autowired annotation is placed on the Department.

import org.springframework.beans.factory.annotation.Autowired;

public class Employee {
    private int eid;
    private String ename;
    @Autowired
    private Department department; //<----------------------
    public int getEid() {
        return eid;
    }
    public void setEid(int eid) {
        this.eid = eid;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
    public void showEmployeeDetails(){
        System.out.println("Employee Id : " + eid);
        System.out.println("Employee Name : " + ename);
        System.out.println("Department : " + department.getDeptName());
    }
}

This runs the program.

public class RunThis {
    public static void main(String[] args) 
    {
        System.out.println("Hello World");
        Employee emp = new Employee();
        emp.showEmployeeDetails();
    }
}

Running the program results in a NULL exception which is expected, but per the provided URL, it reads :

@Autowired on Properties
In the below example, when the annotation is directly used on properties, Spring looks for and injects Department when Employee is created. 

As a result, I interpreted to mean that the Department object would implicitly be instantiated behind the scenes at the time the Employee object is instantiated, but apparently that is wrong.

1 - How can this sample be modified to view the actual benefits of the @Autowired annotation?

Many other examples discuss the @Autowired annotation affecting what is written to some XML file in my project.

I searched my project for all files and don't see any reference toward the object (Department) other than Employee and Department.

2 - Where is the XML file located that one can view the changes affected by the use of the @Autowired annotation?

Unhandled Exception
  • 1,427
  • 14
  • 30

2 Answers2

1

I think you're missing the bigger picture. You annotate classes so that they're managed by Spring. In order to use Spring you'll have to adapt your main.

@SpringBootApplication
public class RunThis {
    public static void main(String[] args) 
    {
        SpringApplication.run(RunThis.class, args);
        System.out.println("Hello World");
        Employee emp = new Employee();
        emp.showEmployeeDetails();
    }
}

Now our project is managed in Spring but our Employee isn't as we make this. We should let Spring handle this by creating a bean for our employee and fetching that bean. The creation of beans we do in classes annotated in @Configuration or via a ComponentScan.

Also all your changes when it comes to annotating won't be vissible in any .xml file.

SanderDR
  • 26
  • 2
  • Thanks for the post. From reading it seemed to indicate the annotations affected the XML file, but I guess it affects what the developer as to write to the XML file???? – Unhandled Exception Mar 13 '20 at 14:49
  • No problem, the annotations have no impact on the XML file. There are 2 ways to setup your Spring configuration, via xml or via annotations. In the documentation you shared you can see both xml and annotations but they're the same configuration. – SanderDR Mar 13 '20 at 15:36
1

You haven't set department to be anything, as department is a pojo, to benefit from autowired you'd be better to do something like the following in a configuration class that gets loaded by spring boot

@Bean
public Department department () {
  Department department = new Department ();
  department.setDeptName("foo");
  return department;
}

Although the link you provided, you haven't even followed it I am guessing as it shows them configuring the beans using XML, which you haven't done so autowiring will not work.

Thomas__
  • 320
  • 3
  • 13
  • Thanks. I was thinking the XML files were generated as part of the project in the IDE or it was auto-generated based on the annotations being used. Is there a specific location within the Project the XML file needs to be located? – Unhandled Exception Mar 13 '20 at 14:47
  • https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html personally never done xml configuration, I usually go with the Java configuration, similar to my answer. – Thomas__ Mar 13 '20 at 15:04