IOC容器的注入可以使用XML和注解的方式,XML已经被淘汰,主流方式是通过注解的方式。

首先引入依赖

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

<!--        spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.15</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>

创建一个实体类,并注入IOC容器,可以通过四个注解进行注入,不同的注解可用在不同层

  • @Component 其他
  • @Controller 控制层
  • @Repository 持久层
  • @Service 业务层

这里创建的实体使用@Component注解标注

package cn.vwmwv.spring.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Account {
    private Integer id;
    private String name;
    private Integer age;

}

注入IOC

package cn.vwmwv.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {
    public static void main(String[] args) {
        // 加载IOC容器
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext("cn.vwmwv.spring.entity");
        System.out.println(applicationContext.getBean("account")); // 输出Account 实体类 值用@Value注入
        System.out.println(applicationContext.getBeanDefinitionCount()); // 输出Bean的数量
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) { // 循环输入Bean名字和内容
            System.out.println(beanDefinitionName);
            System.out.println(applicationContext.getBean(beanDefinitionName));
        }
    }
}

使用@Autowired注解将另一个实体类注入到当前实体类中,这个注解会自动根据类的类型找Bean,如果根据名字来找则使用@Qualifier("名字")

package cn.vwmwv.spring.entity;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Account {

    @Value("1")
    private Integer id;
    @Value("名字")
    private String name;
    @Value("22")
    private Integer age;

    @Autowired
    @Qualifier("order")
    private Order order;

}

package cn.vwmwv.spring.entity;

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

@Data
@Component
public class Order {

    @Value("12345")
    private String orderId;

    @Value("1000.0")
    private Float price;
}

输出的结果为:

Account(id=1, name=名字, age=22, order=Order(orderId=12345, price=1000.0))
6
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor@5136d012
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@5939a379
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.EventListenerMethodProcessor@e1de817
org.springframework.context.event.internalEventListenerFactory
org.springframework.context.event.DefaultEventListenerFactory@52102734
account
Account(id=1, name=名字, age=22, order=Order(orderId=12345, price=1000.0))
order
Order(orderId=12345, price=1000.0)

Process finished with exit code 0
如果觉得我的文章对你有用,请随意赞赏