IntelliJ IDEA 2017 version of Spring5's RunnableFactoryBean configuration

1. Create a new 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       * what type to create object
 30       *
 31       * @return Class<?>
 32       */ 
33      @Override
 34      public Class<?>getObjectType() {
 35          return Runnable.class ;
 36     }
 37 38 39 /** 40      * is it a singleton
 41      *
 42      * @return 43 */ 44     @Override
 45 public boolean isSingleton () {
 46 return true ;
 47     }
 48 }  
 
     
   
      
                  
View Code

2. Load into 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  * Configuration class
 17   */ 
18  
19  @Configuration
 20  public  class SpringConf {
 21  
22  
23      /** 
24       * Configure bean Write a method, return bean
 25       * <p>
 26       * Specify the name, the default is the method name
 27       */ 
28      @Bean(name = "getUser" )
 29      /** 
30       * Modified to non-singleton mode, the created object is different
 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. Method to join Application test

 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          // Instantiate Spring container through Java configuration 
21          AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConf. class ) ;
 22  
23          // Get the Bean object in the Spring container 
24          User user = context.getBean(User.class ) ;
 25  
26          //Get by name (by default, get by method name) 
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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325233942&siteId=291194637