5

I have a model in which I want to inject my service.

My Model

@Configurable
@Entity
@Table(name = "user")
public Class User {

@Autowired
private UserService userService;

{
   System.out.println("Trying Service : " + userService.getMyName()); 
}

}

Here I get always a NullPointerException on 7'th line.

In my spring-context.xml I have :

<context:spring-configured/>
<bean
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean
    class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

EDIT

UserService

@Component
public Class UserService {

  public String getMyName() { return "it's Me!";}

}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kilo Batata
  • 81
  • 1
  • 2
  • 6
  • Please provide code of `UserService` and your spring context XML file, provided information is not sufficient to say anything deterministically. – hagrawal7777 Jun 15 '17 at 11:42
  • Ok check now, i've edited it – Kilo Batata Jun 15 '17 at 11:47
  • 3
    This is not good approach to inject bean into data model. Why do you need this injection? – eg04lt3r Jun 15 '17 at 11:49
  • 1
    You need to have component scan and annotation driven entries in your spring XML file, something like this ` ` and I don't know about `@Configurable` whether it is spring annotation to make the class as bean or not, if not then you have to use `@Component` or similar spring annotation – hagrawal7777 Jun 15 '17 at 11:50
  • **UserService** is just a class that I use like a middle man between my classes and a **.properties** file. – Kilo Batata Jun 15 '17 at 11:53
  • @hagrawal I already have that in my context and I tried `@Component` still no luck – Kilo Batata Jun 15 '17 at 11:54
  • see https://stackoverflow.com/a/4703781/4920138 – dylaniato Jun 15 '17 at 11:54
  • Just check this thread https://stackoverflow.com/a/14879092/5790398. I think you need AspectJ dependency and configure weaving properly. Then it should works. – eg04lt3r Jun 15 '17 at 11:58
  • How are you getting a `User` instance? Because if you're just making `new User()` Spring won't be able to inject any dependency – Alberto S. Jun 15 '17 at 12:04
  • Ok, then how you are creating object of your `User` class? – hagrawal7777 Jun 15 '17 at 12:05
  • @Pelocho actually `user` is an `entity` so I get the error while the application is starting – Kilo Batata Jun 15 '17 at 12:13
  • 1
    As far as I know Spring doesn't initialize any `Entity` at init time. Try debugging the exception to see if it's Spring indeed who is raising the exception or it comes from your code – Alberto S. Jun 15 '17 at 12:19
  • 1
    You cannot do that. The initializer block is part of the constructor, dependencies are injected AFTER the object has been constructed. You are trying to use it before it ever could be injected. – M. Deinum Jun 15 '17 at 12:21
  • Yeah now I know, actually i puted that line just for testing, but even when i removed it the problem still persists and the service always null – Kilo Batata Jun 15 '17 at 12:28

2 Answers2

4

Spring managed components can be wired only into another beans managed by Spring.

However, there is a trick to add service to your POJO if you really need it:

  1. Add UserService as a static field to your POJO with a setter
  2. In UserService after spring initializes the bean, set itself as a field on the POJO (this can be done in @PostConstruct method)
dev4Fun
  • 990
  • 9
  • 18
0

Make a static instance of UserService available:

@Service
public Class UserService {
    private static UserService instance;
    public static UserService getInstance() { return instance; }

    @PostConstruct
    void init() { instance = this; }

    public String getMyName() { return "it's Me!";}
}

call with:

UserService.getInstance().getMyName()