2014-12-22 105 views
1

当我尝试Spring-Dependency-InjectionPlayFrameworkJava 8整合,我得到以下错误,当使用play clean compile命令PlayFramework:集成春天依赖注入PlayFramework

------ 
value findUserById is not a member of com.harmeetsingh13.controllers.UserController 
[error] GET/@com.harmeetsingh13.controllers.UserController.findUserById(userId:Integer) 
------- 

以下是我的配置和代码

编译代码
public class GlobalConfiguration extends GlobalSettings{ 

private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); 

@Override 
public void onStart(Application app) { 
    super.onStart(app); 

    // AnnotationConfigApplicationContext can only be refreshed once, but we do it here even though this method 
    // can be called multiple times. The reason for doing during startup is so that the Play configuration is 
    // entirely available to this application context. 
    applicationContext.scan("com.harmeetsingh13"); 
    applicationContext.refresh(); 

    // This will construct the beans and call any construction lifecycle methods e.g. @PostConstruct 
    applicationContext.start(); 
} 

@Override 
public void onStop(Application app) { 
    // This will call any destruction lifecycle methods and then release the beans e.g. @PreDestroy 
    applicationContext.close(); 

    super.onStop(app); 
} 

@Override 
public <A> A getControllerInstance(Class<A> clazz) throws Exception { 
    return applicationContext.getBean(clazz); 
} 
} 

我Coltroller:

@Named 
@Singleton 
public class UserController extends Controller{ 


public static Result findUserById(Integer userId) { 
    /*Optional<User> user = userService.findUserById(userId); 
    if(user.isPresent()){ 

    }*/ 
    return null; 
} 
} 

我的路由文件如下:

Routes 

# Home page 
GET/@com.harmeetsingh13.controllers.UserController.findUserById(userId:Integer) 

回答

0

最后我找到了一答,当我们使用依赖注入Play Framework有没有必要宣布控制方法为静态。只需从控制器方法中删除静态关键字。这是因为,对象以bean的形式处理,IOC容器创建控制器bean,并从Bean获取方法定义。这就是为什么控制器中不需要静态方法的原因。

public Result findUserById(Integer userId) { 
------- 
return null; 
}