Let's define @Autowired for the previous example. Our goal is to remove the XML configuration <property name="professor" ref="professor" /> for the subject bean with the @Autowired annotation. Let's modify a setter method of the Subject class and Spring application (XML) context file, as follows:
//Updated setter injection method @Autowired public void setProfessor(Professor professor) { System.out.println("setting the professor through setter method injection "); this.professor = professor; }//Updated XML configuration<bean id="professor" class="com.packet.spring.annotation.di.Professor"> <property name="name" value="Nilang" /></bean> <bean id="subject" class="com.packet.spring.annotation.di.Subject"></bean>
We are expected ...