Spring小白到菜鸟修炼之基本概念(2)

来源:互联网 发布:单片机支持snmp协议 编辑:程序博客网 时间:2024/05/15 09:39

上篇文章讲到Spring可以使用xml文件配置或者注解方式来实现注入。这篇这篇博客打算继续讨论xml配置方式注入依赖的方式。

xml文件配置依赖注入方式有设值注入构造注入两种方式。通过setter方法给Bean注入依赖关系的是设值注入,通过构造函数注入依赖关系的是构造注入。

下面看一看设值注入的简单实例

Axe类

package second;public interface Axe {    public String chop();}

StoneAxe类

package second;public class StoneAxe implements Axe{    @Override    public String chop() {        return "石斧砍柴";    }}

Person 类

package second;public interface Person {    public void useAxe();}

Chinese类

package second;public class Chinese implements Person{    private Axe axe;    public void setAxe(Axe axe) {        this.axe = axe;    }    @Override    public void useAxe() {    System.out.println(axe.chop());    }}

配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="              http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">          <!--             设值注入的优缺点            优点              1 和JavaBean相似,容易理解              2 方便,性能相对较高(需要Bean的时候才去加载对应的Bean)          缺点             注入时期                当通过无参构造函数创建bean实例的时候,然后调用对应的setter方法注入依赖关系           -->    <bean id="chinese" class="second.Chinese">        <property name="axe" ref="stoneAxe" />    </bean>    <bean id="stoneAxe" class="second.StoneAxe" /></beans>

TestDemo2 类

package second;import java.util.Random;import java.util.UUID;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import third.Chinese;import third.SteelAxe;/** * 依赖注入方法  * (1) 设值注入 (2) 构造注入 */public class TestDemo2 {    public static void main(String[] args) {         ApplicationContext context = new ClassPathXmlApplicationContext(         "second.xml");         /**         * 设值注入         * 通过setter方法为Bean注入依赖关系         */         Person p = context.getBean("chinese", Person.class);         p.useAxe();        //输出 石斧砍柴    }}

接着,看看构造注入实例

Axe类

package third;public class SteelAxe implements Axe {    @Override    public String chop() {        return "轮子";    }}

Person类

package third;public interface Person {        public void useAxe();}

Chinese类

package third;public class Chinese implements Person {    private Axe axe;    public Chinese(Axe axe){        this.axe=axe;    }    @Override    public void useAxe() {         System.out.println(axe.chop());    }}

SteelAxe类

package third;public class SteelAxe implements Axe {    @Override    public String chop() {        return "轮子";    }}

TestDemo3类

package third;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import third.Chinese;import third.SteelAxe;public class TestDemo3 {    public static void main(String[] args) {        /**         *  构造注入         *     通过构造器注入依赖关系         */        ApplicationContext context = new ClassPathXmlApplicationContext(                "third.xml");        Person p=(Person) new Chinese(new SteelAxe());        p.useAxe(); // 轮子    }}
这里写代码片

配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="              http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!--         构造注入优缺点       优点            1 在构造器中决定依赖关系的注入顺序       缺点           1 笨重             注入时期                bean实例创建完成就完成依赖关系的注入  -->    <bean id="chinese" class="third.Chinese">    <!--驱动Spring调用Chinese带一个参数的构造器来创建对象  -->        <constructor-arg ref="steelAxe" />        <!--constructor-arg          index="0" 指出参数的位置         type="java.lang.Integer" 指出参数类型         value=""  参数的值          ref="steelAxe"  依赖的bean         /**          *  构造注入常见知识点          */         有时候在配置参数的时候,可以不指出参数的类型,Spring可以根据构造器来进行强制类型转换           public Chinese(String str, int n)           如果写成         <constructor-arg value="str" />         <constructor-arg value="23" />         此时"23"会转换成23,但是如果有一下构造函数           public Chinese(String str, int n)           public Chinese(String str, String str)         此时必须使用type来指出"23"是 int,不然"23"只会被认为是字符串         -->    </bean>    <bean id="steelAxe" class="third.SteelAxe" /></beans>
0 0
原创粉丝点击