Java Handy Notes 8 package, environment variables and access control and MAVEN profile to implement multi-environment packaging _java

Source: Internet
Author: User
Tags first row

One, Java in the package

Java uses packages to organize code so that the code structure of large projects is clear, and the package is a namespace division, that is, different packages can have the same name of the class, only in front of the class name and the package name can distinguish them.

Package XXX must be located in the first row of the Java file except for the comment, indicating which package the class belongs to in the current file, and if there is no Package statement, the class in the file belongs to the default package.

Import XXX is used to import classes in the current Java file that are not part of the current package, so that they can be used in the current file.

II. environment Variables in Java

1.path

Environment variables are actually a set of variables (nonsense) that provide parameters to systems and applications. For example: path, which tells the system and application where some of the prerequisite programs are stored, such as where you want to run the ipconfig command, where the system is going to find it, or by the path variable, to find the location that it stores, until it is found.

In Java, when we run a Java program and enter Java XXX in the console, where does the Java application (EXE file) look? The system has to go through path so that the directory containing the Java application is copied to the PATH environment variable, so that after running Java xxx, "Java" is not an internal or external command, nor a running program or batch file. "Up.

2.classpath

Say the path, and then say classpath, solve the problem of Java This command, and Javac This command of the problem, here is involved in the Java package mechanism.

Java programs are made up of a class that can be called from one class to another, and there are classes that have been defined by the Java language, so where to find them when the program uses these classes, this requires CLASSPATH the environment variable to guide the maze.

Summary: Classpath indicates the class lookup path, and if the class is not packaged, you need to add the directory to the Classpath, and if the class is packaged as a jar file, you need to write the actual name of the jar file clearly.

Classpath is generally set to ".; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar; "

Where. Represents the current directory, and%java_home% represents the path to the JAVA jdk. The main dt.jar are the various control classes in the swing package. The Tools.jar is a variety of tool classes.

When the compiler encounters the import statement, it starts looking in the directory contained in the CLASSPATH.

(Reference: http://www.linuxidc.com/Linux/2012-01/52713.htm)

3.java_home

Indicates the path to the JDK

Third, access rights control

1. Access control for class members

Access control character: public/protected/package access (no keyword is default)/private

Class itself

Subclasses in the same package

Subclasses in different packages

Non-subclasses in the same package

Non-subclasses in different packages

Anyway

Public

OK

OK

OK

OK

OK

All can

Private

OK

No

No

No

No

Visible only to itself

Default

OK

OK

No

OK

No

Visible only with Package

Protected

OK

OK

OK

OK

No

Subclasses or the same package is visible

2. Access control permissions for classes

Class has only two types of access control permissions: Default (that is, package access control rights) and public

Here's a little time, then we'll introduce MAVEN profile for multi-environment packaging

Project development needs to have multiple environments, generally for development, testing, advance, formal 4 environments, through MAVEN can be implemented in different environments packaged deployment, the command is:
MVN Package-p Dev

where "dev" for the environment variable ID, you can define their own, I defined the name is: Dev,qa,pre,prod, specifically in the Pom.xml configuration as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 http:// 
      Maven.apache.org/maven-v4_0_0.xsd "> ... <profiles> <profile> <id>dev</id> 
        <properties> <env>dev</env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile&gt 
      ; <id>qa</id> <properties> <env>qa</env> </properties> </ profile> <profile> <id>pre</id> <properties> <env>pre</env > </properties> </profile> <profile> <id>prod</id> <pro Perties> <env>prod</env> </properties> </profile> </profiles> ... <build> <filters& 
      Gt  
        <filter>config/${env}.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> &L  T;/resource> </resources> ... </build> </project>

1.profiles defines the variable ID for each environment
2.filters, where the environment variable in the address is the value
3 defined in the profile above. The resources in resources are defined in which directories the files will be replaced by the variables defined in the configuration file, generally we will put the project's configuration file under Src/main/resources, such as Db,bean, The variables used inside the package are replaced with a fixed value based on the variable configuration in the filter.

Related Article

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.