欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring(3)--装载Bean的概述

程序员文章站 2022-03-03 11:50:03
...

装配Bean的概述

装配Bean:Spring框架在自动实例化类的时候怎样去找到那个类需要自动实例化

三种装配方式:

  • 在 XML 文件中显式配置
  • 在 Java 的接口和类中实现配置
  • 隐式 Bean 的发现机制和自动装配原则

3.1、XML方式装配

简单装配:

根据类的成员直接在beans.xml中装配成员,如果成员是引用对象需要先将成员对象进行装配,通过ref的形式进行目标类的bean装配

applicationContext.xml

<!--简单转配-->
<bean name="source" class="pojo.Source">
    <property name="fruit" value="橙子"/>
    <property name="sugar" value="多糖"/>
    <property name="size" value="超大杯"/>
</bean>
<!--通过ref方式进行装配-->
<bean name="juice" class="pojo.JuiceMaker">
    <property name="source" ref="source"/>
</bean>

Source.java JuiceMaker.java JuiceMakerText.java

package pojo;

public class Source {
    private String fruit;
    private String sugar;
    private String size;

    public String getFruit() {
        return fruit;
    }

    public void setFruit(String fruit) {
        this.fruit = fruit;
    }

    public String getSugar() {
        return sugar;
    }

    public void setSugar(String sugar) {
        this.sugar = sugar;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return "Source{" +
                "fruit='" + fruit + '\'' +
                ", sugar='" + sugar + '\'' +
                ", size='" + size + '\'' +
                '}';
    }
}
package pojo;

public class JuiceMaker {
    private Source source;

    public void setSource(Source source) {
        this.source = source;
    }

    public void juicing(){
        System.out.println(source);
    }
}
package pojo;

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

public class JuiceMakerTest {
    @Test
    public void juicing(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        JuiceMaker juiceMaker = context.getBean(JuiceMaker.class);
        juiceMaker.juicing();
    }
}

装配集合:

命名空间装配:

3.2、注解方式装配

关键字:@Component, @Value, @ComponentScan

@Component 要装配的类

@Value 属性的默认值

package pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component(value="student") //@Component 不填写value表示直接使用类名,并且采用驼峰命名法
public class Student {
    @Value("1") //赋默认值
    private int id;
    @Value("studentName")
    private String name;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name=" + name +
                '}';
    }
}

@ComponentScan 装配类的扫描类

package pojo;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "pojo,hello")
public class StudentConfig {
}

实例化对象

//通过注解的方式去装载bean
ApplicationContext context = new AnnotationConfigApplicationContext(StudentConfig.class);
Student stu = context.getBean(Student.class);
System.out.println(stu);

也可以在applicationContext.xml中直接配置要扫描的包路径,结合注解去装配bean,这样依然可以通过ClassPathXmlApplicationContext 获得 context 对象

<context:component-scan base-package="pojo"/>
<context:component-scan base-package="aspect"/>

3.3、自动装配

@AutoWired 配置到对象属性或者对象属性的set方法上

package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pojo.Student;

@Component("studentService")
public class StudentServiceImpl implements StudentService{
    @Autowired
    private Student stu;

    public void printStudentInfo(){
        System.out.println(stu.toString());
    }
}

注意需要重新配置扫描文件:StudentConfig

package pojo;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "pojo,service")
public class StudentConfig {
}

参考:

Spring(3)——装配 Spring Bean 详解

相关标签: Spring spring

上一篇: Spring中的Bean

下一篇: spring bean装载