0

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 ..

Community
  • 1
  • 1
  • the boss object has no relation with the employee object. They are just two separate objects. If you are referring the `@autowire` magic then this is not how it's supposed to work. `@autowire` will inject the dependency from the dependencies specified in the context file. – Moinul Hossain Dec 29 '15 at 21:50
  • 1
    You have a few issues here. An IoC container like Spring will manage some objects for you and, if it does, you can't instantiate them yourself. If you want to autowire an object, it has to be an object the container manages, which means you have to annotate it with something like @Service. If it is something the container manages, then you should not be instantiating it yourself. – Jim Archer Dec 29 '15 at 21:50
  • Thanks for the response guys, when in spring core, I tried to also simulate this, the only difference that , I could load the beans from ApplicationContext#getBean, but in Spr. MVC, how I load the bean for those *Employee* & *Boss* .., – Plain_Dude_Sleeping_Alone Dec 29 '15 at 21:56
  • Possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – kryger Dec 30 '15 at 08:52

1 Answers1

1

Are you sure you want to have dependency injection (DI) for domain objects? This is not a typicall usage... For a DI, container have to create instances - so do not use new operator...

You can replace new operator using ApplicationContext.getBean() (iff the bean is of type prototype), but as I said above - even if you know how to create such beans, how should Spring know, which instance of Boss you want to have in Employee?

First thing you need is application context in controller, which should work to add interface org.springframework.context.ApplicationContextAware or you can simply autowire it and then use it "standard" way (ac is ApplicationContext):

    Boss b = ac.getBean(Boss.class);
    Employee e = ac.getBean(Employee.class, b);

I have Boss and Employee marked with annotations as:

@Component
@Scope("prototype")
public class Boss {

...

}

@Component
@Scope("prototype")
public class Employee {

    // @Autowire - wrong
    Boss boss;

    public Employee(Boss boss) {
        this.boss = boss;
    }

    ...
}
Community
  • 1
  • 1
Betlista
  • 10,327
  • 13
  • 69
  • 110
  • I think that the only way to get rid of the `new` operator, is by loading the beans from the context of Spring, Like in spring core we use `ApplicationContext#getBean` , but if only I know how to load those `Employee` & `Boss`.. then I think I could get rid of those `new` operators – Plain_Dude_Sleeping_Alone Dec 29 '15 at 22:04
  • Thanks for your edit, the implementor class for *ApplicationContext* that I usually used is `ClassPathXmlApplicationContext(`...`);`, but it has argument which is a path to the context configuration, if i try `ClassPathXmlApplicationContext(`/WEB-INF/x-servlet.xml/`)` then it won't work.., help me bro.. – Plain_Dude_Sleeping_Alone Dec 29 '15 at 22:19
  • Do have to first declare both `Boss` & `Employee`, with ``, in `x-servlet.xml`?? – Plain_Dude_Sleeping_Alone Dec 29 '15 at 22:36
  • No, you do not need to, you have component scan, so use `Component` and `Scope` annotations... – Betlista Dec 29 '15 at 22:38
  • That means I have to create custom config. for the beans, for now I'll upvote you, let me understand first. – Plain_Dude_Sleeping_Alone Dec 29 '15 at 22:47
  • pretty much stuff in my head right now.. :D, I feel guilty that I won't bother you futher, like *Boss b = ac.getBean(Boss.class);* how do I set property for the b` (`b.setName` ?), also there `Boss b = ac.getBean(Boss.class);` seems like *invisible* new., which means the same :D, but just wait for tommorow, I'll learn it first.., thank you – Plain_Dude_Sleeping_Alone Dec 29 '15 at 22:53
  • Exactly - `b.setName`, you have your instance, so do what you need to do with it... As I said at the beginning, typically it's not used with domain objects - there is not added value doing so... – Betlista Dec 29 '15 at 22:56