Spring框架中如何使用IOC实现装配Bean

今天就跟大家聊聊有关Spring框架中如何使用IOC实现装配Bean,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联于2013年开始,是专业互联网技术服务公司,拥有项目成都网站制作、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元陕州做网站,已为上家服务,为陕州各地企业和个人服务,联系电话:028-86922220

IOC装配Bean

(1)Spring框架Bean实例化的方式提供了三种方式实例化Bean

  1. 构造方法实例化(默认无参数,用的最多)
  2. 静态工厂实例化
  3. 实例工厂实例化

下面先写这三种方法的applicationContext.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>

    
    
   
   
   
   
   
   
   
  

Bean1类

public class Bean1 {
  
  //必须提供无参的构造函数 系统有默认无参的构造函数
}

Bean2类

public class Bean2 {
  private static Bean2 Bean2 = new Bean2();

  private Bean2() {
  }

  public static Bean2 createInstance() {
    return Bean2;
  }
}

Bean3类

public class Bean3 {

}

Bean3Factory类

public class Bean3Factory {
  
  private Bean3Factory(){
    
  }
   
  public Bean3 getInstance(){
    return new Bean3();
  }
}

测试类InstanceDemo

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InstanceDemo {
  
  //实例化工厂方法
  @Test
  public void demo3(){
    //加载配置文件 创建工厂
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
    Bean3 bean3 =(Bean3) applicationContext.getBean("bean3");
    System.out.println(bean3);
    
  }
  
  //静态工厂方法
  @Test
  public void demo2(){
    //加载配置文件 创建工厂
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
    Bean2 bean2 =(Bean2) applicationContext.getBean("bean2");
    System.out.println(bean2);
    
  }
  //构造方法得到bean对象
  @Test
  public void demo1(){
    //加载配置文件 创建工厂
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
    
    Bean1 bean1 =(Bean1) applicationContext.getBean("bean1");
    System.out.println(bean1);
    
  }
}
/*
 * 这三个都得到类似于com.study.spring.b_instance.Bean1@7229c204 的内存地址
 */

 (2).Bean的其他配置:

一般情况下,装配一个Bean时,通过指定一个id属性作为Bean的名称

id 属性在IoC容器中必须是唯一的

id 的命名要满足XML对ID属性命名规范 必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号

如果Bean的名称中含有特殊字符,就需要使用name属性 例如:

因为name属性可以相同,所以后出现Bean会覆盖之前出现的同名的Bean

id和name的区别:

id遵守XML约束的id的约束.id约束保证这个属性的值是唯一的,而且必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号

name没有这些要求

如果bean标签上没有配置id,那么name可以作为id.

Bean的scope属性

   
   
   

 * singleton :单例的.(默认的值.)

 * prototype :多例的.

* request :web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();

* session :web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();

* globalSession :一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;

3.Bean属性的依赖注入

前面已经知道如何获得对象,那我们接下来要知道如果给对象对象的属性赋值。

 Spring框架中如何使用IOC实现装配Bean

下面通过举例说明: 

Car 类

public class Car {

  private String name;

  private double price;

  public Car(String name, double price) {
    super();
    this.name = name;
    this.price = price;
  }

  @Override
  public String toString() {
    return "Car [name=" + name + ", price=" + price + "]";
  }
}

Car2类

public class Car2 {
  private String name;

  private double price;

  public void setName(String name) {
    this.name = name;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  @Override
  public String toString() {
    return "Car2 [name=" + name + ", price=" + price + "]";
  }

}

CarInfo类

public class CarInfo {
  
  public String getName(){
    return "哈弗H6";
  }
  
  public double caculatePrice(){
    return 110000;
  }
}

CollectionBean类

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionBean {
  private String name;

  private Integer age;

  private List hobbies;

  private Set numbers;

  private Map map;

  private Properties properties;

  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 List getHobbies() {
    return hobbies;
  }

  public void setHobbies(List hobbies) {
    this.hobbies = hobbies;
  }

  public Set getNumbers() {
    return numbers;
  }

  public void setNumbers(Set numbers) {
    this.numbers = numbers;
  }

  public Map getMap() {
    return map;
  }

  public void setMap(Map map) {
    this.map = map;
  }

  public Properties getProperties() {
    return properties;
  }

  public void setProperties(Properties properties) {
    this.properties = properties;
  }

  @Override
  public String toString() {
    return "CollectionBean [name=" + name + ", age=" + age + ", hobbies=" + hobbies + ", numbers=" + numbers
        + ", map=" + map + ", properties=" + properties + "]";
  }
  
}

Employee类

public class Employee {

  private String name;

  private Car2 car2;

  public void setName(String name) {
    this.name = name;
  }

  public void setCar2(Car2 car2) {
    this.car2 = car2;
  }

  @Override
  public String toString() {
    return "Employee [name=" + name + ", car2=" + car2 + "]";
  }

}

TestDi测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestDi {
  
  @Test
  public void demo6() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");

    System.out.println(collectionBean);
  }
  
  
  @Test
  public void demo5() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    Car2 car2 = (Car2) applicationContext.getBean("car2_2");

    System.out.println(car2);
  }
  
  
  @Test
  public void demo4() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    Employee e = (Employee) applicationContext.getBean("employee2");

    System.out.println(e);
  }
  
  @Test
  public void demo3() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    Employee e = (Employee) applicationContext.getBean("employee");

    System.out.println(e);
  }

  @Test
  public void demo2() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    Car2 car2 = (Car2) applicationContext.getBean("car2");

    System.out.println(car2);
  }

  @Test
  public void demo1() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    Car car = (Car) applicationContext.getBean("car");

    System.out.println(car);
  }
}

上面这几个类都不是最主要的,我们主要是来看配置文件怎么写,这才是最关键的:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

  
  
    
   
    
      
      
       
       
       
       
       
    
    
    
   
     
     
   
   
   
     
     
   
   
   
    
   
   
   
    
   
    
      
      
    
    
     
    
      
      
      
      
       
         
           吃饭
           睡觉
           敲代码
         
       
       
       
       
         
           10
           20
           30
           40
           50
         
       
       
       
         
           
           
           
         
       
       
       
       
         
           杭州归谷
           200
         
       
    
    
    
      
      
    
    

有关applicationContext.xml这个配置文件里的内容一定要看懂,我写的还是比较基础和全面的。

有关命名空间p的使用我这里在解释下:   

p:<属性名>="xxx" 引入常量值

p:<属性名>-ref="xxx" 引用其它Bean对象

看完上述内容,你们对Spring框架中如何使用IOC实现装配Bean有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前文章:Spring框架中如何使用IOC实现装配Bean
标题URL:http://pwwzsj.com/article/ghicog.html