[英]autowire beans.groovy in java spring boot
不了解如何在Java Spring Boot應用程序中正確使用beans.groovy。 我想在Java Bean中更改一些值,例如在xml中更改.groovy。 我該怎么辦? 我嘗試:我有主課:
@SpringBootApplication(scanBasePackages = "proj")
public class Application {
public static void main(String[] args) {
Object[] sources = {Application.class, new ClassPathResource("beans.groovy")};
SpringApplication.run( sources , args);
}
}
和一些TestBean。 我要初始化groovy有2個字段的名稱和姓氏:
@Component
public class TestBean {
public String name;
public String surname;
TestBean(){
System.out.println("INIT!!!!!");
}
}
我想通過Groovy bean來初始化TesbBean。 我在資源中添加了somebeans.groovy文件,並在其中添加了該文件:我想像這樣在我的@Controller中自動裝配它:@RestController
public class StaticContect {
@Autowired
TestBean testBean;
@RequestMapping(value = "test" , method = RequestMethod.GET)
public void test() {
System.out.println(testBean.name);
System.out.println(testBean.surname);
}
}
我將somefile.groovy添加到具有以下內容的資源中:
beans {
TestBean(){
name = "nametest"
surname = "surname"
}
}
但是我有一個空的int名稱和姓氏。
beans.groovy應該是
import proj.config.TestBean
beans {
testBean(TestBean, name = "nametest", surname = "surname")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.