简体   繁体   English

为什么我需要在控制器中使用spring注释?

[英]Why do I need to use spring annotation in my controller?

I'm new to play and also to spring. 我是新手,也是春天。 Started to work in a project and if I just create my controller class like this: 开始在项目中工作,如果我只是这样创建我的控制器类:

package controllers;

import play.mvc.Controller;
import play.mvc.Result;

public class Users extends Controller {

    public Result login(){
        return ok(views.html.login.render());
    }

}

I got this exception: 我有这个例外:

[NoSuchBeanDefinitionException: No qualifying bean of type [controllers.Users] is defined]

在此处输入图片说明

But, if I insert this annoation from spring on the top level: 但是,如果我从春季开始在顶层插入此注释:

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());
    }

}

The page is rendered. 页面已呈现。 But I don't know why this is happening. 但是我不知道为什么会这样。 I would like to use spring only when is necessary, work with play on its full capacity. 我只想在必要时使用spring,并在其最大容量下发挥作用。 So I wonder if I'm doing right using this annotation on my controller. 所以我想知道我是否在控制器上正确使用此注释。

EDIT: 编辑:

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
    }

}

EDIT 2: Guys, now I remember something in this project, we have 2 projects, one developed entirely on java and spring and this one, the webapp. 编辑2:伙计们,现在我记得这个项目中的某些内容,我们有2个项目,其中一个完全在javaspring上开发,而另一个是webapp。 We are importing the .jar built from the first project on this project with play. 我们正在从play导入从该项目的第一个项目构建的.jar We are doing like these so we don't have to maintain two different models. 我们正在这样做,因此我们不必维护两个不同的模型。

In your class Global you actually tell the Playframework to resolve controllers from the Spring application context: 在您的Global类中,您实际上告诉Playframework从Spring应用程序上下文解析控制器:

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

In this case it is clear, that you have to have Spring annotations on your controllers, otherwise they wouldn't be added to the application context. 在这种情况下,很明显,您必须在控制器上具有Spring批注,否则它们不会被添加到应用程序上下文中。 If you don't want to resolve controllers from the application context, remove that method. 如果您不想从应用程序上下文解析控制器,请删除该方法。 But keep in mind, that you will not be able to inject Spring components in your Play controllers then, since they are not managed by Spring. 但是请记住,由于Spring组件不受Spring组件的管理,因此您将无法在它们的Play控制器中注入它们。

Are you familiar with IoC (Inversion of Control) and DI (Dependency Injection)? 您是否熟悉IoC(控制反转)和DI(依赖项注入)? Spring IoC has a container It need to know every component. Spring IoC有一个容器,它需要知道每个组件。 You must to configure it with configuration files/configuration classes, and annotations can be useful to avoid have a extensive configuration. 您必须使用配置文件/配置类对其进行配置,并且注释对于避免进行广泛的配置很有用。

If you want to retreive a bean instance from Spring's context that bean must be managed by Spring . 如果要从Spring的上下文中检索bean实例,则该bean 必须由Spring管理 Adding @Controller to your controller and enabling component scan pointing to its package (or superpackage) tells to Spring to create an instance of that class inside its context, an instance you could lately use. 在控制器中添加@Controller并启用指向其包(或超级包)的组件扫描,将指示Spring在其上下文中创建该类的实例,您可以稍后使用该实例。

Having said that, here is explained why you got that exception. 话虽如此, 这里解释了为什么您遇到该异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么需要使用mapledBy注解? - Why do I need to use mappedBy Annotation? 在 Spring Kafka 中,我是否需要将 @EnableKafka 注释添加到我的应用程序中? - In Spring Kafka, do I need to add the @EnableKafka annotation to my application? 我需要哪个jar使用Spring AOP AspectJ Annotation? - Which jar do I need to use Spring AOP AspectJ Annotation? 为什么需要一种公共方法来使注释生效? - Why do I need a public method to make my annotation work? 为什么需要在控制器操作中添加@ResponseBody? - Why do I need to add @ResponseBody to my controller action? 如果我使用注释,是否需要在Spring Security xml中声明bean - Do i need to declare bean in spring security xml if i use annotation 映射注解有什么用,我需要它吗?(Spring Data ElasticSearch) - What's the use of a mapping annotation and do I need it?(Spring Data ElasticSearch) 为什么控制器中的@RequestMapping Spring注释会捕获更多我想要的? - Why does @RequestMapping Spring annotation in controller capture more that I want? 在 spring boot 中实现 TDD,我不能在控制器中使用 @valid 注释 - implementing TDD in spring boot and i cant use @valid annotation in controller 扩展 CrudRepository (Spring) 时是否需要 @Repository 注释? - Do I need @Repository annotation when extending CrudRepository (Spring)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM