gpt4 book ai didi

java - 为什么我需要在 Controller 中使用 spring 注解?

转载 作者:行者123 更新时间:2023-12-01 18:08:23 25 4
gpt4 key购买 nike

我是新手,也是 Spring 的新手。开始在项目中工作,如果我只是像这样创建 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]

enter image description here

但是,如果我在顶层插入 spring 的这个注释:

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 ,充分发挥其作用。所以我想知道我在 Controller 上使用这个注释是否正确。

编辑:

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 个项目,一个完全基于 javaspring 开发,另一个是 webapp。我们正在通过 play 导入从第一个项目构建的 .jar 。我们这样做是为了不必维护两个不同的模型。

最佳答案

在您的类Global中,您实际上告诉 Playframework 从 Spring 应用程序上下文解析 Controller :

@Override
public <A> A getControllerInstance(Class<A> aClass) {
return ctx.getBean(aClass);
}

在这种情况下,很明显,您必须在 Controller 上使用 Spring 注释,否则它们将不会添加到应用程序上下文中。如果您不想从应用程序上下文解析 Controller ,请删除该方法。但请记住,您将无法在 Play Controller 中注入(inject) Spring 组件,因为它们不受 Spring 管理。

关于java - 为什么我需要在 Controller 中使用 spring 注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34653525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com