Announcement Announcement Module
Collapse
No announcement yet.
Where is wrong with Injecting Simple Values (Annotation)? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Where is wrong with Injecting Simple Values (Annotation)?

    I want to use Annotation to Injecting Simple Values,like follows:
    Code:
    package com.apress.prospring3.ch4.annotation;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.support.GenericXmlApplicationContext;
    import org.springframework.stereotype.Service;
    
    @Service("injectSimple")
    public class InjectSimple {
    	@Value("John Smith")
    	private String name;
    	
    	@Value("35")
    	private int age;
    	
    	@Value("1.78")
    	private float height;
    	
    	@Value("true")
    	private boolean programmer;
    	
    	@Value("1103760000")
    	private Long ageInSeconds;
    	
    	public static void main(String[] args){
    		GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();
    		ctx.load("classpath:app-context-annotation.xml");
    		ctx.refresh();
    		InjectSimple simple=(InjectSimple)ctx.getBean("injectSimple");
    		//ctx.getBean("injectRef");
    		System.out.println(simple);
    	}
    	
    	public void setAgeInSeconds(Long ageInSeconds) {
            this.ageInSeconds = ageInSeconds;
        }
    
    	public void setProgrammer(boolean programmer) {
    		this.programmer = programmer;
    	}
    
    	public void setAge(int age) {
            this.age = age;
        }
    
        public void setHeight(float height) {
            this.height = height;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    	public String toString(){
    		return "Name:"+name+"\n"
    			  +"Age:"+age+"\n"
    			  +"Age in Seconds:"+ageInSeconds+"\n"
    			  +"Height:"+height+"\n"
    			  +"Is Programmer?:"+programmer;
    	}
    }
    app-context-annotation.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    		
    	<bean id="injectSimple" class="com.apress.prospring3.ch4.annotation.InjectSimple"></bean>
    </beans>
    I hope I can get following result:
    Name:John Smith
    Age:35
    Age in Seconds:1103760000
    Height:1.78
    Is Programmer?:true

    But when I run it,it raise following result,where is wrong? How to correct above two file?
    Name:null
    Age:0
    Age in Seconds:null
    Height:0.0
    Is Programmer?:false

  • #2
    Probably XML is wrong

    Probably your app context xml is wrong.

    I tried same example with following app context annotation xml and it worked good...

    <?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"
    xmlns="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schem...ontext-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <context:annotation-config/>
    <context:component-scan
    base-package="com.apress.prospring3.ch4.annotation">

    </context:component-scan>
    </beans>

    Comment

    Working...
    X