• spring应用之Spring的依赖注入的方式四)


    * 构造器注入

    * setter注入

    这两种方式一般情况下使用的概率是setter注入>构造器注入。

    下面演示一下这三种方式该怎么去使用!

    首先,setter注入。

    我们先定义一个Bean叫Person

    package pro.jamal.blog.demo5;
    
    /**
     * @author: lyj
     * @Date: 2019/6/4
     */
    public class Person {
        private String name;
        private  int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    使用Spring的xml配置文件,进行setter依赖注入

    <?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.xsd">
    
    
        <bean id="person" class="pro.jamal.blog.demo5.Person">
            <!---通过setter注入-->
            <property name="name" value="yongjar"/>
            <property name="age" value="19"/>
        </bean>
    
    </beans>

    在Bean里,给成员setter方法,让xml的propertiy产生映射, 并赋予值。

    测试

    package pro.jamal.blog.demo5;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author: lyj
     * @Date: 2019/6/4
     */
    public class Client {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context
                     = new ClassPathXmlApplicationContext("pro/jamal/blog/demo5/bean.xml");
            Person person = context.getBean(Person.class);
            System.out.println("名字:"+ person.getName()+",年龄:"+person.getAge());
        }
    }
    

    image

    第二种通过构造器注入

    我们修改Person类,我们给他两个构造器,一个默认无参的构造器,一个两个参数的构造器

    package pro.jamal.blog.demo5;
    
    /**
     * @author: lyj
     * @Date: 2019/6/4
     */
    public class Person {
        private String name;
        private  int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public Person() {
        }
    
    }
    

    修改bean.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
        <bean id="person" class="pro.jamal.blog.demo5.Person">
            <!--构造器注入 index从0开始,按构造器的参数赋值-->
            <constructor-arg index="0" value="jamal"/>
            <constructor-arg index="1" value="30"/>
        </bean>
    
    </beans>

    测试打印

    1559638321(1)

  • 相关阅读:
    asp.net中Session过期设置方法
    SQL server的一道入门面试题背后的思考
    SQL Server 2008中SQL应用之-“阻塞(Blocking)”
    win2003+vs2010下安装asp.net MVC3正式版失败经历
    WinForm下ComboBox设定SelectedValue总结
    SQL Server 2008中的代码安全(四):主密钥
    【译】SQL Server误区30日谈Day12TempDB的文件数和需要和CPU数目保持一致
    西雅图SQL PASS之旅
    【译】SQL Server误区30日谈Day10数据库镜像在故障发生后,马上就能发现
    Ado.net的连接池
  • 原文地址:https://www.cnblogs.com/jamal/p/10981558.html
Copyright © 2020-2023  润新知