spring —— IoC容器(二)

二、基于注解管理 bean

(一)Autowired 自动装配

(1)属性上自动装配

package com.fourth.anno;

import org.springframework.stereotype.Component;

@Component
public class Dao {
    public void runDao(){
        System.out.println("hello world");
    }
}
package com.fourth.anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Service {
    @Autowired
    private Dao dao;
    public void runService(){
        dao.runDao();
    }
}
package com.fourth.anno;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-anno.xml");
        Service service = context.getBean(Service.class);
        service.runService();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <context:component-scan base-package="com.fourth.anno"></context:component-scan>
</beans>

(2)set 方法上自动装配 

package com.fourth.anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Service {    
    private Dao dao;

    @Autowired
    public void setDao(Dao dao){
        this.dao=dao;
    }

    public void runService(){
        dao.runDao();
    }
}

(3)构造方法上自动装配 

package com.fourth.anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Service {

    private Dao dao;
    @Autowired
    public Service(Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}

(4)构造方法的形参上自动装配 

package com.fourth.anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Service {

    private Dao dao;

    public Service(@Autowired Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}

 (二)Resource 自动装配

(1)属性上自动装配

package com.fourth.anno;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class Service {
    @Resource
    private Dao dao;

    public void runService(){
        dao.runDao();
    }
}

(2)set 方法上自动装配 

package com.fourth.anno;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class Service {

    private Dao dao;
    @Resource
    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}

 Autowired 与 Resource 比较:

Autowired 可以应用在属性、set 方法、构造方法、构造方法的形参上;其默认通过类型进行装配。

Resource 可以应用在属性、set 方法上;其默认通过名称进行装配。

 Autowired 基于 spring 框架;Resource 基于 java 扩展包,如果 JDK 版本高于11或低于8,应引入以下依赖:

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>

(三)全注解开发

全注解开发,即使用配置类代替 xml 配置文件:

package com.fourth.anno;

import org.springframework.stereotype.Component;

@Component
public class Dao {
    public void runDao(){
        System.out.println("hello world");
    }
}
package com.fourth.anno;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class Service {

    private Dao dao;
    @Resource
    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}
package com.fourth.anno;

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

@Configuration
@ComponentScan(value = "com.fourth.anno")
public class SpringConfig {
}
package com.fourth.anno;

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

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        Service service = context.getBean(Service.class);
        service.runService();
    }
}

 

 

 

相关推荐

  1. SpringIOC容器Bean对象的实例化模拟

    2024-07-23 09:00:03       41 阅读
  2. SpringIoC原理

    2024-07-23 09:00:03       55 阅读
  3. vector容器()

    2024-07-23 09:00:03       42 阅读
  4. spring —— IoC容器

    2024-07-23 09:00:03       30 阅读
  5. 简单实现Spring容器()

    2024-07-23 09:00:03       56 阅读
  6. list(链表)容器

    2024-07-23 09:00:03       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 09:00:03       103 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 09:00:03       114 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 09:00:03       93 阅读
  4. Python语言-面向对象

    2024-07-23 09:00:03       99 阅读

热门阅读

  1. Postman 接口测试工具详解

    2024-07-23 09:00:03       25 阅读
  2. Vue中 watch 与 watchEffect 的区别

    2024-07-23 09:00:03       27 阅读
  3. Python题解Leetcode Hot100之回溯

    2024-07-23 09:00:03       26 阅读
  4. 【MySQL进阶之路 | 高级篇】反范式化概述

    2024-07-23 09:00:03       21 阅读
  5. python—爬虫爬取图片网页实例

    2024-07-23 09:00:03       29 阅读
  6. stm32 io输入中断

    2024-07-23 09:00:03       30 阅读
  7. pytorch lightning报错all tensors to be on the same device

    2024-07-23 09:00:03       21 阅读