代码之家  ›  专栏  ›  技术社区  ›  Mohammed Housseyn Taleb

@Autowired springboot对象为空[重复]

  •  0
  • Mohammed Housseyn Taleb  · 技术社区  · 7 年前

    @Autowired 有静态字段。如果没有,还有别的办法吗?

    0 回复  |  直到 9 年前
        1
  •  118
  •   skaffman    17 年前

    简而言之,不可以。不能在Spring中自动或手动连接静态字段。要做到这一点,你必须写下自己的逻辑。

        2
  •  110
  •   Marco Sulla    8 年前
    @Component("NewClass")
    public class NewClass{
        private static SomeThing someThing;
    
        @Autowired
        public void setSomeThing(SomeThing someThing){
            NewClass.someThing = someThing;
        }
    }
    
        3
  •  66
  •   victor hugo    17 年前

    @Autowired

    最后一个建议。。。 不要

        4
  •  12
  •   ak-j    8 年前

    @Component
    public class TestClass {
       private static AutowiredTypeComponent component;
    
       @Autowired
       private AutowiredTypeComponent autowiredComponent;
    
       @PostConstruct
       private void init() {
          component = this.autowiredComponent;
       }
    
       public static void testMethod() {
          component.callTestMethod();
       }
    }
    
        5
  •  5
  •   Jherico    17 年前

    创建一个可以自动连接的bean,它将初始化静态变量作为副作用。

        6
  •  4
  •   Community Mohan Dere    9 年前

    XML表示法 MethodInvokingFactoryBean . 举个例子 here .

    private static StaticBean staticBean;
    
    public void setStaticBean(StaticBean staticBean) {
       StaticBean.staticBean = staticBean;
    }
    

    你应该瞄准使用 在可能的情况下进行弹簧喷射,因为这是推荐的方法

    注意,使用这种方法进行测试也会更加困难。

        7
  •  1
  •   Ori Marko    6 年前

    @Autowired
    private static String staticField = "staticValue";
    
        8
  •  0
  •   Shapur    7 年前

    您可以使用ApplicationContextAware

    @Component
    public class AppContext implements ApplicationContextAware{
        public static ApplicationContext applicationContext;
    
        public AppBeans(){
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    }
    

    然后

    static ABean bean = AppContext.applicationContext.getBean("aBean",ABean.class);
    
        9
  •  0
  •   jozsef morrissey    7 年前

    这绝对不是标准,很可能有更好的春季方式来做这件事。上面的答案都没有解决连接公共静态字段的问题。

    我想完成三件事。

    1. 使用spring“自动连线”(Im使用@Value)
    2. 公开公共静态值
    3. 防止修改

    private static String BRANCH = "testBranch";
    
    @Value("${content.client.branch}")
    public void finalSetBranch(String branch) {
        BRANCH = branch;
    }
    
    public static String BRANCH() {
        return BRANCH;
    }
    

    @Component
    @Aspect
    public class FinalAutowiredHelper {
    
    @Before("finalMethods()")
    public void beforeFinal(JoinPoint joinPoint) {
        throw new FinalAutowiredHelper().new ModifySudoFinalError("");
    }
    
    @Pointcut("execution(* com.free.content.client..*.finalSetBranch(..))")
    public void finalMethods() {}
    
    
    public class ModifySudoFinalError extends Error {
        private String msg;
    
        public ModifySudoFinalError(String msg) {
            this.msg = msg;
        }
    
        @Override
        public String getMessage() {
            return "Attempted modification of a final property: " + msg;
        }
    }
    

    这个方面将包装所有以final开头的方法,并在调用它们时抛出错误。

    我不认为这是特别有用的,但如果你是强迫症,喜欢让你豌豆和胡萝卜分开,这是一种方法,以确保安全。

    重要的 Spring在调用函数时不调用方面。让这件事更容易,更糟糕的是,我在搞清楚之前就已经搞清楚了逻辑。

        10
  •  -2
  •   double-beep hudson solomon    7 年前
    private static UserService userService = ApplicationContextHolder.getContext().getBean(UserService.class);