在Spring中获取Bean的各种方式

1. 通过Spring上下文(ApplicationContext)获取

方式:手动获取Spring上下文,然后从上下文中获取Bean。

示例

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

public class Main {
    public static void main(String[] args) {
        // 加载Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        // 获取Bean
        MyBean myBean = context.getBean(MyBean.class);
        myBean.doSomething();
    }
}

适用场景:适用于需要手动控制Bean创建和管理的场景,通常在简单的测试或非Spring管理的类中使用。

2. 通过注解方式自动注入

使用@Autowired注解

方式:在需要注入Bean的字段、构造器或方法上使用@Autowired注解。

示例

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

@Service
public class MyService {
    @Autowired
    private MyBean myBean;

    public void performTask() {
        myBean.doSomething();
    }
}

适用场景:适用于需要依赖注入的类中,这是Spring中最常用的注入方式。

使用@Resource注解

方式:在需要注入Bean的字段或方法上使用@Resource注解。

示例

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    @Resource
    private MyBean myBean;

    public void performTask() {
        myBean.doSomething();
    }
}

适用场景:适用于需要基于名称注入的场景,@Resource注解更符合Java EE标准。

3. 通过构造器注入

方式:在构造器上使用@Autowired注解,Spring会自动注入依赖。

示例

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

@Service
public class MyService {
    private final MyBean myBean;

    @Autowired
    public MyService(MyBean myBean) {
        this.myBean = myBean;
    }

    public void performTask() {
        myBean.doSomething();
    }
}

适用场景:适用于强制依赖注入,确保所有依赖在对象创建时就已经准备好。

4. 通过@Bean注解的方法

方式:在@Configuration类中定义@Bean方法,Spring会自动调用这些方法并管理返回的Bean。

示例

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

@Configuration
public class AppConfig {
    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

适用场景:适用于需要在Java代码中配置Bean而不是使用XML配置的场景。

5. 通过ApplicationContextAware接口获取

方式:实现ApplicationContextAware接口,并将ApplicationContext保存为静态变量,从而全局访问。

示例

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class MyBeanGetter implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        context = applicationContext;
    }

    public static MyBean getMyBean() {
        return context.getBean(MyBean.class);
    }
}

适用场景:适用于需要在非Spring管理的类中访问Spring管理的Bean。

6. 通过BeanFactory获取

方式:使用BeanFactory接口手动获取Bean。

示例

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyBean myBean = factory.getBean(MyBean.class);
        myBean.doSomething();
    }
}

适用场景:适用于需要更轻量级的Bean容器管理的场景,但一般不如ApplicationContext常用。

7. 使用@Lookup注解

方式:在抽象方法上使用@Lookup注解,Spring会自动实现该方法以返回所需的Bean。

示例

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

@Component
public abstract class MyBeanProvider {
    @Lookup
    public abstract MyBean getMyBean();
}

适用场景:适用于需要每次调用都返回新的Bean实例的场景(如Prototype作用域的Bean)。

这些方法各有优缺点和适用场景,可以根据具体的需求选择合适的方式来获取Spring Bean。

相关推荐

  1. Spring获取Bean各种方式

    2024-07-22 22:00:02       14 阅读
  2. Spring Bean获取方法 (手动注入bean)超级实用

    2024-07-22 22:00:02       55 阅读
  3. spring更加松散获取bean方式ObjectProvider

    2024-07-22 22:00:02       69 阅读
  4. Spring手动获取bean四种方式

    2024-07-22 22:00:02       46 阅读
  5. 使用SpringContextHolder获取 Spring 容器 Bean

    2024-07-22 22:00:02       43 阅读

最近更新

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

    2024-07-22 22:00:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 22:00:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 22:00:02       45 阅读
  4. Python语言-面向对象

    2024-07-22 22:00:02       55 阅读

热门阅读

  1. AWS认证SAA-C03每日一题

    2024-07-22 22:00:02       13 阅读
  2. python 中的 pprint 和 print有什么区别?

    2024-07-22 22:00:02       15 阅读
  3. 优化ifelse语句

    2024-07-22 22:00:02       16 阅读
  4. redis常用指令

    2024-07-22 22:00:02       15 阅读
  5. 多站点环境下Memcached的配置与管理

    2024-07-22 22:00:02       18 阅读
  6. Vue3 深入组件

    2024-07-22 22:00:02       17 阅读