Spring配置的三种主要装配机制
Spring配置的三种主要装配机制
李大钊_ 发表于7个月前
Spring配置的三种主要装配机制
  • 发表于 7个月前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

摘要: Spring Bean的注入方式

1.在XML中进行显示配置

对应的java代码如下所示

package com.spring;
public class Address {
    private String desc;
    // getter setter
}
package com.spring;
public class Car {
    private String desc;
}
package com.spring;

public class Student {
    private String name;
    private Address address;

    public Student(Address address) {
        this.address = address;
    }
}

在XML中可以通过下面的配置来生成一个bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="address" class="com.spring.Address" />
    <bean id="car" class="com.spring.Car" />

    <bean id="student" class="com.spring.Student">
        <constructor-arg ref="address" /><!--构造函数注入 -->
        <property name="car" ref="car" /><!-- set方法注入-->
    </bean>
</beans>

2.在Java中进行显示配置

package com.spring;
public class Address {
    private String desc;
    // getter setter
}

package com.spring;

import org.springframework.stereotype.Component;
public class Car {
    private String desc;
}
package com.spring;

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
@ComponentScan(basePackages = "com.spring")
public class JavaConfig {

    @Bean
    public Car car(){
        return new Car();
    }
    
    @Bean
    public Address address(){
        return new Address();
    }

    @Bean
    public Student student() {
        Student student = new Student(address());
        student.setCar(car());
        return student;
    }
}

3.隐式的bean发现机制和自动装配

package com.spring;

import org.springframework.stereotype.Component;

@Component("address")
public class Address {
    private String desc;
    // getter setter
}
package com.spring;

import org.springframework.stereotype.Component;

@Component("car")
public class Car {
    private String desc;
}
package com.spring;

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

@Component("student")
public class Student {
    private String name;
    private Address address;
    private Car car;

    @Autowired
    public Student(Address address) {
        this.address = address;
    }

    public Car getCar() {
        return car;
    }

    @Autowired
    public void setCar(Car car) {
        this.car = car;
    }
}

@Configuration
@ComponentScan(basePackages = "com.spring")
public class JavaConfig {}
标签: Spring Bean
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 6
码字总数 1484
×
李大钊_
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: