spring

    xiaoxiao2021-04-19  84

    spring的核心思想之一:控制反转也就是依赖注入。在spring的世界里,所有的java对象都是一个bean。程序中类和类之间的互相调用,不再是以往的new的创建。而是依靠spring容器通过反射读取xml配置文件中的一个个的bean来生成一个个的具体的实现类,供程序使用。同时将编程方式提升至面向接口编程。在一定程度上降低了程序的耦合。同时面向接口对后期功能的扩展以及修改降低了难度。只需要修改或增加实现类即可。

    下面展示了spring注入bean的两种方式:设置注入、构造注入。

    (1)设置注入。 接口Person.java

    package com.demo.interfaces; public interface Person { public void drive(); }

    实现类 Chinese.java

    package com.demo.impls; import com.demo.interfaces.Person; import com.demo.interfaces.Shop; public class Chinese implements Person{ private Shop shop; public void setShop(Shop shop) { this.shop = shop; } @Override public void drive() { System.out.println(shop.work()); } }

    接口Shop.java package com.demo.interfaces;

    public interface Shop { public String work(); }

    实现类SShop.java

    package com.demo.impls;

    import com.demo.interfaces.Shop;

    public class Sshop implements Shop{

    @Override public String work() { return "卖汽车"; }

    }

    spring 配置文件beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="chinese" class="com.demo.impls.Chinese"> <property name="shop" ref="sshop"></property> </bean> <bean id="sshop" class="com.demo.impls.Sshop"></bean> </beans> 测试类Test.java public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person p = ctx.getBean("chinese",Person.class); p.drive(); }

    最后输出结果:卖汽车。

    这是第一种设值注入方式,也就是根据

    <bean id="chinese" class="com.demo.impls.Chinese"> <property name="shop" ref="sshop"></property> </bean>

    中的name来指定调用Chinese实现类中的哪个set方法,ref指定set方法的传入参数。

    (2)构造注入

    接口Person.java

    package com.demo.interfaces; public interface Person { public void drive(); }

    实现类 Chinese.java

    package com.demo.impls; import com.demo.interfaces.Person; import com.demo.interfaces.Shop; public class Chinese implements Person{ private Shop shop; public Chinese(Shop shop){ this.shop = shop; } @Override public void drive() { System.out.println(shop.work()); } }

    接口Shop.java package com.demo.interfaces;

    public interface Shop { public String work(); }

    实现类SShop.java

    package com.demo.impls;

    import com.demo.interfaces.Shop;

    public class Sshop implements Shop{

    @Override public String work() { return "卖汽车"; }

    }

    spring 配置文件beans.xml

    <?xml version="1.0" encoding="UTF-8"?> <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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="chinese" class="com.demo.impls.Chinese"> <constructor-arg ref="sshop"></constructor-arg> </bean> <bean id="sshop" class="com.demo.impls.Sshop"></bean> </beans> 测试类Test.java public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person p = ctx.getBean("chinese",Person.class); p.drive(); }

    最后输出结果:卖汽车。

    转载请注明原文地址: https://ju.6miu.com/read-675807.html

    最新回复(0)