如何在非Spring管理的类中使用Spring加载的bean
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

package com.example.demo;
public class Person {
private String name;
public Person() {
System.out.println("person init ..");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
package com.example.demo; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext; public class ContextUtil{ public ContextUtil () { } private static ApplicationContext applicationContext = null; public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ContextUtil.applicationContext = applicationContext;
} public static Object getObject(String beanName) {
if (applicationContext == null) {
throw new RuntimeException("applicationContext is null");
}
return applicationContext.getBean(beanName);
}
}
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; @SpringBootApplication
public class DemoApplication { public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
ContextUtil.setApplicationContext(context); // 在这里把加载好的ApplicationContext设置到静态类中,以后通过这个类取Spring容器中的bean
Person person = (Person)ContextUtil.getObject("p");
System.out.println(person);
} @Bean(name = "p")
public Person person() {
Person person = new Person();
person.setName("小明");
return person;
} }
如何在非Spring管理的类中使用Spring加载的bean的更多相关文章
- 在java中如何在非servlet的普通类中获取request、response、session
原文:http://blog.csdn.net/u012255097/article/details/53092628 在spring的普通类中: HttpServletRequest request ...
- Java 类中各成分加载顺序 和 内存中的存放位置
参加一个笔试,有一个关于类的静态代码块.构造代码块.构造函数的执行顺序的问题.不太清楚,网上百度了一下.在这里记录一下. 一.什么时候会加载类?使用到类中的内容时加载:有三种情况1.创建对象:new ...
- Java笔记(十二)……类中各部分加载顺序及存放位置问题
什么时候会加载类 使用到类中的内容时加载,三种情况: 创建对象:new StaticDemo(); 使用类中的静态成员:StaticCode.num = 9; StaticCode.getNum() ...
- 没有纳入spring管理的类如何注入spring管理的对象
spring 如何在普通类中调用注入的对象? spring 在Thread中注入@Resource失败,总为null~解决 springmvc 注入总是空指针异常? 以上的几个问题就是我在项目中遇到的 ...
- java类中属性的加载顺序,以及内存分配情况介绍
看下面例子及说明: /** 假如有外部类调用了该类,代码为:new StaticTest(); 那么下面是类属性的加载顺序 */ public class StaticTest{ public int ...
- 170630、springboot编程之普通类中调用spring管理的bean对象
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
- 深入理解 Laravel 中 config 配置加载原理
Laravel的配置加载其实就是加载config目录下所有文件配置.如何过使用php artisan config:cache则会把加载的配置合并到一个配置文件中,下次请求就不会再去加载config目 ...
- spring管理的类如何调用非spring管理的类
spring管理的类如何调用非spring管理的类. 就是使用一个spring提供的感知概念,在容器启动的时候,注入上下文即可. 下面是一个工具类. import org.springframewor ...
- Spring Boot集成Quartz注入Spring管理的类
摘要: 在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入. Spring有自己的Schedule定时任务,在S ...
随机推荐
- 20145313张雪纯 《Java程序设计》第5周学习总结
20145313张雪纯 <Java程序设计>第5周学习总结 教材学习内容总结 JAVA中所有错误都会被打包成对象,可以用尝试(try)捕捉(catch)代表错误的对象后做一些处理.使用tr ...
- 项目总结--基于Cortex-A9平台的米兰花智能培育系统
基于Cortex-A9平台的米兰花智能培育系统 1. 系统功能概述 本系统主要实现了模拟米兰花智能培育的过程.通过前端传感器采集相关环境因子数据经ZigBee组网发送到协调器汇总,网关通过串口读取协调 ...
- [Deep Learning] 神经网络基础【转】
本文转载自:http://www.cnblogs.com/maybe2030/p/5597716.html 阅读目录 1. 神经元模型 2. 感知机和神经网络 3. 误差逆传播算法 4. 常见的神经网 ...
- Live Score FAQ
Q: Why doesn't the selected game go to top? A: The game which include your favorite team will be alw ...
- 计算java对象的内存占用
代码引用自:https://blog.csdn.net/antony9118/article/details/54317637 感谢博主分享: import java.util.ArrayList; ...
- JAVA小工具打包
D: cd D:\xxx\IPOSpider javac -d bin/ src/com/xxx/IPOSpider.java src/com/xxx/ConfigProperties.java -c ...
- Python学习札记(十九) 高级特性5 迭代器
参考:迭代器 Note 1.可用于for循环的对象有两类:(1)集合数据类型:list tuple dict str set (2)Generator:生成器和含yield语句的函数.这些可以直接作用 ...
- 面试笔试总结(二)之 C++基础
上节,一定要写出基于引用计数的智能指针 明白单例模式 会写出代码 复习: 1- 2- 推荐leveldb ....是c++的写代码很规范的地方?比如智能指针在这里... 对类进行改造 可以改成Sing ...
- 【三小时学会Kubernetes!(三) 】Service实践
服务Service Kubernetes 服务资源可以作为一组提供相同服务的 Pod 的入口.这个资源肩负发现服务和平衡 Pod 之间负荷的重任,如图 16 所示. 图16:Kubernetes 服务 ...
- linux定时任务php
1. 在需要定时执行的PHP文件的第一行加 #! /usr/local/php/bin/php 服务器php.exe的位置 2. 上传要定时执行的php文件到一个位置,可以通过/pa ...