简体   繁体   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. If I try to inject a bean with a web aware scope (ie session scope, request scope), the injector ignores the bean. The bean method and object constructor are not called.没有调用 bean 方法和 object 构造函数。 This only happens for classes I have declared, as I can inject a session scoped bean for standard library types such as List or Map.这只发生在我声明的类中,因为我可以为标准库类型(例如 List 或 Map)注入 session 范围的 bean。 In addition, injection works fine if I use singleton or prototype scopes.此外,如果我使用 singleton 或原型范围,注入工作正常。

Can someone explain this strange behavior?有人可以解释这种奇怪的行为吗? I created a barebones sample to demonstrate the issue.我创建了一个准系统示例来演示该问题。 (I also tried searching, but couldn't find anyone who had this issue.) (我也尝试过搜索,但找不到遇到此问题的人。)

Class I'd like to inject Class 我想注入

public class CustomObj{
    public String field;

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

Configuration file配置文件

@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 to inject the objects 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;
    }
}

Main主要的

@SpringBootApplication
public class SessionbeansApplication {

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

}

Accessing /test/pleasework gives output List values: [This gets initialized] Obj value: null , showing that the List is injected properly but CustomObj is not.访问 /test/pleasework 会给出 output List values: [This gets initialized] Obj value: null ,表明列表已正确注入,但 CustomObj 未正确注入。

If you run your code the output will be like List values: [This gets initialized] Obj value: null如果您运行您的代码 output 将类似于List values: [This gets initialized] Obj value: null

You are NOT getting a NullpoiterException , this means that your CustomObj-bean is created您没有收到NullpoiterException ,这意味着您的CustomObj-bean已创建

You should use getters and setter in your CustomObj.您应该在 CustomObj 中使用 getter 和 setter。 And use getter to access field: this.customObj.getField();并使用 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
    }

adding toString() in CustomObj will work alsoCustomObj中添加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