简体   繁体   English

Springboot,使用Rest Controller从对象访问字段

[英]Springboot, accessing field from object with Rest Controller

Having a bit of trouble with using Springboot to create an API, and understanding how Beans work. 使用Springboot创建API时遇到麻烦,并了解Beans的工作方式。

I have three classes, and I have simplified them as much as I can; 我有三节课,我已经尽力简化了。

ClassA A类

public class ClassA()
{
    private variable neededVar;

    public ClassA()
    {
        //initialise other variables;
    }

    public start()
    {
        /initialise neededVar;
    }

    @Bean
    public variable getNeededVar()
    {
        return neededVar;
    }
}

Application 应用

@SpringBootApplication
public class Application
{
    private static ClassA myClass;

    public static void main( String[] args )
    {
        myClass = new ClassA();
        ClassA.start();

        SpringApplication.run( Application.class, args );
    }
}

Controller 控制者

@RestController
public class Controller
{
    @Autowired
    private variable neededVar;

    @RequestMapping( "/temp" )
    public string getVar()
    {
        return neededVar.toString();
    }
}

My problem is that in the controller, I am not getting the neededVar from the created ClassA object myClass, I'm actually not sure what I'm getting. 我的问题是,在控制器中,我没有从创建的ClassA对象myClass中获得neededVar,实际上我不确定自己要得到什么。

I also tried doing it this way, with similar results. 我也尝试过以这种方式进行操作,结果相似。

@SpringBootApplication
public class Application
{
    private static ClassA myClass;
    private static variable myNeededVar;

    @Bean
    public variable getNeededVar()
    {
        return myNeededVar;
    }

    public static void main( String[] args )
    {
        myClass = new ClassA();
        myNeededVar = myClass.getNeededVar();
        ClassA.start();

        SpringApplication.run( Application.class, args );
    }
}

If anyone could point me in the correct direction of getting the neededVar from the instantiated ClassA in the application into my rest controller (and subsequently all the others I will create), that would be very much appreciated. 如果有人能指出正确的方向,将我需要的Var从应用程序中的实例化ClassA导入到我的rest控制器(以及随后将创建的所有其他控制器)中,那将不胜感激。

Thanks! 谢谢!

  • The spring doesn't exactly work in a way you are expecting. 春天不能完全按照您期望的方式工作。 You should delegate object's lifecycle to spring. 您应该将对象的生命周期委托给spring。 ie You do not need to manually create ClassA object and call its start method. 即,您无需手动创建ClassA对象并调用其start方法。
  • @Bean annotation is used to notify spring that you want to create an object of a class that was not part of spring's autoscan mechanism. @Bean批注用于通知spring您要创建不属于spring autoscan机制一部分的类的对象。
  • In order to delegate class object management to spring, class should be either annotated with @Component or it's other variants such as @Controller , @Service etc. or you should tell spring how to create object of that class using @Bean annotation. 为了将类对象管理委派给spring,应该使用@Component或其他变体(例如@Controller@Service @Controller等)对类进行注释,或者应告诉spring如何使用@Bean注释创建该类的对象。

To show how you can use @Component and @Bean , I'll mark Class A as component and inject Variable with @Bean . 为了展示如何使用@Component@Bean ,我将Class A标记为组件,并使用@Bean注入Variable

 @Component
    public class ClassA()
    {
         /*
          * Notice this autowired annotation. It tells spring to insert Variable object.
          * What you were trying to do with getNeededVar() is done using Autowired annotation
          */
        @Autowired
        private Variable neededVar;

    }

Now tell spring how to create object to Variable as it's not marked as @Component 现在告诉spring如何为Variable创建对象,因为它没有标记为@Component

@SpringBootApplication
public class Application
{
    public static void main( String[] args ) {
        SpringApplication.run( Application.class, args );
    }

  // This is how you register non spring components to spring context.
  // so that you can autowire them wherever needed
   @Bean
   public Variable variable() { 
      return new Variable();
   }
}

Your rest controller code stays as is. 您的休息控制器代码保持不变。 As Variable is registred to spring via @Bean in SpringApplication class, you don't really need ClassA . 由于Variable是通过@Bean类中的SpringApplication为spring的,因此您实际上不需要ClassA You can delete it. 您可以删除它。

@RestController
public class Controller
{
    @Autowired
    private Variable neededVar;

    @RequestMapping( "/temp" )
    public string getVar()
    {
        return neededVar.toString();
    }
}

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

相关问题 从部署在 Tomcat 上的 Springboot 应用程序访问 REST 端点时出现问题 - Problems accessing REST endpoints from Springboot application deployed on Tomcat 如何将 JSON 从静态文件传递到休息控制器 SpringBoot App - How to pass JSON from static file to rest controller SpringBoot App 没有 rest controller 的 SpringBoot 应用程序 - SpringBoot application without a rest controller 如何从SpringBoot的RestController中的REST请求中获取Calendar字段? - How to get Calendar field from REST request in RestController in SpringBoot? Rest Controller 在我的 Springboot 中似乎不起作用 - Rest Controller not seem to work in my Springboot JAXB - 将带有命名空间的XML发送到SpringBoot中的REST控制器 - JAXB - Send XML with namespace to REST controller in SpringBoot 从通用对象访问字段变量 - Accessing Field Variable from Generic Object 从作为第一个对象中的字段的对象访问对象的字段 - Accessing fields of an object from the object that is a field in the first object 通过来自其他类的Scene对象访问JavaFX的控制器类(对象) - Accessing Controller Class (Object) of JavaFX by Scene object from other classes How to pass JSON Object and return Object from Spring rest controller - How to pass JSON Object and return Object from Spring rest controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM