我有一个模型,我想在其中注入我的服务。
我的模特
@Configurable
@Entity
@Table(name = "user")
public Class User {
@Autowired
private UserService userService;
{
System.out.println("Trying Service : " + userService.getMyName());
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我总是NullPointerException在第 7 行得到一个。
在我的 spring-context.xml 我有:
<context:spring-configured/>
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Run Code Online (Sandbox Code Playgroud)
编辑
用户服务
@Component
public Class UserService {
public String getMyName() { return "it's Me!";}
}
Run Code Online (Sandbox Code Playgroud) 我在/ src / main / resources /下有一个属性文件,我想使用Spring从中加载数据。
在我的Spring-context.xml中,我有这个:
<context:property-placeholder location="classpath:UserUtil.properties" />
Run Code Online (Sandbox Code Playgroud)
并且也有 <context:component-scan base-package="com.myApp" />
在我的课上,我像这样加载它:
@Value("${injectMeThis}")
private String injectMeThis;
Run Code Online (Sandbox Code Playgroud)
但该值始终为null
编辑:
检查值,我用这个:
System.out.println(new myClass().getInjectMeThis());
Run Code Online (Sandbox Code Playgroud)