IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置
Posted 北极的大企鹅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置相关的知识,希望对你有一定的参考价值。
1、新建RunnableFactoryBean
1 package com.spring4.pojo; 2 3 import org.springframework.beans.factory.FactoryBean; 4 5 /** 6 * Created by liuya 7 * 8 * @author User: liuya 9 * Date: 2018/5/3 10 * Time: 23:31 11 * projectName:spring4 12 */ 13 public class RunnableFactoryBean implements FactoryBean<Runnable> { 14 15 16 /** 17 * 获取FactoryBean获取的实例对象 18 * 19 * @return Runnable 20 * @throws Exception 21 */ 22 @Override 23 public Runnable getObject() throws Exception { 24 return () -> { 25 }; 26 } 27 28 /** 29 * 创建什么类型的对象 30 * 31 * @return Class<?> 32 */ 33 @Override 34 public Class<?> getObjectType() { 35 return Runnable.class; 36 } 37 38 39 /** 40 * 是不是单例的 41 * 42 * @return 43 */ 44 @Override 45 public boolean isSingleton() { 46 return true; 47 } 48 }
2、加载到Myconfig中
1 package com.spring4.conf; 2 3 import com.spring4.pojo.RunnableFactoryBean; 4 import com.spring4.pojo.User; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Scope; 8 9 /** 10 * Created by liuya 11 * 12 * @author : liuya 13 * Date: 2018/5/2 14 * Time: 23:50 15 * projectName:spring4 16 * 配置类 17 */ 18 19 @Configuration 20 public class SpringConf { 21 22 23 /** 24 * 配置bean 写一个方法,返回bean 25 * <p> 26 * 指定名字,默认是方法名字 27 */ 28 @Bean(name = "getUser") 29 /** 30 * 修改为非单例模式,创建的对象就是不一样的了 31 * 32 */ 33 @Scope(value = "prototype") 34 public User getUser() { 35 User user = new User(); 36 user.setAge(29); 37 user.setUserName("王圆圆"); 38 user.setPassWord("1110000"); 39 40 return user; 41 } 42 43 @Bean 44 public RunnableFactoryBean createRunnableFactoryBean() { 45 RunnableFactoryBean runnableFactoryBean = new RunnableFactoryBean(); 46 47 return runnableFactoryBean; 48 49 } 50 51 52 }
3、方法加入Application测试
1 package com.study.spring; 2 3 import com.spring4.conf.SpringConf; 4 import com.spring4.pojo.RunnableFactoryBean; 5 import com.spring4.pojo.User; 6 import org.springframework.boot.SpringApplication; 7 import org.springframework.boot.autoconfigure.SpringBootApplication; 8 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 9 10 import java.io.IOException; 11 import java.util.List; 12 13 /** 14 * @author liuyang 15 */ 16 @SpringBootApplication 17 public class Spring4Application { 18 19 public static void main(String[] args) { 20 // 通过Java配置来实例化Spring容器 21 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConf.class); 22 23 // 在Spring容器中获取Bean对象 24 User user = context.getBean(User.class); 25 26 //根据名字获取(默认是根据方法名字获取) 27 Object user2 = context.getBean("getUser"); 28 29 System.out.println(user.getUserName() + ", " + user.getAge() + ", " + user.getPassWord()); 30 System.out.println(user2.toString() + ", "); 31 String list = user2.toString(); 32 String[] ont = list.split(","); 33 for (int i = 0; i < ont.length; i++) { 34 System.out.println(ont[i]); 35 } 36 37 RunnableFactoryBean runnableFactoryBean = context.getBean(RunnableFactoryBean.class); 38 39 try { 40 System.out.println("createRunnableFactoryBean :"+runnableFactoryBean); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 45 // 销毁该容器 46 context.destroy(); 47 } 48 }
以上是关于IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置的主要内容,如果未能解决你的问题,请参考以下文章
Intellij idea史上最简单的教程之Linux下安装与破解Intellij idea2017
IntelliJ IDEA 2017版 spring-boot2.0.2 自动配置Condition
IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例
IntelliJ IDEA 2017版 spring-boot2.0.4的集成JSP