IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置
1、新建RunnableFactoryBean
package com.spring4.pojo; import org.springframework.beans.factory.FactoryBean; /**
* Created by liuya
*
* @author User: liuya
* Date: 2018/5/3
* Time: 23:31
* projectName:spring4
*/
public class RunnableFactoryBean implements FactoryBean<Runnable> { /**
* 获取FactoryBean获取的实例对象
*
* @return Runnable
* @throws Exception
*/
@Override
public Runnable getObject() throws Exception {
return () -> {
};
} /**
* 创建什么类型的对象
*
* @return Class<?>
*/
@Override
public Class<?> getObjectType() {
return Runnable.class;
} /**
* 是不是单例的
*
* @return
*/
@Override
public boolean isSingleton() {
return true;
}
}
2、加载到Myconfig中
package com.spring4.conf; import com.spring4.pojo.RunnableFactoryBean;
import com.spring4.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; /**
* Created by liuya
*
* @author : liuya
* Date: 2018/5/2
* Time: 23:50
* projectName:spring4
* 配置类
*/ @Configuration
public class SpringConf { /**
* 配置bean 写一个方法,返回bean
* <p>
* 指定名字,默认是方法名字
*/
@Bean(name = "getUser")
/**
* 修改为非单例模式,创建的对象就是不一样的了
*
*/
@Scope(value = "prototype")
public User getUser() {
User user = new User();
user.setAge(29);
user.setUserName("王圆圆");
user.setPassWord("1110000"); return user;
} @Bean
public RunnableFactoryBean createRunnableFactoryBean() {
RunnableFactoryBean runnableFactoryBean = new RunnableFactoryBean(); return runnableFactoryBean; } }
3、方法加入Application测试
package com.study.spring; import com.spring4.conf.SpringConf;
import com.spring4.pojo.RunnableFactoryBean;
import com.spring4.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.io.IOException;
import java.util.List; /**
* @author liuyang
*/
@SpringBootApplication
public class Spring4Application { public static void main(String[] args) {
// 通过Java配置来实例化Spring容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConf.class); // 在Spring容器中获取Bean对象
User user = context.getBean(User.class); //根据名字获取(默认是根据方法名字获取)
Object user2 = context.getBean("getUser"); System.out.println(user.getUserName() + ", " + user.getAge() + ", " + user.getPassWord());
System.out.println(user2.toString() + ", ");
String list = user2.toString();
String[] ont = list.split(",");
for (int i = 0; i < ont.length; i++) {
System.out.println(ont[i]);
} RunnableFactoryBean runnableFactoryBean = context.getBean(RunnableFactoryBean.class); try {
System.out.println("createRunnableFactoryBean :"+runnableFactoryBean);
} catch (Exception e) {
e.printStackTrace();
} // 销毁该容器
context.destroy();
}
}
IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置的更多相关文章
- IntelliJ IDEA 2017版 Spring5最基本的bean例子创建
一.简述 SpringBoot是基于spring框架之上的快速开发的框架.Spring4核心就是容器,容器提供了对bean的装配和管理. spring依赖加载: ...
- IntelliJ IDEA 2017版 SpringBoot的核心配置详解
Spring Boot的核心 (1)Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法. (2)@Spri ...
- IntelliJ IDEA 2017版 Spring5 java.lang.NoSuchMethodError: org.springframework.boot.SpringApplication.<init>([Ljava/lang/Object;)V
错误是java.lang.NoSuchMethodError: org.springframework.boot.SpringApplication.<init>([Ljava/lang/ ...
- IntelliJ IDEA 2017版 spring-boot加载jsp配置详解(详细图文实例)
一.创建项目 (File--->New-->Project) 2.项目配置内容 3.选择配置项目的Group包名,Artifact项目名称 4.选择项目类型为web类型 5.创建成功,点击 ...
- IntelliJ IDEA 2017版 spring-boot2.0.4的yml配置使用
一.必须配置字端两个 server: port: 8080 servlet: context-path: /demo 二.两种mvc转换springboot,一种是注解,一种就是.yml或proper ...
- IntelliJ IDEA 2017版 SpringBoot的关闭自动配置和自定义Banner
一.关闭自动配置 在jar包下找下边的名字 设置关闭自动配置jar 多个的时候配置 二.自定义Banner (1)网站搜索一个图案.网址:http://patorjk.co ...
- IntelliJ IDEA 2017版 spring-boot修改端口号配置把端口号改为8081
1.修改端口号主要是通过配置文件修改.如图: 完整版配置 ######################################################## ###server 配置信息 ...
- IntelliJ IDEA 2017版 spring-boot2.0.2 自动配置Condition
描述: 编译器修改参数 -Dfile.encoding=GBK -Dstr.encoding=GBK Condition位置: 某一个类或注解存在的时候,装配,否则不装配 相关代码: ...
- IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)
一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...
随机推荐
- 开机进入boot menu和application menu,无法开机
1.开机点击F1进入到bios界面 2.进入Security—Secure Boot—Disabled 如果不修改Secure boot选项为Disabled,在光驱引导时可能会出现报错 3. ...
- linux详解sudoers
sudo使用 Linux是多用户多任务的操作系统, 共享该系统的用户往往不只一个.出于安全性考虑, 有必要通过useradd创建一些非root用户, 只让它们拥有不完全的权限; 如有必要,再来提升权限 ...
- mysql定位慢查询
mysql定位慢查询 //显示数据库的状态 show status; //显示执行了多少次插入 show status like 'com_insert'; //显示执行了多少次更新 show sta ...
- DevExpress XPO 开发指南 简要
最近在看devexpress 安装程序中的代码Demos .. C:\Users\Public\Documents\DevExpress Demos 16.1\Components\WinFor ...
- Service Broker 概述
ServiceBroker(简称SSB)是基于数据库引擎提供的一个强大的异步编程模型,通过ServiceBroker,开发人员无需编写复杂的通信和消息程序,即可在数据库实例之间完成高效可靠的异步通信. ...
- Vs2015 c# 诊断工具查看程序的占用情况
windbg用着还不熟悉,dottrace 还要版权,着急查看程序的cpu 的使用情况,因为程序开启之后占用处理器资源较大,问题在哪里呢,于是点开了vs2015自带的诊断工具,以前偶尔打开过,没发现 ...
- Android.DebugOnDevices
真机调试Android http://www.cnblogs.com/junqilian/archive/2012/11/08/2760734.html
- 离线在MVC4中的使用
最近在项目中用到了离线功能,自己感觉挺高端的,但是遇到了一些问题. 现在工作告一小段落,就把这次离线工作中遇到的关键技术点和难题记录下来. 什么是离线,什么时候需要离线 离线:Offline,不联网也 ...
- Luogu 2279 [HNOI2003]消防局的设立 - 贪心
Description 给定一棵树形图, 建若干个消防站, 消防站能够覆盖到距离不超过2的点, 求最少需要建几个消防站才能覆盖所有点 Solution 从深度最深的点开始, 在它的爷爷节点上建, 每建 ...
- Codeforces 792B. Counting-out Rhyme
B. Counting-out Rhyme time limit per test: 1 second memory limit per test: 256 megabytes input: stan ...