如果我在下面的@AutowireBlahService 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的正确方法是什么?

最佳答案

我想,你有类似

@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

10-04 17:16