spring依赖注入方法

本文介绍如何使用Spring框架进行依赖注入,包括通过构造方法和get/set方法两种方式,并提供了详细的配置文件示例。

本例都是在局部配置文件中配置的,也可以在applicationContext.xml文件中直接进行配置,为了说明方便,这里用的局部配置方法。

(1)通过构造方法

创建一个类Cat

package cn.java.di;
 
public class Cat {
     private String name;
     private Integer age;
     private Float weight;
     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 Float getWeight() {
         return weight;
     }
     public void setWeight(Float weight) {
         this .weight = weight;
     }
     public Cat() {
         System.out.println( "无参构造方法" );
     }
     public Cat(String name, Integer age, Float weight) {
         super ();
         this .name = name;
         this .age = age;
         this .weight = weight;
         System.out.println( "有参构造方法" );
     }
     @Override
     public String toString() {
         return "Cat [name=" + name + ", age=" + age + ", weight=" + weight + "]" ;
     }
     
}


创建一个局部配置文件:

<!--?xml version= "1.0" encoding= "UTF-8" ?-->
    xsi:schemalocation="https://www.springframework.org/schema/beans
    https: //www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class = "cn.java.di.Cat" id= "cat" >
        <constructor-arg index= "0" type= "java.lang.String" value= "tom" ></constructor-arg>
        <constructor-arg index= "1" type= "java.lang.Integer" value= "10" ></constructor-arg>
        <constructor-arg index= "2" type= "java.lang.Float" value= "2.5" ></constructor-arg>
    </bean>

</beans>

然后在applicationContext.xml文件中将其关联:

<!--?xml version= "1.0" encoding= "UTF-8" ?-->
    xsi:schemalocation="https://www.springframework.org/schema/beans
     https: //www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           
      <!-- bean:将一个类的对象创建过程交给spring容器 class :指定类的具体路径 id:唯一标识符
       scope:用来控制spring容器产生对象的方式,可以为单例也可以为多例。
            常用的值为singleton(单例),prototype(多例),默认的是单例。    
       init-method:表示初始化方法,只写初始化方法的名字,不用加上();
      -->
       < import resource= "cn/java/di/Cat.xml" ></ import >
</beans>

创建一个测试类:

package cn.java.ioc1;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import cn.java.di.Cat;
import cn.java.initAndDistroy.Ji;
import cn.java.lazy.Duck;
import cn.java.singleton2.Dog;
 
public class Window {
 
     public static void main(String[] args) {
         //启动框架(context代表spring容器)
         ApplicationContext context= new ClassPathXmlApplicationContext( "applicationContext.xml" ); //只写这句也可以调用YellowMouseWolf类中的无参构造方法
          System.out.println( "-----------------------" );
         //获取spring容器中创建的对象(通过id值获取)
         Cat cat=(Cat)context.getBean( "cat" );
         
     }
 
}

(2)通过get/set方法赋值

这时只需要修改:

<!--?xml version= "1.0" encoding= "UTF-8" ?-->
    xsi:schemalocation="https://www.springframework.org/schema/beans
        https: //www.springframework.org/schema/beans/spring-beans-2.5.xsd">              
         
             <bean class = "cn.java.di.Cat" id= "cat" >
                  <property name= "name" value= "jack" ></property>
                  <property name= "age" value= "20" ></property>
                  <property name= "weight" value= "5.2" ></property>                
             </bean>    
                      
  </beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值