springCore使用案例

测试普通类属性的注入

1.普通类

package com.ping;
import java.util.*;
public class Bean1 {
    //测试普通类属性的注入
    private int intValue;
    private String  strValue;
    private List listValue;
    private Map mapValue;
    private Set setValue;
    private String [] arrayStr;
    private Date dateValue;
    public Bean1(){}

    public int getIntValue() {
        return intValue;
    }

    public void setIntValue(int intValue) {
        this.intValue = intValue;
    }

    public String getStrValue() {
        return strValue;
    }

    public void setStrValue(String strValue) {
        this.strValue = strValue;
    }

    public List getListValue() {
        return listValue;
    }

    public void setListValue(List listValue) {
        this.listValue = listValue;
    }

    public Map getMapValue() {
        return mapValue;
    }

    public void setMapValue(Map mapValue) {
        this.mapValue = mapValue;
    }

    public Set getSetValue() {
        return setValue;
    }

    public void setSetValue(Set setValue) {
        this.setValue = setValue;
    }

    public String[] getArrayStr() {
        return arrayStr;
    }

    public void setArrayStr(String[] arrayStr) {
        this.arrayStr = arrayStr;
    }

    public Date getDateValue() {
        return dateValue;
    }

    public void setDateValue(Date dateValue) {
        this.dateValue = dateValue;
    }

    @Override
    public String toString() {
        return "Bean1{" +
                "intValue=" + intValue +
                ", strValue='" + strValue + '\'' +
                ", listValue=" + listValue +
                ", mapValue=" + mapValue +
                ", setValue=" + setValue +
                ", arrayStr=" + Arrays.toString ( arrayStr ) +
                ", dateValue=" + dateValue +
                '}';
    }
}

2.由于Date类型在 sping中无法转换,需要自定义类进行转换(如下)

package com.ping;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ConverDate extends PropertyEditorSupport  {
    public String partttern="yyyy-mm--dd";
    @Override
    public  void  setAsText(String text){
        System.out.println ("text"+text );
        try {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat ( partttern );
            Date date=simpleDateFormat.parse ( text );
            this.setValue ( date );
        } catch (ParseException e) {
            e.printStackTrace ( );
        }
    }

}

3.添加spring核心配置文件 applicationContext.xml 【spring、spring框架,spring容器,spring技术】



      
        
          
          
              
                  list1
                  list2
                  list3
              
          
          
              
                  set1
                  set2
                  set3
              
          
          
              
                  
                  
                      
                          value2
                      
                  
                  
              
          
          
              
                  array1
                  array2
                  array3
              
          
          
      
    
        
            
                
            
        
    

4.测试类进行测试

package com.ping;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoTest1 {
    private ApplicationContext context;
    @Before
    public  void initMethod(){
       context=new ClassPathXmlApplicationContext ( "applicationContext2.xml" ) ;
    }
    @Test
    public  void  testAANdB(){
        Bean1 bean1=(Bean1)context.getBean ( "bean1" );
        System.out.println ("bean-strVlaue"+bean1.getStrValue () );
        System.out.println ("bean-mapValue"+bean1.getMapValue () );
        System.out.println ("bean-ArrayStr"+bean1.getArrayStr () );
        System.out.println (bean1.getSetValue () );
        System.out.println (bean1.getListValue () );
        System.out.println (bean1.getIntValue () );
        System.out.println (bean1.getDateValue () );
    }
}

5.所依赖的 pom.xml如下:



    
        myspringEL
        com.ping
        1.0-SNAPSHOT
        ../myspringEL/pom.xml
    
    4.0.0
   myspringcore
 
        
        
            org.springframework
            spring-context
            5.1.5.RELEASE
        
     <-https://mvnrepository.com/artifact/org.springframework/spring-core -->
        
            org.springframework
            spring-core
            5.1.5.RELEASE
        

        
        
            org.springframework
            spring-beans
            5.1.5.RELEASE
        
        
        
            org.springframework
            spring-aop
            5.1.5.RELEASE
        
        
        
            org.springframework
            spring-tx
            5.1.5.RELEASE
        
        
            junit
            junit
            4.12
            test
        
    

 

你可能感兴趣的:(springCore使用案例)