zoukankan      html  css  js  c++  java
  • springBean参数注入的几个方法

    1、普通方式注入

    applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
     5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <bean id="person" class="com.xiaostudy.service.PersonImple">
     8         <property name="id" value="2018"></property>
     9         <property name="name" value="xiaostudy"></property>
    10         <property name="age" value="23"></property>
    11     </bean>
    12 </beans>

    PersonImple.java

     1 package com.xiaostudy.service;
     2 
     3 public class PersonImple implements Person {
     4 
     5     private Integer id;
     6     private String name;
     7     private Integer age;
     8 
     9     public Integer getId() {
    10         return id;
    11     }
    12 
    13     public void setId(Integer id) {
    14         this.id = id;
    15     }
    16 
    17     public String getName() {
    18         return name;
    19     }
    20 
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24 
    25     public Integer getAge() {
    26         return age;
    27     }
    28 
    29     public void setAge(Integer age) {
    30         this.age = age;
    31     }
    32 
    33     @Override
    34     public String toString() {
    35         return "PersonImple [id=" + id + ", name=" + name + ", age=" + age + "]";
    36     }
    37 
    38 }

    2、通过构造方法的注入,name为参数名称

    applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
     5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <bean id="person" class="com.xiaostudy.service.PersonImple">
     8         <constructor-arg name="id" value="2018"></constructor-arg>
     9         <constructor-arg name="name" value="xiaostudy"></constructor-arg>
    10         <constructor-arg name="age" value="23"></constructor-arg>
    11     </bean>
    12 </beans>

    PersonImple.java

     1 package com.xiaostudy.service;
     2 
     3 public class PersonImple implements Person {
     4 
     5     private Integer id;
     6     private String name;
     7     private Integer age;
     8 
     9     public PersonImple(Integer id, String name, Integer age) {
    10         this.id = id;
    11         this.name = name;
    12         this.age = age;
    13     }
    14 
    15     public Integer getId() {
    16         return id;
    17     }
    18 
    19     public void setId(Integer id) {
    20         this.id = id;
    21     }
    22 
    23     public String getName() {
    24         return name;
    25     }
    26 
    27     public void setName(String name) {
    28         this.name = name;
    29     }
    30 
    31     public Integer getAge() {
    32         return age;
    33     }
    34 
    35     public void setAge(Integer age) {
    36         this.age = age;
    37     }
    38 
    39     @Override
    40     public String toString() {
    41         return "PersonImple [id=" + id + ", name=" + name + ", age=" + age + "]";
    42     }
    43 
    44 }

    3、也是通过构造方法注入,index为参数索引的位置

    applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
     5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <bean id="person" class="com.xiaostudy.service.PersonImple">
     8         <constructor-arg index="0" value="2018"></constructor-arg>
     9         <constructor-arg index="1" value="xiaostudy"></constructor-arg>
    10         <constructor-arg index="2" value="23"></constructor-arg>
    11     </bean>
    12 </beans>

    4、也是通过构造方法注入,type是参数的数据类型

    applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans 
     5                               http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <bean id="person" class="com.xiaostudy.service.PersonImple">
     8         <constructor-arg type="java.lang.Integer" value="2018" index="0"></constructor-arg>
     9         <constructor-arg type="java.lang.String" value="xiaostudy" index="1"></constructor-arg>
    10         <constructor-arg type="java.lang.Integer" value="23" index="2"></constructor-arg>
    11     </bean>
    12 </beans>

  • 相关阅读:
    document.getElementById("mytxt").style.left=""style.left在IE的FF中注意
    asp.net 用户控件中 使用相对路径的解决方法 图片路径问题(用户控件、图片路径) ,ResolveUrl
    探索 Block (一) (手把手讲解Block 底层实现原理)
    iOS 多线程开发 (概念与API简介)
    iOS 性能小点
    iOS runtime (二)(runtime学习之AutoCoding源码分析)
    探索 NSRunLoop (二)(NSRunLoop 自己动手实现SimpleRunLoop)
    iOS NSNotificationCenter (自己实现一个通知中心XMCNotificationCenter)
    iOS runtime (三)(runtime学习之YYModel源码分析)
    iOS runtime(一)(runtime 分析理解)
  • 原文地址:https://www.cnblogs.com/xiaostudy/p/9533818.html
Copyright © 2011-2022 走看看