对IOC最简单的理解:
IOC就是创建对象和管理对象;
他是spring的底层,负责bean的装载、分配、实例化、管理生命周期;
spring命名空间的理解
命名空间相当于磁盘里的D盘和F盘,每个命名空间里相互独立的,在同一个命名空间里不能出现相同的东西,否则会被覆盖。
spring的配置文件开头会有个<beans>标签,里面放的就是spring的命名空间:
其肉眼可见的作用就是:在编码时会有提示,去掉就没有,而且还报错!
spring bean:比较两个bean是否相同?
在实际业务中,经常会有比较两种物品是否相同,投射到程序上则表现为比较他们的属性是否相同,假如要通过if else去比较他们的关键属性,实在是麻烦。其实可以通过equals来比较
每个实体类里都会有个hasCode方法,他是用来记录属性变化的,通过这些变化来计算对象的内存地址,
每个实体类同时也有equals方法,这就是比较两类是否相同,里面先是比较他们的内存地址,然后比较他们的属性值是否相同,这时我们可以改写这个方法,去掉多余的属性,只留下关键属性,这样我们就可以用equals来比较两个对象是否相同了。
bean:构造函数注入
<bean id="user5" class="com.tz.ioc.User">
<constructor-arg index="0" type="java.lang.String" value="keke"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="123456ADSDF">
</constructor-arg>
<constructor-arg index="2" type="java.lang.Integer"><value>30</value></constructor-arg>
<constructor-arg index="3" type="java.lang.Float" value="89.9234"></constructor-arg>
<constructor-arg index="4" type="java.lang.String" value="578979789797979"></constructor-arg>
<constructor-arg index="5" type="java.lang.String" value="湖南长沙"></constructor-arg>
</bean>
public User(String username, String password, Integer age, Float score,
String iDcard, String address) {
super();
this.username = username;
this.password = password;
this.age = age;
this.score = score;
IDcard = iDcard;
this.address = address;
}
简单对象配置:
<bean id="user3" class="com.tz.ioc.User"
p:username="keke"
p:password="112487897"
p:IDcard="23423456234756723684"
p:age="30"
p:address="士大夫士大夫">
</bean>
<bean id="user4" class="com.tz.ioc.User"
p:username="keke"
p:password="112487897">
<property name="IDcard" value="54545646"></property>
<property name="score" value="6545"></property>
<property name="age"><value>20</value></property>
<property name="address" value="士大夫地方士大夫"></property>
</bean>
集合list配置:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="customPropertyEditorRegistrar"/>
</list>
</property>
<property name="students">
<list>
<ref bean="doudou"/>
<ref bean="jiandan"/>
<ref bean="jony"/>
</list>
</property>
</bean>
<bean id="customPropertyEditorRegistrar" class="com.tz.ioc.CustomPropertyEditorRegistrar"/>
set配置:
<property name="jobs">
<set>
<value>服务生</value>
<value>帮转工</value>
<value>程序员</value>
<value>厨师</value>
<value>厨师</value>
</set>
</property>
数组配置:
数组 简写方式:p:names="宝宝,小明,小红"
<property name="names">
<array> -->
<value>宝宝</value>
<value>小明</value>
<value>小红</value>
</array>
</property>
map配置:
<property name="map">
<map>
<entry>
<key><value>username</value></key>
<value>keke</value>
</entry>
<entry>
<key><value>age</value></key>
<value>30</value>
</entry>
</map>
</property>
properties配置
<property name="emails">
<props>
<prop key="kekemail">153398644@qq.com</prop>
<prop key="jonymail">678979789@qq.com</prop>
<prop key="doudoumail">878979@qq.com</prop>
</props>
</property>
事件date配置:
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd" />
</bean>
等价于
SimpleDateFormate dateFormat = new SimpleDateFormate("yyyy-MM-dd");
<bean id="customer" class="com.mkyong.common.Customer">
<property name="date">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2010-01-31" />
</bean>
等价于
date = dateFormat.parse("2010-01-31")
</property>
</bean>
可以做个工具类:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringUtils {
public static Date parseDate(String dateString,String pattern) throws ParseException{
return new SimpleDateFormat(pattern).parse(dateString);
}
}
<property name="ctime">
<bean class="com.tz.ioc.StringUtils" factory-method="parseDate">
<constructor-arg index="0" value="2015-12-12 12:12:10"></constructor-arg>
<constructor-arg index="1" value="yyyy-MM-dd HH:mm:ss"></constructor-arg>
</bean>
</property>