mac之java环境变量配置和maven安装及配置
java环境配置
配置JAVA的环境变量
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环境变量路径:
1. echo $JAVA_HOME
Maven下载安装install
接着到https://maven.apache.org/download.cgi下载Maven安装文件,如apache-maven-3.3.9-bin.tar.gz,然后解压到本地目录
接下来,我们需要设置M2_HOME(IntelliJ默认为M2_HOME)环境变量指向apache-maven-3.3.9,并且把Maven安装目录下的bin/文件夹添加到系统环境变量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
至此,安装完成,我们可以运行以下命令检查Maven安装:
1. echo $M2_HOME
2. mvn –version
环境变量目录:~/.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工程创建
pom.xml文件中,加入如下依赖:
1. <dependency>
2. <groupId>org.springframework</groupId>
3. <artifactId>spring-context</artifactId>
4. <version>4.3.11.RELEASE</version>
5. </dependency>
当创建好对应的目录后,有时候会发现不能新建java文件,这时可以
右键单击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服务......"></property>
9. </bean>
我们创建了两个类,一个User、一个ServerClass;
如果一个类中包含了另一个类的引用,在applicationContext.xml文件中,配置<property>,直接使用ref即可,后面跟的是对应<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. }
测试调用要改下:
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. }