How to 1.0-springboot Java configuration

Source: Internet
Author: User

1. Create the user entity class.
@Data  Public class User {    private  String username;     Private String password;     Private Integer age;}
2. Create Userdao to simulate database interaction.
 Public classuserdao{ PublicList<user>queryuserlist () {List<User> result =NewArraylist<user>(); //Simulating database queries         for(inti = 1;i < 10; i++) {User User=NewUser (); User.setusername ("Username_" +i); User.setpassword ("Password" +i);                User.setage (i);        Result.add (user); }        returnresult; }}
3. Write userservice to implement the user data manipulation business logic.
@service  Public class userservice{    @Autowired// inject Bean object in spring container    private  Userdao Userdao;      Public List<user> queryuserlist () {        // calls the method in Userdao to query.         returnthis. Userdao.queryuserlist ();    }}
4. Write Springconfig to instantiate the spring container.
@Configuration// This annotation indicates that the class is a spring configuration, equivalent to an XML file.  // Configure the scan package. @ComponentScan (basepackages = "Cn.my.springboot.javaconfig")publicclass  springconfig {    @Bean// through this annotation to indicate that it is a Bean object, equivalent to the <bean> public in XML      Userdao Getuserdao () {        returnnew Userdao ();   Direct new object for presentation.     }}
5. Write test methods to start the spring container.
 Public classTest { Public Static voidMain (string[] args) {//instantiate the spring container with a Java configuration. Annotationconfigapplicationcontext context =NewAnnotationconfigapplicationcontext (springconfig.class); //get the Bean object in the spring containerUserService userservice = Context.getbean (userservice.class); //calling methods in an objectlist<user> list =userservice.queryuserlist ();  for(User user:list) {System.out.println (User.getusername ()+ "|" User.getpassword () + "|"user.getage ()); //Destroy the containerContext.destroy; }    }}

Test results:

You can use Java code as a perfect alternative to XML configuration files.
As for the structure please not clear is the beholder benevolent see.

How to 1.0-springboot Java 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.