当前位置:  开发笔记 > 编程语言 > 正文

如何通过Java配置设置Spring原型bean的属性?

如何解决《如何通过Java配置设置Spring原型bean的属性?》经验,为你挑选了1个好方法。

如果下面@AutowireBlahServicewith SCOPE_PROTOTYPE,我得到的IllegalArgumentException原因name是null:

@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
class BlahService {
   private String name;

   @PostConstruct
   public void init()
   {
      If (name == null) {
         throw new IllegalArgumentException("");
      }
   }

   private void setName(String name) {
       this.name = name;
   }
}

class Foo {
    @Autowired
    private BlahService service;
}

确保name被设置的正确方法是BlahService什么?



1> Betlista..:

我想,你有类似

@Bean
public BlahService getBlahService() {
    Blahservice bean = new BlahService();
    return bean;
}

并且您必须将其修改为

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public BlahService getBlahService() {
    Blahservice bean = new BlahService();
    bean.setName( findProperName() );
    retunrn bewn;
}

完整的测试是:

主要

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);

        BlahService bean1 = ac.getBean(BlahService.class);
        System.out.println(bean1.getName());

        BlahService bean2 = ac.getBean(BlahService.class);
        System.out.println(bean2.getName());

        FooService bean3 = ac.getBean(FooService.class);
        bean3.print();
    }
}

BlahService

package test;

public class BlahService {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

FooService

package test;

import org.springframework.beans.factory.annotation.Autowired;

public class FooService {

    @Autowired
    BlahService blahService;

    public void print() {
        System.out.println("FooService#print: " + blahService.getName());
    }

}

设定档

package test;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class Config {

    static int counter = 0;

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public BlahService getBlahService() {
        BlahService bean = new BlahService();
        bean.setName("name" + counter++);
        return bean;
    }

    @Bean
    public FooService getFooService () {
        return new FooService();
    }
}

执行Main#main打印:

@Bean
public BlahService getBlahService() {
    Blahservice bean = new BlahService();
    return bean;
}

编辑(扩展)

JUnit的

package test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Config.class)
public class MyTest {

    @Autowired
    BlahService blahService;

    @Autowired
    FooService fooService;

    @Test
    public void test() {
        System.out.println(blahService.getName());
        fooService.print();
    }

}

印刷品:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public BlahService getBlahService() {
    Blahservice bean = new BlahService();
    bean.setName( findProperName() );
    retunrn bewn;
}

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有