gpt4 book ai didi

spring - 使用 Kotlin 解决 Spring Boot Rest Controller 中的单例

转载 作者:行者123 更新时间:2023-12-02 13:16:30 38 4
gpt4 key购买 nike

我是 Spring 和 Spring Boot 的新手,我尝试了不同的方式来解决 Beans。在我的示例中,我有一个应该 的 Bean。总是 成为单例。令我惊讶的是,似乎有一种方法可以将这个 bean 解析为我认为的“原型(prototype)”。
谁能向我解释为什么在方法签名中解决它不是单例 showSingletonBeans ?

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}

@Service("stackSingletonBean")
// @Scope("singleton")
class MySingletonBean {
init {
println("Created MySingletonBean " + this.hashCode())
}
}

@RestController
class MyController {

@Autowired
// @Qualifier("singletonBean")
lateinit var memberSingletonBean: MySingletonBean

@Autowired
lateinit var singeltonFactory: ObjectFactory<MySingletonBean>

fun buildSingleton() : MySingletonBean {
return singeltonFactory.`object`
}

@Lookup
fun getSingletonInstance() : MySingletonBean? {
return null
}

@GetMapping("/")
fun showSingletonBeans(@Autowired stackSingletonBean: MySingletonBean) {
println("member " + memberSingletonBean.hashCode() )
println("stack " + stackSingletonBean.hashCode())
println("lookup:" + getSingletonInstance().hashCode())
println("factory: " + buildSingleton().hashCode())
}
}
日志如下所示:
2020-08-13 18:44:32.604  INFO 172175 --- [           main] com.example.demo.DemoApplicationKt       : No active profile set, falling back to default profiles: default
2020-08-13 18:44:33.118 INFO 172175 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-13 18:44:33.124 INFO 172175 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-13 18:44:33.124 INFO 172175 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-13 18:44:33.164 INFO 172175 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-13 18:44:33.164 INFO 172175 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 528 ms
Created MySingletonBean 1747702724
2020-08-13 18:44:33.286 INFO 172175 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-13 18:44:33.372 INFO 172175 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-13 18:44:33.379 INFO 172175 --- [ main] com.example.demo.DemoApplicationKt : Started DemoApplicationKt in 1.011 seconds (JVM running for 1.24)
2020-08-13 18:44:37.341 INFO 172175 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-13 18:44:37.341 INFO 172175 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-08-13 18:44:37.344 INFO 172175 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
Created MySingletonBean 562566586
member 1747702724
stack 562566586
lookup:1747702724
factory: 1747702724
Created MySingletonBean 389331797
member 1747702724
stack 389331797
lookup:1747702724
factory: 1747702724

最佳答案

解析 Controller 方法参数实际上是完全不同的机制。它与依赖注入(inject)和 @Autowired 无关。注释:注释可以被删除,它不会改变行为。

Although @Autowired can technically be declared on individual method or constructor parameters since Spring Framework 5.0, most parts of the framework ignore such declarations. The only part of the core Spring Framework that actively supports autowired parameters is the JUnit Jupiter support in the spring-test module (see the TestContext framework reference documentation for details).https://docs.spring.io/


在您的情况下, stackSingletonBeanModelAttributeMethodArgumentResolver 实例化.它不知道 @Service注释也不是它的范围:它只是在每个请求上使用默认构造函数。

Model attributes are sourced from the model, or created using a default constructor and then added to the model.


Note that use of @ModelAttribute is optional — for example, to set its attributes. By default, any argument that is not a simple value type( as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver is treated as if it were annotated with @ModelAttribute. Web on Reactive Stack

关于spring - 使用 Kotlin 解决 Spring Boot Rest Controller 中的单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63399721/

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