2016-12-19 6 views
1

빈 범위를 스프링 4으로 프로토 타입으로 내 프로그램 중 하나에서 사용하려고했는데 요청마다 다른 개체가 만들어 지는지 확인합니다. 그것을 위해 나는 다음 코드를 사용했다 : 스프링 4가 범위 속성을 지원하지 않습니까?

<bean id="television" class = "org.java.springcore.Television" scope="prototype"> 
    <property name="model" value="Samsung_6970"/> 
    <property name="yearOfManufature" value="2016"/> 
    <property name="diameter" value="55"/>  
</bean> 

은 그 때 나는 다음과 같은 내 주요 클래스에있는 삼각형 객체를 초기화 :

public class TelevisionUser { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // BeanFactory factory = new XmlBeanFactory(new 
     // FileSystemResource("spring.xml")); 
     AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
     context.registerShutdownHook(); 

     Television television1 = (Television) context.getBean("television"); 

     television1.setMsg("Setting messege for Television"); 
     System.out.println("The message for television 1 is: "+television1.getMsg()); 
     Television television2 = (Television) context.getBean("television"); 

     System.out.println("The message for television 2 is: "+television2.getMsg()); 

    } 
} 

를 다음과 같이 내 텔레비전 클래스는 다음과 같습니다

public class Television implements InitializingBean 
{ 
    private Integer yearOfManufature; 
    private String model; 
    private Integer diameter; 
    private String msg; 

    public String getModel() { 
     return model; 
    } 
    public void setModel(String model) { 
     this.model = model; 
    } 
    public Integer getYearOfManufature() { 
     return yearOfManufature; 
    } 
    public void setYearOfManufature(Integer yearOfManufature) { 
     this.yearOfManufature = yearOfManufature; 
    } 
    public Integer getDiameter() { 
     return diameter; 
    } 
    public void setDiameter(Integer diameter) { 
     this.diameter = diameter; 
    } 
    /** 
    * @return the msg 
    */ 
    public String getMsg() { 
     return msg; 
    } 
    /** 
    * @param msg the msg to set 
    */ 
    public void setMsg(String msg) { 
     this.msg = msg; 
    } 

    @Override 
    public void afterPropertiesSet() throws Exception { 
     System.out.println("Initialising the Bean Television"); 

    } 
} 

질문이 2 개 있습니다.

  1. scope 속성을 사용할 때 XML 유효성 검사기가 오류를 던지고 있습니다. "속성"범위 "를 요소 유형"bean "에 대해 선언해야합니다. ?. 초기화 중

    : " 내가 속성 싱글을 사용하고 false로 값의 설정하면 범위가 봄 4

  2. 에 대한 사용에 더 이상 속성 없으며, 다음 내 프로그램이 이상하게 작동되고 출력이오고있다, 즉 빈이 텔레비전 텔레비전 1에 대한 메시지는 : 텔레비전 2 텔레비전 메시지에 대한 설정 messege입니다 빈이에도 출력으로부터 명백한 바와 번만 초기화되는 텔레비전

위한 messege 설정 나는 se이다. tting singleton = "false". 따라서, 메시지는 텔레비전 (1)에 대해서도 설정되고 텔레비전 (2)에 대해서도 반사된다.

어디서 잘못 될지 이해할 수 없습니다.

답변

3

scopebean 요소는 여전히 스프링 4에서 지원되며, http://www.springframework.org/schema/beans/spring-beans-4.3.xsd beans 스키마를 참조하십시오.

singleton이 사라 졌으므로 예제가 실패 할 가능성이 있습니다. 내부적으로는 여전히 지원되지만 나중에 완전히 제거 될 수도 있지만 Spring 2.0부터 사라졌습니다. 당신이 적절한 스키마 위치가 확인 여부를

  • : 나는 당신의 예를 몇 가지 이중 공간을 참조 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"

  • 를,이 단지 있는지 확인 검사기는 여러분의 빈 정의에 실패하는 이유에 대해

    공백 또는 탭 및 유니 코드의 별난 문자가 없습니다.

+0

감사합니다. Zybnek. 너는 맞다. 어떻게 든 Doctype 선언에서 Spring 버전을 언급하지 않았다. –

+0

대신 를 언급했습니다. . 그러므로 문제.결과는 다음과 같습니다. Bean Television 초기화하기 텔레비전 1에 대한 메시지는 다음과 같습니다. 텔레비전에 대한 메시지 전달 Bean Television 초기화 텔레비전 2에 대한 메시지는 다음과 같습니다. null –