Spring-IoC的代码实现与总结
新人首次发表,内容原创,求关注,求点赞,在屏幕这边给您贵了!!!!!!那块有纰漏请您指正,小南立刻改正。后续更新订单查询(利用mybatis的联查)和AOP相关的代码实现,关注不迷路幺。
一、IoC是什么?
Inversio of Control 控制反转,是Spring框架特有的一种方法,有效的降低了类与类之间的耦合性,是被动创建对象的一种方式,利用了单例模式中的饿汉式。在建立相应的xml文件时,考虑依赖注入(DI),本次代码的实现使用的是set注入和constructor注入
二、代码实现
1.实体类的引入
代码如下:
public class students {
private String name;
private int uid;
private boolean sex;
private double score;
}
2.xml的编写(重点)
使用set注入设置bean对象时,实体类中必须存在相应的set方法(对于xml而言,每一个实体类都将是一个bean对象):
<!--scope中有singleton.prototype两个参数这两个参数都是什么意思
singleton-单例模式-饿汉式
prototype-多例模式
-->
<bean id="s1" class="com.flex.pojo.students" scope="prototype">
<property name="score" value="99"/>
<property name="sex" value="true"/>
<property name="name" value="sang"/>
<property name="uid" value="1"/>
</bean>
<bean id="s2" class="com.flex.pojo.students">
<property name="name" value="sang"/>
<property name="uid" value="1"/>
<property name="score" value="99"/>
<property name="sex" value="true"/>
</bean>
使用Constructor注入设置bean对象时,实体类中必须存在相应的参数构造器,且该注入方法较为灵活,强烈推荐使用name+value这种方法:
<!--bean+constructor进行设置
有四种设计方式:1.name+value
2.index+value
3.直接上val
4.数据类型加上相应的值
-->
<bean id="s3" class="com.flex.pojo.students">
<constructor-arg name="name" value="sang"/>
<constructor-arg name="score" value="99"/>
<constructor-arg name="uid" value="1"/>
<constructor-arg name="sex" value="true"/>
</bean>
<bean id="s4" class="com.flex.pojo.students">
<constructor-arg value="sang"/>
<constructor-arg value="1"/>
<constructor-arg value="true"/>
<constructor-arg value="99"/>
</bean>
<bean id="s5" class="com.flex.pojo.students">
<constructor-arg index="0" value="sang"/>
<constructor-arg index="1" value="1"/>
<constructor-arg index="2" value="true"/>
<constructor-arg index="3" value="99"/>
</bean>
<bean id="s6" class="com.flex.pojo.students">
<constructor-arg type="java.lang.String" value="sang"/>
<constructor-arg type="int" value="1"/>
<constructor-arg type="boolean" value="true"/>
<constructor-arg type="double" value="99"/>
</bean>
Constructor的bean对象构建极为灵活,比如
<bean id="s2" class="com.flex.pojo.Students" p:name="sang" p:uid="1" p:sex="true" p:score="99" p:birthplace-ref="b" p:place-ref="p"/>
只需在xml中引入相应地头文件即可
总结
提示:
1.在maven基础上建立工程,会很大程度上简化工程实现(将原来的导入外部包转换为添加依赖),这里分享一下我的pom.xml中的依赖导入
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
2.其实IoC是一个很有意思的中间工厂,对相应的对象进行构建时,也可以通过ref引入同xml下的相应实体类,这个是对mysql中多表进行联查的一个思路。
3.在bean的构建时,同样也可以引入set,map,array,properties,list这些属性-代码实现如下
<bean id="c" class="com.flex.pojp.Collection">
<property name="args">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<property name="list">
<list>
<value>心</value>
<value>极</value>
<value>净</value>
<ref bean="t"/>
</list>
</property>
<property name="set">
<set>
<value>清</value>
<value>明</value>
<value>明</value>
<ref bean="t"/>
</set>
</property>
<property name="map">
<map>
<entry key="s" value="莎莉"/>
<entry key="m" value="漫步"/>
<entry key="h" value="花园"/>
</map>
</property>
<property name="pro">
<props>
<prop key="user">root</prop>
<prop key="pass">666</prop>
<prop key="sourcespath">null</prop>
</props>
</property>
</bean>
最后,基本就这些了,且这些代码都是经过Test过的,可放心食用!