簡體   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