2017-03-01 4 views
0

스레드를 확장하는 클래스가 있습니다. 각 스레드는 자체 HTTP 서버에 연결합니다.쓰레드 스코프 스프링 빈 - 가능합니까?

스프링 빈을 구성하고 각 스레드에 사용자 정의 도메인을 전달하는 방법은 무엇입니까?

내가 아는 한, 스프링은 싱글 톤, 프로토 타입, 세션, 요청과 같은 범위를 지원한다.

답변

0

등록 정보를 기반으로 스레드 목록을 주입 할 수 있습니다. 다음과 같이 :

스레드 클래스.

public class MyThread implements Runnable{ 

    private String domain; 

    public MyThread(String domain) { 
     this.domain = domain; 
    } 

    @Override 
    public void run() { 
     System.out.println(domain); 
    } 

} 

속성 파일 :

t1.domain=www.domain1.com 
t2.domain=www.domain2.com 

는 config 클래스 :

List<MyThread> ll = (List<MyThread>)context.getBean("myThreads"); 
     for(MyThread t : ll){ 
      Thread th = new Thread(t); 
      th.start(); 
     } 
: 스레드를 사용

@Configuration 
@PropertySource(value="classpath:test.properties", name="testProperties") 
public class Config { 

    @Autowired 
    private Environment env; 

    @Bean 
    public List<MyThread> myThreads(){ 
     List<MyThread> list = new ArrayList<>(); 
     for(Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext();) { 
      org.springframework.core.env.PropertySource propertySource = (org.springframework.core.env.PropertySource) it.next(); 

      if ("testProperties".equals(propertySource.getName())) { 
       for(String propertyName : ((MapPropertySource) propertySource).getPropertyNames()){ 
        list.add(new MyThread(propertySource.getProperty(propertyName).toString())); 
       } 
      } 
     } 
     return list; 
    } 
} 

클래스