Spring——IOC + DI功能实现(完全注解开发)

本文介绍了如何使用Spring的注解完全实现IOC(控制反转)和DI(依赖注入)。通过两种方式展示了注解的用法:一种是结合@Component、@ComponentScan、@Value和@Autowired;另一种是使用@Configuration和@Bean。示例包括了Student和School类的创建,以及配置类和测试类的编写,详细解释了注解的功能和作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.前言

IOC也是Spring的核心之一了,之前学的时候是采用xml配置文件的方式去实现的,后来其中也多少穿插了几个注解,但是没有说完全采用注解实现。

那么这篇文章就和大家分享一下,全部采用注解来实现IOC + DI。


2.项目源码

2.1 方式一:@Component + @ComponentScan + @Value + @Autowired

首先还是pom文件,maven项目依赖必不可少。

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

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.20</version>
    </dependency>

    <dependency>
      <groupId>javax.annotation</groupId>
      <artifactId>javax.annotation-api</artifactId>
      <version>1.3.2</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

然后,写两个Java Bean,一个是Student学生类、另一个是School学校类。

由于不写xml配置,所以在两个类上方都要加上 @Component 注解,通过注解的方式将其交给Spring IOC容器管理,@Value注解则用于给8种基本数据类型以及String类型做依赖注入,@Autowired是针对引用类型的,这里不再多说了。

package com.szh.bean;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 *
 */
@Data
@Component
public class Student {

    @Value("张起灵")
    private String name;
    @Value("20")
    private Integer age;
    @Autowired
    private School school;

}
package com.szh.bean;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 *
 */
@Data
@Component
public class School {

    @Value("北京大学")
    private String name;
    @Value("北京市海淀区")
    private String address;

}

下面要写一个配置类,功能就是添加包扫描机制,确保上面那两个 @Component 注解修饰的Java Bean可以被Spring扫描并添加至容器中。

package com.szh.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 *
 */
@Configuration
@ComponentScan(basePackages = "com.szh.bean")
public class SpringConfig {
}

最后是我们的测试类了。

package com.szh;

import com.szh.bean.Student;
import com.szh.config.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 *
 */
public class MyTest {

    @Test
    public void testIocAnnotation() {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig.class);
        Student student = (Student) ioc.getBean("student");
        System.out.println(student);
    }
}

2.2 方式二:@Configuration + @Bean

pom文件和方式一是一样的。

下面是不一样的Java Bean。

package com.szh.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Goods {

    private String name;
    private String info;

}
package com.szh.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 *
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {

    private Integer id;
    private BigDecimal totalFee;
    private Goods goods;

}

然后是该方式对应的配置类,采用@Bean实现。

package com.szh.config;

import com.szh.entity.Goods;
import com.szh.entity.Order;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;

/**
 *
 */
@Configuration
public class SpringConfig2 {

    @Bean
    public Goods goods() {
        return new Goods("联想-拯救者", "一款不错的游戏笔记本");
    }

    @Bean
    public Order order(Goods goods) {
        return new Order(1, new BigDecimal(9999), goods);
    }

}

最后是这种方式的测试类代码。

    @Test
    public void testIocAnnotation2() {
        ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig2.class);
        Order order = (Order) ioc.getBean("order");
        System.out.println(order);
        System.out.println("IOC容器中存在的bean如下:");
        String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
        for (String bean : beanDefinitionNames) {
            System.out.println(bean);
        }
    }

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贝尔摩德苦艾酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值