zoukankan      html  css  js  c++  java
  • Spring bean注入方式

    版权声明:本文为博主原创文章,如需转载请标注转载地址。

    博客地址:http://www.cnblogs.com/caoyc/p/5619525.html 

    Spring bean提供了3中注入方式:属性注入和构造方法注入

    1、属性注入:

    1 <bean id="dept" class="com.proc.bean.Dept">
    2     <property name="id" value="2"/>
    3     <property name="name" value="信息部"></property>
    4 </bean>

      属性注入方式,要求属性提供呢setXxx方法。上面提供的是普通属性注入,如果要注入对象属性,可以这样

    1 <bean id="user" class="com.proc.bean.User">
    2     <property name="id" value="1" />
    3     <property name="username" value="caoyc"></property>
    4     <property name="dept" ref="dept"></property>
    5 </bean>

      我们看到第三个属性dept,是一个Dept类型的属性,可以通过ref来引用一个已定义的Dept类型的dept对象。

    1 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    2 User user=context.getBean("user", User.class);
    3 System.out.println(user.getUsername());
    4 System.out.println(user.getDept().getName());

      结果我们可以看到

    caoyc
    信息部

      除了可以使用ref来引用外部对象外,我们也可以在user对象内部声明一个Dept对象

     1 <bean id="user" class="com.proc.bean.User">
     2     <property name="id" value="1" />
     3     <property name="username" value="caoyc"></property>
     4     <property name="dept">
     5         <bean class="com.proc.bean.Dept">
     6             <property name="id" value="2"/>
     7             <property name="name" value="信息部"></property>
     8         </bean>
     9     </property>
    10 </bean>

    2、使用构造器注入

      假如,有一个User类

    package com.proc.bean;
    
    public class User {
        
        private int id;
        private String username;
        private int age;
        private double slary;
        
        public User() {    
            
        }
        public User(int id, String username) {
            this.id = id;
            this.username = username;
        }
    
        @Override
        public String toString() {
            return "User [id=" + id + ", username=" + username + ", age=" + age
                    + ", slary=" + slary + "]";
        }
    }

      

     

    <bean id="user" class="com.proc.bean.User">
        <constructor-arg  value="1"/>
        <constructor-arg  value="caoyc"/>
    </bean>

      这里使用的是: public User(int id, String username)构造方式 

      这里使用的是下标方式,这里省略了index属性。index属性从0开始。上面的代码相当于

    1 <bean id="user" class="com.proc.bean.User">
    2     <constructor-arg index="0" value="1"/>
    3     <constructor-arg index="1" value="caoyc"/>
    4 </bean>

      假设有这样的两个构造方式

     1 public User(int id, String username, int age) {
     2     this.id = id;
     3     this.username = username;
     4     this.age = age;
     5 }
     6 public User(int id, String username, double slary) {
     7     this.id = id;
     8     this.username = username;
     9     this.slary = slary;
    10 }

      配置bean

     1 <bean id="user" class="com.proc.bean.User">
     2     <constructor-arg index="0" value="1" />
     3     <constructor-arg index="1" value="caoyc"/>
     4     <constructor-arg index="2" value="18"/>
     5 </bean>
     6 
     7 <bean id="user2" class="com.proc.bean.User">
     8     <constructor-arg index="0" value="1" />
     9     <constructor-arg index="1" value="caoyc"/>
    10     <constructor-arg index="2" value="1800"/>
    11 </bean>

       测试代码

    1 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    2 User user=context.getBean("user",User.class);
    3 System.out.println(user);
    4 
    5 User user2=context.getBean("user2",User.class);
    6 System.out.println(user2);

      输出结果

    1 User [id=1, username=caoyc, age=0, slary=18.0]
    2 User [id=1, username=caoyc, age=0, slary=1800.0]

       这里都是调用的public User(int id, String username, double slary)这个构造函数。那么怎么调用public User(int id, String username, int age)这个构造函数呢?

       方法是,在这里我们需要使用到type属性,来指定参数的具体类型

    1 <bean id="user" class="com.proc.bean.User">
    2     <constructor-arg index="0" value="1" />
    3     <constructor-arg index="1" value="caoyc"/>
    4     <constructor-arg index="2" value="18" type="int"/>
    5 </bean>

      输出结果

    1 User [id=1, username=caoyc, age=18, slary=0.0]
    2 User [id=1, username=caoyc, age=0, slary=1800.0]

    【其它说明】

      1、如果value属性是基本属性直接使用

      2、如果valeu属性是其它类型,需要使用ref引用外部类型或使用内部定义方式

      3、如果value属性中包含了xml特殊字符,需要使用CDATA来。例如:

    1 <constructor-arg index="1">
    2     <value><![CDATA[<caoyc>]]></value>
    3 </constructor-arg>
  • 相关阅读:
    #include<unistd.h>存在linux中,含有系统服务的函数
    Value Categories
    Main function
    Phases of translation
    Memory model
    Unqualified name lookup
    Qualified name lookup
    Name lookup
    Definitions
    Object lifetime
  • 原文地址:https://www.cnblogs.com/caoyc/p/5619525.html
Copyright © 2011-2022 走看看