IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置

一、新建RunnableFactoryBeanjava

 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

二、加載到Myconfig中spring

 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

三、方法加入Application測試ide

 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
相關文章
相關標籤/搜索