I'm missing fundamental things here that I do, I'm sorry for this question, basically I tried to simulate my own question from Autowring for @Sevice field failed , but in very simple form , I tried to somehow raise errors on purpose for building understanding, but when it comes, I just can't handle it.
x-servlet.xml
<beans xmlns= ...... >
<context:component-scan base-package="com" />
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
com.domain
Boss.java
package com.domain;
public class Boss {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Employee:
package com.domain;
import org.springframework.beans.factory.annotation.Autowired;
public class Employee {
@Autowired
private Boss boss;
String nameBoss;
public String getNameBoss() {
nameBoss = this.boss.getName();
return nameBoss;
}
}
com.controller
controller.java :
package com.controller
import com.domain.Boss;
import com.domain.Employee;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class controller {
@RequestMapping("/try")
public String show() {
Boss b = new Boss();
b.setName(" hans");
Employee e = new Employee();
String bossName = e.getNameBoss();
System.out.println(bossName );
return "";
}
}
I was thinking that,
String bossName = e.getNameBoss();
in controller won't be null because Boss is already initialized right before Employee construction, but I'm wrong ..