spring配置文件参数化、自定义类型转换器

spring配置文件参数化、自定义类型转换器,第1张

 

 

 

 

 

 

 

 

 

 applicationContext1.xml:








    

    
        
        
        
        
    

db.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/library?useSSlL=false
jdbc.username=root
jdbc.password=123456

applicationContext2.xml:







    
        
    

    
        
            
                
            
        
    

    
        
        
    



applicationContext3.xml:








    
        
        
    

    


    


Category类:
package com.itheima.beanpost;

public class Category {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
MyBeanPostProcessor类:
package com.itheima.beanpost;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        //首先进行判断是否是Category,才进行转换
        if (bean instanceof Category) {
            Category category = (Category) bean;
            category.setName("xiaowb");//重新加工赋值
        }
        return bean;
    }
}
MyDateConverter类:
package com.itheima.converte;

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyDateConverter implements Converter {

    private String pattern;

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    /*
          convert方法的作用:把String---Date
               SimpleDateFormat sdf=new SimpleDateFormat();
               sdf.parset(String)  ---Date
          param:source 代表的是配置文件中 日期字符串 2022-05-10

          return :当把转换好的Date作为convert方法的返回值后,Spring自动的为birthday属性进行注入

         */
    @Override
    public Date convert(String source) {
        Date date=null;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat(pattern);//通过配置文件进行格式的赋值
            date=sdf.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

Person类:

package com.itheima.converte;

import java.util.Date;

public class Person {
    private String name;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

测试类:SpringTest:

package com.itheima;

import com.itheima.beanpost.Category;
import com.itheima.constructer.Customer;
import com.itheima.converte.Person;
import com.itheima.factorybean.ConnectionFactoryBean;
import com.itheima.life.Product;
import com.itheima.scope.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class SpringTest {
    //配置文件参数化测试
    @Test
    public void test1(){
        //1.获得spring的工厂
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext1.xml");
        Connection conn = (Connection) ctx.getBean("conn");
        System.out.println("conn"+conn);


    }
    //用于测试自定义类型转换器
    /*
    
        
        
    
    自定义转换器将2022-05-10转换为Date类型
     */
    @Test
    public void test2(){
        //1.获得spring的工厂
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext2.xml");
        //2.通过工厂类获得对象
        Person person = (Person) ctx.getBean("person");
        System.out.println("person="+person);
        System.out.println("birthday:"+person.getBirthday());

    }

    //测试BeanPostProcessor,后置处理Bean重新赋值的测试,
    @Test
    public void test(){
        //1.获得spring的工厂
        ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext3.xml");
        //2.通过工厂类获得对象
        Category c = (Category) ctx.getBean("c");
        System.out.println("name="+c.getName());


    }

}

test1测试结果:

test2:

 

 test3:

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/920435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存