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技术】

<?xml version="1.0" encoding="ISO-8859-1"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">
      <bean id="bean1" class="com.ping.Bean1">
        <property name="strValue" value="zhangsan"/>
          <property name="intValue" value="1234"/>
          <property name="listValue" >
              <list>
                  <value>list1</value>
                  <value>list2</value>
                  <value>list3</value>
              </list>
          </property>
          <property name="setValue">
              <set>
                  <value>set1</value>
                  <value>set2</value>
                  <value>set3</value>
              </set>
          </property>
          <property name="mapValue">
              <map>
                  <entry key="key1" value="value1"/>
                  <entry key="key2">
                      <value>
                          value2
                      </value>
                  </entry>
                  <entry key="key3" value="value3"/>
              </map>
          </property>
          <property name="arrayStr" >
              <list>
                  <value>array1</value>
                  <value>array2</value>
                  <value>array3</value>
              </list>
          </property>
          <property name="dateValue" value="2019-02-08"/>
      </bean>
    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date" value="com.ping.ConverDate"/>
            </map>
        </property>
    </bean>
</beans>

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如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>myspringEL</artifactId>
        <groupId>com.ping</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../myspringEL/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
   <artifactId>myspringcore</artifactId>
 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
     <-https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值