idea使用maven新建spring项目
idea使用maven新建spring项目
1.新建项目maven,
2.next之后给项目起名
3. 改造成我们常用的maven结构,在main下new directory >java包和resources包,把java包标记成根路径,resources标记成资源根路径包
4.pom.xml中导入spring的pom,如下,可以现在properties中定义好spring.version,这里为了测试方便,导入了lombok
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.1.6.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
引入之后会自动把相关的依赖包传递进来,maven的图如下
5.新建实体类,在resources文件中new加入spring的xml配置
@Data
public class Student {
private String name;
private Integer age;
}
6.配置xml
<?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="student" class="com.zy.entity.Student">
<property name="name" value="周杨"/>
<property name="age" value="25"/>
</bean>
</beans>
7.编写测试类
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Student studnet = (Student) applicationContext.getBean("student");
System.out.println(studnet);
只是简单编写一个spirng的maven项目,spring的其他用法可以参考的的spring博客。这个例子后面加入了很多spring的其他知识,完整的传在我的github上面,方便下载使用。
亲测能运行,代码github地址:

浙公网安备 33010602011771号