码迷,mamicode.com
首页 > 编程语言 > 详细

IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置

时间:2018-05-04 01:41:00      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:ati   2-2   factor   text   容器   OLE   一个   ++   work   

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 }
View Code

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 }
View Code

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 }
View Code

 

IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置

标签:ati   2-2   factor   text   容器   OLE   一个   ++   work   

原文地址:https://www.cnblogs.com/liuyangfirst/p/8988404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!