IntelliJ idea version 2017 Spring5 's Runnablefactorybean configuration

Source: Internet
Author: User

1. New Runnablefactorybean

1  PackageCom.spring4.pojo;2 3 ImportOrg.springframework.beans.factory.FactoryBean;4 5 /**6 * Created by Liuya7  *8  * @authorUser:liuya9 * DATE:2018/5/3Ten * time:23:31 One * Projectname:spring4 A  */ -  Public classRunnablefactorybeanImplementsFactorybean<runnable> { -  the  -     /** - * Gets the instance object obtained by Factorybean -      * +      * @returnRunnable -      * @throwsException +      */ A @Override at      PublicRunnable GetObject ()throwsException { -         return() { -         }; -     } -  -     /** in * What type of object is created -      * to      * @returnclass<?> +      */ - @Override the      PublicClass<?>Getobjecttype () { *         returnRunnable.class; $     }Panax Notoginseng  -  the     /** + * is not a single case A      * the      * @return +      */ - @Override $      Public BooleanIssingleton () { $         return true; -     } -}
View Code

2. Load into the Myconfig

1  Packagecom.spring4.conf;2 3 ImportCom.spring4.pojo.RunnableFactoryBean;4 ImportCom.spring4.pojo.User;5 ImportOrg.springframework.context.annotation.Bean;6 Importorg.springframework.context.annotation.Configuration;7 ImportOrg.springframework.context.annotation.Scope;8 9 /**Ten * Created by Liuya One  * A  * @author: Liuya - * DATE:2018/5/2 - * time:23:50 the * Projectname:spring4 - * Configuration Class -  */ -  + @Configuration -  Public classspringconf { +  A  at     /** - * Config Bean write a method, return bean - * <p> - * Specify name, default is method name -      */ -@Bean (name = "GetUser") in     /** - * modified to non-singleton mode, created by the object is not the same to      * +      */ -@Scope (value = "Prototype") the      PublicUser GetUser () { *User User =NewUser (); $User.setage (29);Panax NotoginsengUser.setusername ("Wang Yuan Yuan"); -User.setpassword ("1110000"); the  +         returnuser; A     } the  + @Bean -      PublicRunnablefactorybean Createrunnablefactorybean () { $Runnablefactorybean Runnablefactorybean =NewRunnablefactorybean (); $  -         returnRunnablefactorybean; -  the     } - Wuyi  the}
View Code

3, method added application test

1  Packagecom.study.spring;2 3 Importcom.spring4.conf.SpringConf;4 ImportCom.spring4.pojo.RunnableFactoryBean;5 ImportCom.spring4.pojo.User;6 Importorg.springframework.boot.SpringApplication;7 Importorg.springframework.boot.autoconfigure.SpringBootApplication;8 ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext;9 Ten Importjava.io.IOException; One Importjava.util.List; A  - /** -  * @authorLiuyang the  */ - @SpringBootApplication -  Public classspring4application { -  +      Public Static voidMain (string[] args) { -         //instantiate a spring container with a Java configuration +Annotationconfigapplicationcontext context =NewAnnotationconfigapplicationcontext (springconf.class); A  at         //get the Bean object in the spring container -User user = Context.getbean (user.class); -  -         //Get by name (default is based on method name) -Object user2 = Context.getbean ("GetUser"); -  inSystem.out.println (User.getusername () + "," + user.getage () + "," +User.getpassword ()); -System.out.println (user2.tostring () + ","); toString list =user2.tostring (); +String[] ont = list.split (","); -          for(inti = 0; i < ont.length; i++) { the System.out.println (Ont[i]); *         } $ Panax NotoginsengRunnablefactorybean Runnablefactorybean = Context.getbean (Runnablefactorybean.class); -  the         Try { +System.out.println ("Createrunnablefactorybean:" +Runnablefactorybean); A}Catch(Exception e) { the e.printstacktrace (); +         } -  $         //Destroy the container $ Context.destroy (); -     } -}
View Code

IntelliJ idea version 2017 Spring5 's Runnablefactorybean configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.