首先要把eclipse中的maven配置好就可以创建maven项目了
然后使用maven创建项目过程中可能会碰见以下正在创建的情况,在下面输入Y就可以创建成功,如果不输入就停在这里创建不成功。
然后创建成功maven项目后可能会出现pom.xml文件发生Cannot access defaults field of Properties
遇见这种情况,可以在pom.xml 的plugins添加如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
下一步添加spring的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
</dependency>
最后创建beans.xml文件(放在src/main/resources)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="com.lcj.bean.HelloWorld"></bean>
//com.lcj.bean为src/main/java下的包
</beans>
测试
package com.lcj.bean;
public class HelloWorld {
public void sayHello(){
System.out.println("helloworld");
}
}
package com.lcj.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
HelloWorld he =(HelloWorld)ac.getBean("helloWorld");
he.sayHello();
System.out.println("啦啊");
}
}