欢迎使用CSDN-markdown编辑器

  • Post author:
  • Post category:其他


Spring 学习(一)—–装配Bean

装配Bean

刚学习spring,一开始被spring各种bean注入方式搞得晕头转向,现在花时间进行梳理。spring的bean注入主要有以下三种方式。

装配Bean.png

一 自动装配Bean

1) 用@componet+@Autowired+Spring 配置类名(指定 Spring 配置信息的来源)

Printer.class


package xuejun.printer;
public interface Printer {
    public void print();
}

JSONPrinter.class

@Componet表示JSONPrinter类可以进行组装


package xuejun.printer;
import org.springframework.stereotype.Component;

@Component("jSONPrinter")
public class JSONPrinter implements Printer {
    public void print(){
        System.out.println("Json Output Printer");
    }
}

OutPutHelper.class

@Componet表示OutPutHelper类可以进行组装,@Autowired声明要装载进来的类JSONPrinter


package xuejun.printer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("outPutHelper")
public class OutPutHelper {
    Printer printer;
    @Autowired
    public OutPutHelper(Printer SONPrinter){
        this.printer = SONPrinter;
    }
    public void print(){
        printer.print();
    }
}

OutPutHelperConfig.class

声明spring配置类


package xuejun.printer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class OutPutHelperConfig {
      Printer printer;
      @Autowired
      public void OutPutHelper(Printer SONPrinter){
            this.printer = SONPrinter;
      }
      public void print(){
            printer.print();
      }
}

测试类


package xuejun.output;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xuejun.printer.OutPutHelper;
import xuejun.printer.OutPutHelperConfig;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=OutPutHelperConfig.class)
public class Output {
    @Resource OutPutHelper outPutHelper;
    @Test
    public  void test( )
    {
        outPutHelper.print();
    }
}


输出“Json Output Printer”

2) 用@componet+@Autowired+xml配置(指定 Spring 配置信息的来源)

第一步,将OutPutHelperConfig.class去掉;

第二步,新增xml配置;

第三步,更改测试类。

spring01.png


在/resource/ApplicationContext/ 路径下新增spring-component.xml配置

测试类


package xuejun.output;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xuejun.printer.OutPutHelper;
import xuejun.printer.OutPutHelperConfig;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/ApplicationContext/spring-component.xml")
public class Output {
    @Resource OutPutHelper outPutHelper;
    @Test
    public  void test( )
        outPutHelper.print();     
    }
}


输出“Json Output Printer”

### 二 通过Java代码装配Bean ####用@bean+javaConfig,@Bean注解会告诉spring这个方法将返回一个对象,该对象注册为spring应用上下文中的bean。 #####1)OutPutHelper.class,

支持构造函数


把所有类的@componet和@Autowired都去掉


package xuejun.printer
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class OutPutHelper {
    Printer printer;
    private JSONPrinter outPutHelper;
    public OutPutHelper(Printer SONPrinter){
        this.printer = SONPrinter;
    }
    public void print(){
        printer.print();
    }
}

OutPutHelperConfig.class


package xuejun.printer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OutPutHelperConfig {
      @Bean
      public JSONPrinter JSONPrinter(){
           return new JSONPrinter();
      }
      @Bean
      public OutPutHelper OutPutHelper(Printer jSONPrinter){
            return new OutPutHelper(jSONPrinter);
      }
}

测试类


package xuejun.output;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xuejun.printer.OutPutHelper;
import xuejun.printer.OutPutHelperConfig;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = OutPutHelperConfig.class)
public class Output {
    @Resource OutPutHelper outPutHelper;
    @Test
    public  void test( )
        outPutHelper.print();     
    }
}


输出“Json Output Printer”

三 通过xml装配Bean


把所有类的@componet和@Autowired都去掉

1)OutPutHelper.class,

支持setter与getter


package xuejun.printer
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class OutPutHelper {
    Printer printer;
    private JSONPrinter outPutHelper;
    public void setOutPutHelper(Printer SONPrinter){
        this.printer = SONPrinter;
    }
    public void print(){
        printer.print();
    }
}


在/resource/ApplicationContext/ 路径下新增xml配置

spring02.png

测试类


package xuejun.output;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xuejun.printer.OutPutHelper;
import xuejun.printer.OutPutHelperConfig;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/ApplicationContext/Spring-common.xml")
public class Output {
    @Resource OutPutHelper outPutHelper;
    @Test
    public  void test( )
        outPutHelper.print();     
    }
}


输出“Json Output Printer”


把所有类的@componet和@Autowired都去掉

2)OutPutHelper.class,

支持构造函数


package xuejun.printer
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class OutPutHelper {
    Printer printer;
    private JSONPrinter outPutHelper;
    public OutPutHelper(Printer SONPrinter){
        this.printer = SONPrinter;
    }
    public void print(){
        printer.print();
    }
}


在/resource/ApplicationContext/ 路径下新增xml配置,不同的是“property”换成了“constructor-arg”

spring03.png

测试类


package xuejun.output;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xuejun.printer.OutPutHelper;
import xuejun.printer.OutPutHelperConfig;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/ApplicationContext/Spring-common.xml")
public class Output {
    @Resource OutPutHelper outPutHelper;
    @Test
    public  void test( )
        outPutHelper.print();     
    }
}


输出“Json Output Printer”



版权声明:本文为fpfpfpfpf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。