初学spring idea中创建maven项目
首先,下载maven仓库
下载好过后,配置环境变量:~/bin 写入path环境变量中。( ~ 表示maven3的加压路径)
把配置文件settings.xml中的镜像换成阿里的镜像,文件系统中有两个配置文件,建议只修改本地文件(即C盘下面的)
配置如图:
接下来就是再idea中创建:
然后名字那些自己去,之后点next。
然后点finish
配置上面说到的文件夹:创建maven项目的时候就是根据下面的原型创建的,所以说我们现在就是相当于再修改模型。
pom.xml也可以修改,也在那个压缩文件中,根据自己需要修改,修改原理一样。以后需要改的时候自己就知道怎么改了。就不演示了。
最后就是添加spring的依赖,有两种方法:
1、自己手写
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.2</version>
</dependency>
2、去官网复制,搜索想添加依赖,下面有模板。
最后一步,创建spring 配置文件,名字随意
在配置文件中添加一行:就是告诉spring要扫描的包
<context:component-scan base-package=“cdu.zyf”></context:component-scan>
在file-project structure–modules中:
至此项目配置完成!可以测试一下
源码
@Service
public class SpringTest {
@Test
public void testSpring(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringTest springTest = (SpringTest) context.getBean("springTest");
springTest.hello();
}
public void hello(){
System.out.println("hello,nls");
}
}