Spring中使用set方法进行依赖注入

使用的标签:property
出现的位置:bean标签的内部
标签的属性:

  • name:用于指定注入时,所调用的set方法名称
  • value:用于提供基本类型和String类型的数据
  • ref:用于指定其他的bean类型数据,它指的就是在spring的IoC核心容器中出现过的bean对象。

优势:创建对象时,没有明确的限制,可以直接使用默认构造函数。
弊端:如果有某个成员必须有值,则获取对象时有可能set方法没有执行。

示例:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qublog</groupId>
    <artifactId>spring01_di</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

AccountService接口:

package com.qublog.service;

//账户业务层接口
public interface AccountService {
    //模拟保存账户
    void saveAccount();
}

AccountServiceImpl2类:

package com.qublog.service.impl;

import com.qublog.service.AccountService;

import java.util.Date;

//账户的业务层实现类
public class AccountServiceImpl2 implements AccountService {

    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

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

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了"+name+","+age+","+birthday);
    }
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 把对象的创建交给spring来管理 -->
    <!-- 构造函数注入:
        使用的标签:constructor-arg
        标签出现的位置:bean标签的内部
        标签的属性
    -->
    <bean id="accountService" class="com.qublog.service.impl.AccountServiceImpl">
        <constructor-arg name="name" value="泰斯特"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>

    <!-- 配置一个日期对象 -->
    <bean id="now" class="java.util.Date"></bean>

    <!-- 使用set方法注入
        涉及的标签property
        出现的位置bean标签的内部
    -->
    <bean id="accountService2" class="com.qublog.service.impl.AccountServiceImpl2">
        <property name="name" value="tz"></property>
        <property name="age" value="21"></property>
        <property name="birthday" ref="now"></property>
    </bean>
</beans>

Client类:

package com.qublog.ui;

import com.qublog.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//模拟一个表现层,用于调用业务层
public class Client {
    public static void main(String[] args) {
        //获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //根据id获取bean对象,下面两种方式都可以
        AccountService as = (AccountService)ac.getBean("accountService2");
        as.saveAccount();
    }
}
发布了56 篇原创文章 · 获赞 0 · 访问量 529

猜你喜欢

转载自blog.csdn.net/qq_41242680/article/details/105611982