依赖注入

6.1、构造器注入

4中已经说过了

6.2、Set方式注入(重点)

  • 依赖注入:Set注入!
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入!

【环境搭建】

1.创建实体类

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" + "address='" + address + '\'' + '}';
    }
}
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> bobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getBobbys() {
        return bobbys;
    }

    public void setBobbys(List<String> bobbys) {
        this.bobbys = bobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" + "name='" + name + '\'' + ", address=" + address.toString() + ", books=" + Arrays.toString(books) + ", bobbys=" + bobbys + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}';
    }
}

2.bean注入

<bean id="address" class="com.raymond.pojo.Address">
        <property name="address" value="西安"/>
    </bean>

    <bean id="student" class="com.raymond.pojo.Student">
        <!--普通值注入,value-->
        <property name="name" value="RayMond"/>
        <!--Bean注入,ref-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>红楼梦</value>
                <value>三国演义</value>
            </array>
        </property>
        <!--List注入-->
        <property name="bobbys">
            <list>
                <value>足球</value>
                <value>看电影</value>
                <value>敲代码</value>
            </list>
        </property>
        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="111111222222223333"/>
                <entry key="银行卡" value="1231546465132131321"/>
            </map>
        </property>
        <!--Set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>Stream</value>
            </set>
        </property>
        <!--NUll注入-->
        <property name="wife">
            <null/>
        </property>
        <!--properties注入-->
        <property name="info">
            <props>
                <prop key="学号">123456789</prop>
                <prop key="班级">122203</prop>
            </props>
        </property>

    </bean>

3.输出结果

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student student = (Student) context.getBean("student");

        System.out.println(student.toString());


    }
Student{name='RayMond', address=Address{address='null'}, books=[西游记, 水浒传, 红楼梦, 三国演义], bobbys=[足球, 看电影, 敲代码], card={身份证=111111222222223333, 银行卡=1231546465132131321}, games=[LOL, Stream], wife='null', info={学号=123456789, 班级=122203}}

6.3、P命名空间

可以直接注入属性的值:property

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="classic" class="com.example.ExampleBean">
        <property name="email" value="someone@somewhere.com"/>
    </bean>

    <bean name="p-namespace" class="com.example.ExampleBean"
        p:email="someone@somewhere.com"/>
</beans>

6.4、C命名空间

通过构造器注入,constructor-arg

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="beanTwo" class="x.y.ThingTwo"/>
    <bean id="beanThree" class="x.y.ThingThree"/>

    <!-- traditional declaration with optional argument names -->
    <bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg name="thingTwo" ref="beanTwo"/>
        <constructor-arg name="thingThree" ref="beanThree"/>
        <constructor-arg name="email" value="something@somewhere.com"/>
    </bean>

    <!-- c-namespace declaration with argument names -->
    <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
        c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>

</beans>

注意点,P命名空间和C命名空间不能直接使用,必须导入依赖

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

6.5、Bean的作用域

在这里插入图片描述

1.单例模式(Spring默认机制)

<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

在这里插入图片描述

2.原型模式(每次从容器中get的时候,都会产生一个新的对象!)

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>

在这里插入图片描述

3.其余的request、session、application 这些只能在web开发中使用到!

7、Bean的自动装配

  • 自动装配是Spring满足Bean依赖的一种方式
  • Spring会在上下文中自动寻找,并自动给bean装配属性!

在Spring中有三中装配的方式

1.在xml中显示的装配

2.在java中显示配置

3.隐式的自动装配bean【重要!!!】

7.1、ByName自动装配和ByType自动装配

<bean id="address" class="com.raymond.pojo.Address" autowire="byType">
    <property name="address" value="西安"/>
</bean>
  • 使用byName的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
  • 使用byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的属性类型一致!

7.2、使用注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解了。

The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.

使用须知:

​ 1.导入约束,context约束

​ 2.配置注解的支持:

<?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
                           https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
</beans>

@Autowired直接在属性上使用即可。

使用Autowired我们可以不用编写set方法了,前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byName。

科普

@Nullable  字段标注了这个注解,说明这个字段可以为null
@Qualifier("main") 如果自动装配无法通过一个注解完成,我们可以使用Qualifier来配合Autowired使用,指定一个唯一的bean对象注入
@Resource  java的注解

小结:

@Resource和@Autowired的区别

  • 都是用来自动装配的,都可以房子啊属性字段上
  • @Autowired通过byType的方式实现,而且必须要求这个对象存在【常用】
  • @Resource默认通过byName的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到,报错!【常用】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值