• 欢迎光临~

Spring类型转换器和静态代理

开发技术 开发技术 2022-06-21 次浏览

1、创建一个类型转换器,可以将用户给定的工资(Double)类型,转换成BigDecimal类型

2、使用静态代理模式,完成孙悟空对高翠兰的代理

   孙悟空根据高翠兰的样貌,变成高翠兰,可以和猪八戒战斗,可以抓住猪八戒

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="person" class="com.xzit.model.Person" p:name="张三" p:age="20" p:birthday="1999/12/12"></bean>
    <!-- 自定义类型转换器 -->
    <bean id="converter" class="com.xzit.model.DateConverter" p:pattern="yyyy/MM/dd"></bean>
    <!-- 对类型转换器进行注册 -->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="converter"></ref>
            </set>
        </property>
    </bean>
    <bean id="stu" class="com.xzit.model.Student" p:name="李四" p:birth="1997/02/01"></bean>
</beans>
package com.xzit.model;

import lombok.Data;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Data
public class DateConverter implements Converter<String, LocalDate> {
    private String pattern;
    @Override
    public LocalDate convert(String source) {
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern(pattern);
        LocalDate date=LocalDate.parse(source,formatter);
        return date;
    }
}
package com.xzit.model;

import lombok.Data;

import java.time.LocalDate;

@Data
public class Person {
    private String name;
    private int age;
    private LocalDate birthday;
}
package com.xzit.model;

import lombok.Data;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Data
public class Student {
    private String name;
    private LocalDate birth;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", birth=" + birth.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")) +
                '}';
    }
}
package com.xzit.model;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
        //Person bean = context.getBean(Person.class);
        Student student=context.getBean(Student.class);
        System.out.println(student);
        System.out.println(student.getBirth().getClass().getName());
    }
}

 

 

package com.xzit.model;

public interface Change {
    void change();
}
package com.xzit.model;

public class GaoYuLan implements Change{

    @Override
    public void change() {
        System.out.println("高玉兰抓住猪八戒");
    }
}
package com.xzit.model;

public class WuKong implements Change{
    private GaoYuLan gaoYuLan;
    @Override
    public void change() {
        wukong();
        if (gaoYuLan==null)
            gaoYuLan=new GaoYuLan();
        gaoYuLan.change();
    }
    public void wukong(){
        System.out.println("孙悟空变成");
    }
}
package com.xzit.test;

import com.xzit.model.Change;
import com.xzit.model.WuKong;

public class Test2 {
    public static void main(String[] args) {
        Change c=new WuKong();
        c.change();
    }
}

 

程序员灯塔
转载请注明原文链接:Spring类型转换器和静态代理
喜欢 (0)