springboot整合applicationContext实现上下文获取实例bean

Posted 健康平安的活着

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合applicationContext实现上下文获取实例bean相关的知识,希望对你有一定的参考价值。

一.工程结构

1.1 工程结构

1.2 逻辑流程

1.先用注解@component将ApplicationContextUtils放到ioc容器中

2.在使用的类中进行引用。

二 操作案例

2.1 将application实例化放到ioc容器中

package com.ljf.spring.boot.demo.util;

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

/**
 * @ClassName: ApplicationContextUtils
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/11/03 21:14:43 
 * @Version: V1.0
 **/
@Component   //此注解记住一定要加,实例化这个bean放到spring的容器中
public class ApplicationContextUtils implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("..............application context 在初始化:..............");
        this.context = applicationContext;
        System.out.println("context:"+context);
    }
    //根据bean名字获取工厂中指定bean 对象
    public static Object getBean(String beanName){
        System.out.println("context:"+context);
        return context.getBean(beanName);
    }
}

2.2 service层

package com.ljf.spring.boot.demo.service.impl;

import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/05/31 11:13:53 
 * @Version: V1.0
 **/
@Service("userService")
public class UserServiceImpl implements UserService {
    @Override
    public List<String> queryUserList() {
        List<String> dataList=new ArrayList<>();
        dataList.add("liu");
        dataList.add("nou");
        return dataList;
    }
}

2.3  controller

package com.ljf.spring.boot.demo.controller;

import com.ljf.spring.boot.demo.service.UserService;
import com.ljf.spring.boot.demo.util.ApplicationContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/05/31 11:30:35 
 * @Version: V1.0
 **/
@Controller
public class UserController
{
    @Autowired
    private ApplicationContextUtils applicationContext;
    @RequestMapping("/test")
    @ResponseBody
    public Object show(){
        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
        List<String> list= userService.queryUserList();
        System.out.println("list:"+list.toString());
        return "ok:"+list.toString();
    }

}

2.4 启动

package com.ljf.spring.boot.demo;

import com.ljf.spring.boot.demo.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import sun.plugin2.message.CustomSecurityManagerRequestMessage;

import java.util.List;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class);
        /**
        SpringApplication application = new SpringApplication(App.class);
        ConfigurableApplicationContext context = application.run(args);
        UserService userPoJpaRepository = context.getBean(UserService.class);
        List<String> list= userPoJpaRepository.queryUserList();
        System.out.println("list:"+list.toString());
         **/
        System.out.println("启动完成!!!");
    }
}

2.5 测试

 

以上是关于springboot整合applicationContext实现上下文获取实例bean的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQSpringBoot 整合 RabbitMQSpringBoot 整合 Kafka)

[SpringBoot系列]SpringBoot如何整合SSMP

springboot怎么整合activiti

SpringBoot完成SSM整合之SpringBoot整合junit

springboot整合jedis

SpringBoot 整合其他框架 -- SpringBoot整合Mybatis