浅谈Spring-DI 依赖注入

浅谈Spring-DI 依赖注入

依赖注入的概念

所谓依赖注入其实就是,给spring容器管理的bean对象,复制一个对象作为其属性.

在学习依赖注入以前,如果我们在一个类中将其他类作为属性.那么就会在这个类中new一个相应的对象,这样其实耦合性就太高了,依赖注入就是解决这个问题的,将耦合性降到最低.我们通过代码来实现,通过两种方式,xml配置文件方式以及注解方式,首先介绍xml方式

创建Person类


public class Person {
    // 定义属性
    private Integer id;
    private String name;

    // 提供set/get方法

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public void say(){
        System.out.println("你好啊:"+id+name);
    }
}

编辑xml配置文件

    <bean id="person" class="com.qty.demo4.Person">
        <property name="id" value="1"/>
        <property name="name" value="老乔"/>
    </bean>

编辑测试类

public class TestPerson {
    public static void main(String[] args) {
        String resource = "spring.xml";
        ApplicationContext context =
                new ClassPathXmlApplicationContext(resource);
        Person person = context.getBean(Person.class);
        person.say();
    }
}

创建Person类的属性类Dog

public class Dog {
    public void pet(){
        System.out.println("我是宠物狗");
    }
}

编辑xml配置文件

    <bean id="person" class="com.qty.demo4.Person">
        <property name="id" value="1"/>
        <property name="name" value="老乔"/>
        <property name="dog" ref="dog"/>
    </bean>
    <bean id="dog" class="com.qty.demo4.Dog"/>

ref:指的是id的属性值

编辑Person类

public class Person {
    // 定义属性
    private Integer id;
    private String name;

    private Dog dog;

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
// 提供set/get方法

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public void say(){
        System.out.println("你好啊:"+id+name);
    }

    public void pet(){
        dog.pet();
    }
}

编辑测试类

public class TestUser {
    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        User user = context.getBean(User.class);
        user.hello();
        //context.close();
    }
}

注意:依赖注入,对象属性必须提供get/set方法.

属性注入的方法

1.set注入

    <bean id="person" class="com.qty.demo4.Person">
        <property name="id" value="1"/>
        <property name="name" value="老乔"/>
    </bean>

注解就是调用set方法,将值注入进去

2.构造方法注入

编辑Dog类

public class Dog {

    public Dog() {
    }
    public Dog(String name) {
        this.name = name;
    }
    // 属性
    private String name;
    public void pet(){
        System.out.println("我是宠物狗"+name);
    }
}

编辑xml配置文件

    <bean id="dog" class="com.qty.demo4.Dog">
        <constructor-arg name="name" value="狗蛋"/>
    </bean>

注意:通过构造方法注入值,需要手动写无参构造,否则无参构造就会被含参构造覆盖,程序报错.

配置xml文件时,根据所写参数匹配相应的构造方法,我这里只写了一个,所以直接匹配含有一个参数的构造方法.

总结:

set和构造两种注入方法没有谁好谁坏,但是set使用的比较多.

注解方式注入

使用到的注解:

@Autowired:在为类注入其他类时,使用在属性上;

编辑配置类

@Configuration
@ComponentScan("com.qty.demo5")
public class SpringConfig {
}

@ComponentScan(“包路径”):扫描标识这个包路径下的类

编辑接口Pet

public interface Pet {
    void say();
}

编辑实现类

@Component
public class Pig implements Pet{
    @Override
    public void say() {
        System.out.println("嗷~");
    }
}

@Component:被这个注解标识的,表明此类被spring容器管理

编辑User类

@Component
public class User {
    @Autowired
    private Pet pet;
    public void say(){
        pet.say();
    }
}

编辑测试类

public class TestUser {
    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        User user = context.getBean(User.class);
        user.say();
        //context.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值