[英]Why do I need to use spring annotation in my controller?
我是新手,也是春天。 開始在項目中工作,如果我只是這樣創建我的控制器類:
package controllers;
import play.mvc.Controller;
import play.mvc.Result;
public class Users extends Controller {
public Result login(){
return ok(views.html.login.render());
}
}
我有這個例外:
[NoSuchBeanDefinitionException: No qualifying bean of type [controllers.Users] is defined]
但是,如果我從春季開始在頂層插入此注釋:
package controllers;
import play.mvc.Controller;
import play.mvc.Result;
@org.springframework.stereotype.Controller
public class Users extends Controller {
public Result login(){
return ok(views.html.login.render());
}
}
頁面已呈現。 但是我不知道為什么會這樣。 我只想在必要時使用spring,並在其最大容量下發揮作用。 所以我想知道我是否在控制器上正確使用此注釋。
編輯:
import configuration.WebAppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import play.Application;
import play.GlobalSettings;
/**
* Application wide behaviour. We establish a Spring application context for the dependency injection system and
* configure Spring Data.
*/
public class Global extends GlobalSettings {
/**
* The name of the persistence unit we will be using.
*/
static final String DEFAULT_PERSISTENCE_UNIT = "default";
/**
* Declare the application context to be used - a Java annotation based application context requiring no XML.
*/
final private AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
/**
* Sync the context lifecycle with Play's.
*/
@Override
public void onStart(final 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.
ctx.register(SpringDataJpaConfiguration.class);
ctx.scan("controllers", "models");
ctx.refresh();
// This will construct the beans and call any construction lifecycle methods e.g. @PostConstruct
ctx.start();
}
/**
* Sync the context lifecycle with Play's.
*/
@Override
public void onStop(final Application app) {
// This will call any destruction lifecycle methods and then release the beans e.g. @PreDestroy
ctx.close();
super.onStop(app);
}
/**
* Controllers must be resolved through the application context. There is a special method of GlobalSettings that we
* can override to resolve a given controller. This resolution is required by the Play router.
*/
@Override
public <A> A getControllerInstance(Class<A> aClass) {
return ctx.getBean(aClass);
}
/**
* This configuration establishes the Spring context which in our case is defined in the "other" project.
*/
@Configuration
@Import(WebAppConfig.class)
public static class SpringDataJpaConfiguration {
// At the moment this class is just a entry point for the "other project" Spring context config, AppContext
}
}
編輯2:伙計們,現在我記得這個項目中的某些內容,我們有2個項目,其中一個完全在java
和spring
上開發,而另一個是webapp。 我們正在從play導入從該項目的第一個項目構建的.jar
。 我們正在這樣做,因此我們不必維護兩個不同的模型。
在您的Global
類中,您實際上告訴Playframework從Spring應用程序上下文解析控制器:
@Override
public <A> A getControllerInstance(Class<A> aClass) {
return ctx.getBean(aClass);
}
在這種情況下,很明顯,您必須在控制器上具有Spring批注,否則它們不會被添加到應用程序上下文中。 如果您不想從應用程序上下文解析控制器,請刪除該方法。 但是請記住,由於Spring組件不受Spring組件的管理,因此您將無法在它們的Play控制器中注入它們。
您是否熟悉IoC(控制反轉)和DI(依賴項注入)? Spring IoC有一個容器,它需要知道每個組件。 您必須使用配置文件/配置類對其進行配置,並且注釋對於避免進行廣泛的配置很有用。
如果要從Spring的上下文中檢索bean實例,則該bean 必須由Spring管理 。 在控制器中添加@Controller
並啟用指向其包(或超級包)的組件掃描,將指示Spring在其上下文中創建該類的實例,您可以稍后使用該實例。
話雖如此, 這里解釋了為什么您遇到該異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.