繁体   English   中英

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

[英]How to set the properties of a Spring prototype bean via Java configs?

如果我@AutowireBlahServiceSCOPE_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;
}

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

我想,你有类似

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM