繁体   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