2013-05-17 7 views
0

작동하지 않습니다 ("프로토 타입")는 다음과 같은 구성을@Scope 제대로

public class MainApp { 
     public static void main(String args[]){ 

      ApplicationContext ac=new ClassPathXmlApplicationContext("src/Beans.xml"); 
      HelloWorld obj1=(HelloWorld) ac.getBean("helloWorld"); 
      obj1.setMessage("OBJ1"); 
      HelloWorld obj2=(HelloWorld) ac.getBean("helloWorld"); 
      //obj2.setMessage("OBJ2"); 
      System.out.println(obj1.getMessage()); 
      System.out.println(obj2.getMessage()); 
     } 
    } 

    @Scope("prototype") 

    public class HelloWorld { 
    String message; 
     public String getMessage() { 
      return "Your Message:"+message; 
     } 
     public void setMessage(String message) { 
      this.message = message; 
     } 
     } 


    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
       <context:annotation-config /> 
       <context:component-scan base-package="SpringDemo.src" /> 
       <bean id="helloWorld" class="src.HelloWorld"> 
      </bean> 
      </beans> 

을 고려하십시오. 누군가가 왜 "프로토 타입"범위로 행동하지 않는지 알려주실 수 있습니까?

+0

'src.HelloWorld' 클래스 이름이 잘못되었습니다. 'src' 디렉토리를'package'로 지정할 필요가 없습니다 –

+0

제거하면 에러가납니다 클래스 경로 [src/Beans.xml]에 'helloWorld'라는 이름의 bean이있는 클래스 [HelloWorld]를 찾을 수 없습니다. ; 나는 결과를 얻고 있지만 싱글 톤 스코프가 아닌 프로토 타입은 – Aashish

+0

입니다. @Aashish 여러분이보고있는 동작은 정확히 무엇입니까? – soulcheck

답변

4

<bean id="helloWorld" class="src.HelloWorld">은 xml 구성에 포함되어 있습니다. 범위를 지정하지 않으면 scope의 기본값은 singleton입니다. xml 구성은 주석을 덮어 씁니다. @Scope("prototype")을 제거하고 scope="prototype"을 XML에 추가하십시오.

+0

그러나 예프 게니, 나는 그런 다음 주석 – Aashish

+0

를 사용하여 범위를 정의에만 주석을 사용하면 XML bean 정의 –

+0

를 제거하려면 나는 그것을 제거하고 오류가 발생했다 "main"스레드에서 예외 org.springframework.beans.factory.NoSuchBeanDefinitionException : 'helloWorld'라는 Bean이 정의되지 않았다. – Aashish

관련 문제