How to inject value into bean properties in Spring

2728 ワード

In Spring、there are three ways to inject value into bean properties.
  • Normal way
  • Shotcut
  • 「p」schema
  • See a simple Java class,which contains two properties–name and type.Later you will use Spring to inject value into the bean properties.
    package com.mkyong.common;
    public class FileNameGenerator { 
        private String name; 
        private String type; 
        public String getName() { 
            return name; 
        } 
        public void setName(String name) { 
            this.name = name; 
        } 
        public String getType() { 
            return type; 
        } 
        public void setType(String type) {  
            this.type = type; 
        }
    }
    
    1.Normal way
    Inject value within a‘value’(and enclosed with‘property’)
    
    
        
            
                mkyong
            
            
                txt
            
        
    
    
    2.Shotcut
    Inject value with「value」atribute.
    
    
        
            
            
        
    
    
    3.「p」schema
    Inject value by using“p”schema as an atributes.
    
    
        
    
    
    Remember declares the xmlns:p=”http://www.springframework.org/schema/p in the Spring XML bean configration file.
    コンサート
    Which methods to use is totally base on personal preference,it will not affect the value inject into the bean properties.