In spring code, resolve Docker environment variable delivery issues

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1,docker variable

Define variables in Docker and use the Echo $Value to display directly on the command line,
This can be injected directly into the code using spring.
PHP has handy code to get the environment variables directly, but Java is a bit of a hassle.
Just use spring's value directly.

2, Code

For example, a spring user login action.
It uses Docker to configure a default admin login name and password.
In spring it can be written like this:

import org. Springframework. Beans. Factory. Annotation. Value;import org. Springframework. Web. Bind. Annotation. Requestmapping;import org. Springframework. Web. Bind. Annotation. Requestmethod;import org. Springframework. Web. Bind. Annotation. Requestparam;import org. Springframework. Web. Bind. Annotation. Restcontroller;@RestController @requestmapping ("/user") public class Useraction {@Value ("${default.admin.username}") Private String UserName;@Value ("${default.admin.password}") Private String Password;@RequestMapping (value ="/login", method = Requestmethod. POST) Public Resultjson Channelkey (@RequestParam (value ="UserName", DefaultValue ="") String UserName, @RequestParam (value ="Password", DefaultValue =""String password) {...}

3, simultaneous configuration under Spring

It is not possible to have such a code alone.
Spring is required to inject the configuration into it.
Spring configuration: Add such a line.

    <context:propertyfile-encoding="UTF-8"/>

4, summary

And two previously written articles:
"Use junit&spring to modify the environment variables of the system to solve the Docker program test Problem"
http://blog.csdn.net/freewebsys/article/details/52781896
"Docker under development, modify environment variable start jetty"
http://blog.csdn.net/freewebsys/article/details/52782032
Explore how Java projects are migrated to Docker environment projects.
There are not many changes to the code. Docker deployment is easy and resource-saving is good. is the future trend.

The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/51089323 not allowed to reprint without the Bo master.
Bo main address is: Http://blog.csdn.net/freewebsys

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.