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

装配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 详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值