[英]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; 我有三节课,我已经尽力简化了。
public class ClassA()
{
private variable neededVar;
public ClassA()
{
//initialise other variables;
}
public start()
{
/initialise neededVar;
}
@Bean
public variable getNeededVar()
{
return neededVar;
}
}
@SpringBootApplication
public class Application
{
private static ClassA myClass;
public static void main( String[] args )
{
myClass = new ClassA();
ClassA.start();
SpringApplication.run( Application.class, args );
}
}
@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! 谢谢!
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
机制一部分的类的对象。 @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. @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.