簡體   English   中英

Spring 引導不注入 Bean 與 web 感知 scope

[英]Spring Boot not injecting Bean with web aware scope

If I try to inject a bean with a web aware scope (ie session scope, request scope), the injector ignores the bean. 沒有調用 bean 方法和 object 構造函數。 這只發生在我聲明的類中,因為我可以為標准庫類型(例如 List 或 Map)注入 session 范圍的 bean。 此外,如果我使用 singleton 或原型范圍,注入工作正常。

有人可以解釋這種奇怪的行為嗎? 我創建了一個准系統示例來演示該問題。 (我也嘗試過搜索,但找不到遇到此問題的人。)

Class 我想注入

public class CustomObj{
    public String field;

    public CustomObj(){
        System.out.println("CustomObj constructor called");
    }
}

配置文件

@Configuration
public class Config {

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public List<String> userValues() {
        List<String> list = new ArrayList<String>();
        list.add("This gets initialized");
        return list;
    }

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public CustomObj customObj() {
        CustomObj obj = new CustomObj();
        obj.field = "This doesn't";
        return obj;
    }
}

Controller 注入對象

@RestController
@RequestMapping("/test")
public class TestController{

    @Autowired
    List<String> userValues;

    @Autowired
    CustomObj customObj;

    @RequestMapping("/pleasework")
    public String please(){
        return "List values: "+Arrays.toString(this.userValues.toArray())
            + " Obj value: "+this.customObj.field;
    }
}

主要的

@SpringBootApplication
public class SessionbeansApplication {

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

}

訪問 /test/pleasework 會給出 output List values: [This gets initialized] Obj value: null ,表明列表已正確注入,但 CustomObj 未正確注入。

如果您運行您的代碼 output 將類似於List values: [This gets initialized] Obj value: null

您沒有收到NullpoiterException ,這意味着您的CustomObj-bean已創建

您應該在 CustomObj 中使用 getter 和 setter。 並使用 getter 訪問字段: this.customObj.getField();

    @RequestMapping("/pleasework")
    public String please(){
        System.out.println(userValues +  " and " + customObj);
        return "List values: "+ Arrays.toString(this.userValues.toArray())
                + " Obj value: " + this.customObj.getField();// <--- use getter
    }

CustomObj中添加toString()也可以

class CustomObj {
    public String field;

    public CustomObj(){
        System.out.println("CustomObj constructor called");
    }

    @Override
    public String toString() {
        return "CustomObj{" +
                "field='" + field + '\'' +
                '}';
    }
}

....

  @RequestMapping("/pleasework")
    public String please(){
        System.out.println(userValues +  " and " + customObj);
        return "List values: "+ Arrays.toString(this.userValues.toArray())
                + " Obj value: " + this.customObj; //<---- use toString()
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM