Spring自动装配的学习

Spring自动装配的学习
在xml配置文件中, autowire有6种类型,可以在<bean/>元素中使用 autowire属性指定:
模式                        说明   
 no                       不使用自动装配,必须通过ref元素指定依赖,默认设置。   
byName                    根据属性名自动装配。此选项将检查容器并根据名字查找与                   
                          属性完全一致的bean,并将其与属性自动装配。   
byType                    如果容器中存在一个与指定属性类型相同的bean,那么将与   
                          该属性自动装配;如果存在多个该类型bean,那么抛出异   
                          常,并指出不能使用byType方式进行自动装配;如果没有找   
                          到相匹配的bean,则什么事都不发生,也可以通过设置   
                          dependency
- check = " objects " 让Spring抛出异常。   
constructor               与byType方式类似,不同之处在于它应用于构造器参数。如   
                          果容器中没有找到与构造器参数类型一致的bean,那么抛出   
                          异常。   
autodetect                通过bean类的自省机制(introspection)来决定是使用   
                          constructor还是byType方式进行自动装配。如果发现默认的   
                          构造器,那么将使用byType方式。  
default
看代码:
Computer.java
package  com.redoffice;

public   class  Computer  {
    
    
private Host host;
    
private Display display;
    
    
public Computer(){
        
    }

    
    
public Computer(Host host, Display display) {
        
super();
        
this.host = host;
        
this.display = display;
    }


    
public void run(){
        System.out.println();
        System.out.print(
"   "+host.run()+",");
        System.out.println(display.run());
    }


    
public Host getHost() {
        
return host;
    }


    
public void setHost(Host host) {
        
this.host = host;
    }


    
public Display getDisplay() {
        
return display;
    }


    
public void setDisplay(Display display) {
        
this.display = display;
    }

}


Host.java
package  com.redoffice;

public   class  Host  {
    
public String run() {
        
return "我是主机,正在运行!";
    }

}

Display.java
package  com.redoffice;

public   class  Display  {
    
public String run() {
        
return "我是显示器,正在运行!";
    }

}

TestMain.java
package  com.redoffice;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
import  org.apache.log4j.PropertyConfigurator;
import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

public   class  TestMain  {
    
private static Log log = LogFactory.getLog(TestMain.class);
    
    
public static void main(String[] args) {
        PropertyConfigurator.configure(
"bin/log4j.properties");
        log.debug(
"entering main method");
        ApplicationContext ac 
= new ClassPathXmlApplicationContext(
                
"applicationContext.xml");
        
//byName
        Computer computer1 = (Computer)ac.getBean("computer1");
        System.out.println(
"autowire=\"byName\":");
        computer1.run();
        
        
//byType
        Computer computer2 = (Computer)ac.getBean("computer2");
        System.out.println(
"autowire=\"byType\":");
        computer2.run();
        
        
//default
        Computer computer3 = (Computer)ac.getBean("computer3");        
        System.out.println(
"autowire=\"default\":");
        computer3.run();
        
        
//autodetect
        Computer computer4 = (Computer)ac.getBean("computer4");        
        System.out.println(
"autowire=\"autodetect\":");
        computer4.run();
        
        
//constructor
        Computer computer5 = (Computer)ac.getBean("computer5");        
        System.out.println(
"autowire=\"constructor\":");
        computer5.run();
        
        
//no
        Computer computer6 = (Computer)ac.getBean("computer6");        
        System.out.println(
"autowire=\"no\":");
        computer6.run();
        log.debug(
"leaving main method");
    }

}


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 "
    xsi:schemaLocation
= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "
    
default - autowire = " autodetect " >
    
< bean id = " host "  name = " host "   class = " com.redoffice.Host " ></ bean >
    
< bean id = " display "  name = " display "   class = " com.redoffice.Display " ></ bean >
    
    
<!--  autowire有no,constructor,byName,byType, default ,autodetect几种状况  -->
    
< bean id = " computer1 "   class = " com.redoffice.Computer "  autowire = " byName " ></ bean >
    
< bean id = " computer2 "   class = " com.redoffice.Computer "  autowire = " byType " ></ bean >
    
< bean id = " computer3 "   class = " com.redoffice.Computer "  autowire = " default " ></ bean >
    
< bean id = " computer4 "   class = " com.redoffice.Computer "  autowire = " autodetect " ></ bean >
    
<!--  在Computer类中必须有一个无参和有参的构造函数,否则报错  -->
    
< bean id = " computer5 "   class = " com.redoffice.Computer "  autowire = " constructor " >
        
< constructor - arg index = " 0 " >
            
< ref bean = " host " />
        
</ constructor - arg >
        
< constructor - arg index = " 1 " >
            
< ref bean = " display " />
        
</ constructor - arg >
    
</ bean >
    
< bean id = " computer6 "   class = " com.redoffice.Computer "  autowire = " no " >
        
< constructor - arg index = " 0 " >
            
< ref bean = " host " />
        
</ constructor - arg >
        
< constructor - arg index = " 1 " >
            
< ref bean = " display " />
        
</ constructor - arg >
    
</ bean >
    
</ beans >

希望有什么不对的地方多多指教。

你可能感兴趣的:(Spring自动装配的学习)