Spring框架03

类型转换器的使用:

作用:自定义注入参数和实体类中类型的匹配方式

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

public class MyConverter  implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = simpleDateFormat.parse(source);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}

xml文件配置:

<!--托管converter-->
    <bean id="converter" class="cn.kgc.spring04.convertor.MyDateConverter">
        <property name="pattern" value="yyyy-MM-dd"></property>
    </bean>
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
               <ref bean="converter"></ref>
            </set>
        </property>
    </bean>

 

Bean的自动装配

1.autowire="byName"在容器的上下文中寻找与类中属性对应的set方法名字相同的id属性值进行装配

	<bean id="teacher" class="cn.kgc.spring.entity.Teacher">
        <property name="name" value="李老师"/>
        <property name="teaNo" value="001"/>
    </bean>
    <bean id="classRoom1" class="cn.kgc.spring.entity.ClassRoom">
        <property name="address" value="学思楼1楼"/>
        <property name="classNo" value="1"/>
    </bean>
    <bean id="student" class="cn.kgc.spring.entity.Student" autowire="byName" ></bean>

2.autowire="byType"在容器的上下文中寻找与类中属性类型相同的Bean进行装配

<bean id="teacher" class="cn.kgc.spring.entity.Teacher">
        <property name="name" value="李老师"/>
        <property name="teaNo" value="001"/>
    </bean>
    <bean id="classRoom1" class="cn.kgc.spring.entity.ClassRoom">
        <property name="address" value="学思楼1楼"/>
        <property name="classNo" value="1"/>
    </bean>
    <bean id="student" class="cn.kgc.spring.entity.Student" autowire="byType" ></bean>

3.使用注解自动装配

1.导入context约束

<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"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd" >

2.开启注解支持

<context:annotation-config/>

3.即可使用注解自动装配

public class Student {

    @Value("2021")
    private String stuNo;
    @Value("wangwu")
    private String name;
    @Value("20")
    private int age;
    @Value("2021/12/08")
    private Date birth;

    @Autowired  
    private Teacher teacher;
    @Resource 
    private ClassRoom classRoom;
    
}

获取配置文件中的值

public class Aoo {

    @Value("${test.boolean}")
    private Boolean testBoolean;

    @Value("${test.string}")
    private String testString;

    @Value("${test.integer}")
    private Integer testInteger;

    @Value("${test.long}")
    private Long testLong;

    @Value("${test.float}")
    private Float testFloat;

    @Value("${test.double}")
    private Double testDouble;

    @Value("#{'${test.array}'.split(',')}")
    private String[] testArray;

    @Value("#{'${test.list}'.split(',')}")
    private List<String> testList;

    @Value("#{'${test.set}'.split(',')}")
    private Set<String> testSet;

    @Value("#{${test.map}}")
    private Map<String, Object> testMap;
    
}

配置文件 properties

test.boolean=true
test.string=abc
test.integer=123
test.long=123
test.float=1.2345678123456
test.double=1.2345678123456
test.array=1,3,4,5,6,1,2,3
test.list=1,3,4,5,6,1,2,3
test.set=1,3,4,5,6,1,2,3
test.map={name:"zhangsan", age:18}

spring中复杂对象的创建

1.FactoryBean

public class ConnectionFactoryBean  implements FactoryBean<Connection> {

    @Override
    public Connection getObject() throws Exception {

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql:///java2215?serverTimezone=UTC", "root", "root");
        return connection;
    }

    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

xml配置方式:

<bean id="conn" class="cn.kgc.spring.ioc.entity.ConnectionFactoryBean"></bean>

 2.实例工厂

public class ConnectionFactoryBean {
    
    public Connection getConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql:///java2215?serverTimezone=UTC&useSSL=false", "root", "root");
        return connection;
    }

}

 xml配置方式:

<bean id="conn" class="cn.kgc.spring.ioc.entity.ConnectionFactoryBean"></bean>
<bean id="connection" factory-bean="conn" factory-method="getConnection"></bean>

 3.静态工厂

public class ConnectionFactoryBean {

    public static Connection getConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql:///java2215?serverTimezone=UTC&useSSL=false", "root", "root");
        return connection;
    }

}

xml配置方式:

<bean id="conn" class="cn.kgc.spring.ioc.entity.ConnectionFactoryBean" factory-method="getConnection"></bean>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值