zoukankan      html  css  js  c++  java
  • Spring(四)Bean注入方试

    一、构造方法注入

    定义:通过构造函数来完成依赖关系的设定

    优缺点:

    在构造对象的同时,完成依赖关系的建立

    如果关联的对象很多,那和不得不在构造方法上加入过多的参数

    基中有index:如果指定索引从0开始,type用来指定类型

    实体类:

    package com.pb.entity;
    /**
     * 班级类
     * @author Administrator
     *
     */
    public class Grade {
        private int id;            //班级编号
        private String name; //班级名称
        
        
        public Grade() {
            super();
            // TODO Auto-generated constructor stub
        }
        
        public Grade(int id, String name) {
            super();
            this.id = id;
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        
    
    }
    package com.pb.entity;
    /**
     * 学生类
     * @author Administrator
     *
     */
    public class Student {
        private String name;    //学生名称
        private Integer age;   //学生年龄
        private Grade grade;  //所属班级
        
        public Student() {
            super();
        }
        
        
        public Student(String name, Integer age, Grade grade) {
            super();
            this.name = name;
            this.age = age;
            this.grade = grade;
        }
    
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        public Grade getGrade() {
            return grade;
        }
        public void setGrade(Grade grade) {
            this.grade = grade;
        }
        
        
    
    }

    使用构造方法注入

    applicationContext.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-3.1.xsd">
    <!--班级类  -->
    <bean id="grade" class="com.pb.entity.Grade">
    <!-- 使用构造方法注入 -->
    <constructor-arg>
    <value>1001</value>
    </constructor-arg>
    <constructor-arg>
    <value>计算应用一班</value>
    </constructor-arg>
    
    </bean>
    <!--学生类  -->
    <bean id="student" class="com.pb.entity.Student">
    <!-- 使用构造方法注入 -->
    <constructor-arg>
    <value>张三</value>
    </constructor-arg>
    <constructor-arg>
    <value>23</value>
    </constructor-arg>
    <!-- 使用ref注入班级bean -->
    <constructor-arg ref="grade">
    </constructor-arg>
    </bean>
    
    </beans>

    测试类:

    package com.pb.demo;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.pb.entity.Student;
    
    public class Demo1 {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "applicationContext.xml");
            Student stu = context.getBean("student", Student.class);
            System.out.println("学生姓名:" + stu.getName() + "学生年龄:" + stu.getAge()
                    + "学生班级编号:  " + stu.getGrade().getId() + "学生班级名称:  "
                    + stu.getGrade().getName());
    
        }
    
    }

    二、属性注入

    其它不变只更改配置文件

    applicationContext.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-3.1.xsd">
    <!--班级类  -->
    <bean id="grade" class="com.pb.entity.Grade">
    <property name="id">
    <value>1001</value>
    </property>
    <property name="name" value="计算机应用一班"></property>
    </bean>
    <!-- 学生类  -->
    <bean id="student" class="com.pb.entity.Student">
    <property name="name" value="张三" />
    <property name="age" value="18"/>
    <property name="grade" ref="grade"/>
    </bean>
    </beans>

    三、注入NULL和空字符串值

    <value></value>表示空字符串

    <null></null>表示为null值

    <bean id="student" class="com.pb.entity.Student">
    <!-- 姓名注入空字符串 <value></value>表示空字符串 -->
    <property name="name"><value></value></property>
    <!--年龄注入为NULL值  -->
    <property name="age"><null></null></property>
    <property name="grade" ref="grade"/>
    </bean>

    四、使用p 命名空间注入bean

    官方推荐的注入方式

    需要在XML上加入

    xmlns:p="http://www.springframework.org/schema/p"
    <?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-3.1.xsd">
        <!--班级类 使用p命名空间注入 -->
        <bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外语一班">
    
        </bean>
        <!-- 学生类 使用p命名空间注入 -->
        <bean id="student" class="com.pb.entity.Student" p:name="张三"
            p:age="23" p:grade-ref="grade">
    
        </bean>
    </beans>

    效果一目了然

    五、自动装配

    需要使用autowire属性来配置

    可以在每个bean中使用autowire来配置

    也可以在<beans>中使用autowire全局配置表示这个beans下的都使用自动装配,

    缺点:不清晰,有问题比较难以查找

    autowire:

    no(默认值):不进行自动装配,必须显示指定依赖对象

    byName: 根据属性名自动装配。自动查找与属性名相同的id,如果找到,则自动注入,否则什么都不做

    byType:根据属性的类型自动装配,Spring自动查找与属性类型相同的Bean,如果刚好找到唯一的那个,则自动注入,如果找到多个与属性类型相同的Bean,则抛出异常,如果没有找到就什么都不做。

    constructor:和byType类似,不过它针对构造方法,如果找到一个Bean和构造方法的参数类型相匹配,则通过构造注入该依赖对象,如果找不到,就抛出异常

    <?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-3.1.xsd">
        <!--班级类 使用p命名空间注入 -->
        <bean id="grade" class="com.pb.entity.Grade" p:id="1001" p:name="外语一班" >
    
        </bean>
        <!-- 学生类 使用p命名空间注入 -->
        <bean id="student" class="com.pb.entity.Student" p:name="张三"
            p:age="23" autowire="byName">
    
        </bean>
    </beans>

    自动装配使得配置文件可以非常简洁,但同时也造成组件之间的依赖关系不明确,容易引发一些潜在的错误,谨慎使用

  • 相关阅读:
    delphi内存管理
    Dbgrid导出为word
    indy传输文件
    TMethod
    http状态码详解
    如何严格设置php中session过期时间
    http和https的优缺点,区别与工作原理
    session的生命周期
    Mysql分库分表方案,如何分,怎样分?
    【观点】程序员应知——循序渐进 狼人:
  • 原文地址:https://www.cnblogs.com/liunanjava/p/4401236.html
Copyright © 2011-2022 走看看