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);
    }
}

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

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);
        }
    }

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