【java 4】---根据名称或类型 完成自动装配

本文介绍了Java Spring框架中如何根据名称和类型进行自动装配,通过实例代码展示了配置过程,并给出了单元测试结果。强调了根据类型自动匹配的便利性和安全性,建议在开发中使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



【java 4】---根据名称或类型完成自动装配






前言:

       根据名称或类型配置有什么好处呢?


       这样可以减少很多的配置量,特别是代码量?



根据名称那到底怎么配置呢?


 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">



中添加:default-autowire="byName" 根据名字自动匹配:
             
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
 default-autowire="byName">




实践完整代码:

             




Bean2.java:

package com.bjpowernode.spring;
public class Bean2 {
    private Bean3 bean3;
    private Bean4 bean4;
    private Bean5 bean5;
    public Bean3 getBean3() {
        return bean3;
    }
    public void setBean3(Bean3 bean3) {
        this.bean3 = bean3;
    }
    public Bean4 getBean4() {
        return bean4;
    }
    public void setBean4(Bean4 bean4) {
        this.bean4 = bean4;
    }
    public Bean5 getBean5() {
        return bean5;
    }
    public void setBean5(Bean5 bean5) {
        this.bean5 = bean5;
    }
    
}


Bean3.java:

package com.bjpowernode.spring;
public class Bean3 {
    private int id;
    private String name;
    private String sex;
    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;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
}


Bean4.java:

package com.bjpowernode.spring;
public class Bean4 {
    private int id;
    private String name;
    private String sex;
    private  int age;
    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;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}




Bean5.java:

package com.bjpowernode.spring;
public class Bean5 {
    private String password;
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    
}




AutoWireTest.java:

package test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjpowernode.spring.Bean2;
import junit.framework.TestCase;
public class AutoWireTest extends TestCase {
    private BeanFactory factory;
    
   @Override
    protected void setUp() throws Exception { 
       //读取的方式一:
      //String[] configLocation=new String[]{ "applicationContext-beans.xml","applicationContext-editor.xml"};
      //factory=new ClassPathXmlApplicationContext(configLocation);
      
       // factory=new ClassPathXmlApplicationContext("applicationContext.xml");
       
       //读取方式二:
       factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml");
       
    }
       
    
   
   /*protected void tearDown() throws Exception {
        // TODO Auto-generated method stub
        
    }*/
   
    /*public void testInjection1()
    {
       //简单的new 一下,属性是不会注入的,必须从容器中拿出来
       //Bean bean=new Bean();普通的new 不会被注入
        
        
         Bean1 bean1=(Bean1)factory.getBean("bean1");
        System.out.println("bean.strValue= " + bean1.getStrValue());
        System.out.println("bean.intValue= " + bean1.getIntValue());
        System.out.println("bean.listValue= " + bean1.getListValue());
        System.out.println("bean.setValue= " + bean1.getSetValue());
        System.out.println("bean.arrayValue= " + bean1.getArrayValue());
        System.out.println("bean.mapValue= " + bean1.getMapValue());
        System.out.println("bean.dateValue= " + bean1.getDateValue());        
    }
    */
   
   
    public void testInjection1()
    {
       //简单的new 一下,属性是不会注入的,必须从容器中拿出来
       //Bean bean=new Bean();普通的new 不会被注入
                
         Bean2 bean2=(Bean2)factory.getBean("bean2");
        System.out.println("bean2.bean3.id= " + bean2.getBean3().getId());
        System.out.println("bean2.bean3.name= " + bean2.getBean3().getName());
        System.out.println("bean2.bean3.sex= " + bean2.getBean3().getSex());
        System.out.println("bean2.bean4.id= " + bean2.getBean4().getId());
        System.out.println("bean2.bean4.name= " + bean2.getBean4().getName());
        System.out.println("bean2.bean4.sex= " + bean2.getBean4().getSex());
        System.out.println("bean2.bean4.age= " + bean2.getBean4().getAge());
        System.out.println("bean2.bean5.password= " + bean2.getBean5().getPassword());
        
    }
    
}



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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
 default-autowire="byName">


<!-- 自动配置,自动找,根据名字相同 default-autowire="byName" -->




<!-- 
<bean id="bean2" class="com.bjpowernode.spring.Bean2">
      <property name="bean3" ref="bean3"/>
      <property name="bean4" ref="bean4"/>
       <property name="bean5" ref="bean5"/>
 </bean>-->




<bean id="bean2" class="com.bjpowernode.spring.Bean2"/>
<bean id="bean3" class="com.bjpowernode.spring.Bean3">
    <property name="id" value="100"/>
    <property name="name" value="Daniel"/>
    <property name="sex" value="male"/>
</bean> 
<bean id="bean4" class="com.bjpowernode.spring.Bean4">
    <property name="id" value="100"/>
    <property name="name" value="Daniel"/>
    <property name="sex" value="male"/>
    <property name="age" value="102"/>
</bean>
<bean id="bean5" class="com.bjpowernode.spring.Bean5">
    <property name="password" value="1"/>
</bean>
</beans>




单元测试运行结果:成功如下


  
 




根据类型自动匹配:

      上面我们介绍了更加名字自动匹配,那么怎么根据类型自动匹配呢?


      原理是一样,就是添加一句话罢了,详细如下:  

 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
>


 
  在这句中添加:        
 default-autowire="byType" 

    

得到如下:


http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
 default-autowire="byType">



小结:

       开发阶段,为了提高速度和效率,在配置文件中是可以更加名字或类型自动匹配。好

处是开发快,但是后面是强烈建议不上具体的注释,为了方便了解,对于的配置和注

释,能更好的调错和维护。


    对比: 相对于根据名字自动配置来说,还是相对类型配置是更方便,更安全的。



    
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值