mac之java環(huán)境變量配置和maven安裝及配置
java環(huán)境配置
配置JAVA的環(huán)境變量
1. JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
2. PATH=$JAVA_HOME/bin:$PATH:
3. CLASSPAHT=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
5. export JAVA_HOME
6. export PATH
7. export CLASSPATH
輸出java環(huán)境變量路徑:
1. echo $JAVA_HOME
Maven下載安裝install
接著到https://maven.apache.org/download.cgi下載Maven安裝文件略板,如apache-maven-3.3.9-bin.tar.gz,然后解壓到本地目錄
接下來婆硬,我們需要設(shè)置M2_HOME(IntelliJ默認(rèn)為M2_HOME)環(huán)境變量指向apache-maven-3.3.9,并且把Maven安裝目錄下的bin/文件夾添加到系統(tǒng)環(huán)境變量PATH中去:
vim打開/Users/developer/.bash_profile(~/.bash_profile)文件加入如下代碼:
1. export M2_HOME=/usr/local/apache-maven-3.3.9
2. export PATH=$PATH:$M2_HOME/bin
輸入命令以使bash_profile生效
1. $ source ~/.bash_profile
至此劲室,安裝完成仿耽,我們可以運(yùn)行以下命令檢查Maven安裝:
1. echo $M2_HOME
2. mvn –version
環(huán)境變量目錄:~/.bash_profile
.bash_profile文件
1. JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home
2. CLASSPAHT=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar PATH=$JAVA_HOME/bin:$PATH:
3. export JAVA_HOME
4. export CLASSPATH
5. export PATH
7. export M2_HOME=/usr/local/apache-maven-3.3.9
8. export PATH=$PATH:$M2_HOME/bin
9. export PATH=$PATH:/usr/local/apache-tomcat-8.0.26/bin
11. [-s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
maven工程創(chuàng)建
pom.xml文件中岩榆,加入如下依賴:
1. <dependency>
2. <groupId>org.springframework</groupId>
3. <artifactId>spring-context</artifactId>
4. <version>4.3.11.RELEASE</version>
5. </dependency>
當(dāng)創(chuàng)建好對應(yīng)的目錄后,有時(shí)候會(huì)發(fā)現(xiàn)不能新建java文件恭金,這時(shí)可以
右鍵單擊java文件夾操禀,選擇Sources即可。
工程目錄:
Spring HelloWorld案例
Spring的配置文件applicationContext.xml文件一般放在resources文件下横腿。
配置文件寫法:
1. <bean id="user" class="com.renboan.beans.User">
2. <property name="name" value="Hello World ..."></property>
3. <property name="server" ref="serverClass">
4. </property>
5. </bean>
7. <bean id="serverClass" class="com.renboan.beans.ServerClass">
8. <property name="iPhoneServer" value="iPhone server服務(wù)......"></property>
9. </bean>
我們創(chuàng)建了兩個(gè)類颓屑,一個(gè)User、一個(gè)ServerClass蔑水;
如果一個(gè)類中包含了另一個(gè)類的引用邢锯,在applicationContext.xml文件中,配置<property>搀别,直接使用ref即可丹擎,后面跟的是對應(yīng)<bean id="serverClass">的id
1. public class Main {
2. public static void main(String[] args) {
3. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
4. User obj = (User) context.getBean("user");
5. ServerClass serverClass = obj.getServer();
6. serverClass.runServer();
7. System.out.println(serverClass);
8. obj.workRun();
9. }
10. }
ApplicationContext類一般要寫成單例類的方式:
1. final public class ApplicationContextUtil {
2. private static ApplicationContext applicationContext = null;
3. private ApplicationContextUtil(){}
5. static {
6. applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
7. }
8. public static ApplicationContext getApplicationContext(){
9. return applicationContext;
10. }
11. }
測試調(diào)用要改下:
1. public class Main {
2. public static void main(String[] args) {
3. ApplicationContext context = ApplicationContextUtil.getApplicationContext();
4. User obj = (User) context.getBean("user");
5. ServerClass serverClass = obj.getServer();
6. serverClass.runServer();
7. System.out.println(serverClass);
8. obj.workRun();
9. }
10. }