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

时间:2016-01-08 06:52:20

标签: java spring

如果我@Autowire BlahService下面有SCOPE_PROTOTYPE,我会得到IllegalArgumentException,因为name为空:

@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 个答案:

答案 0 :(得分:6)

我认为,你有类似

的东西
@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打印:

name1
name2
FooService#print: name0

编辑(扩展)

<强> 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();
    }

}

打印:

name1
FooService#print: name0