您好,登錄后才能下訂單哦!
這篇文章主要介紹IDEA+Maven創建Spring項目的方法步驟,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
第一步:在IDEA點擊new -> project
左側選擇Maven,直接點擊Next。第一次使用IDEA的朋友,頂部還要選擇Project SDK路徑,就是Java的安裝路徑。
這里隨便填一下之后點擊Next
選擇項目存放路徑,或者保持默認,點擊Finish,來到工程頁面之后,在項目文件夾上右鍵并選擇Add Framework Support
在這個頁面找一下Spring,打鉤。點擊OK
完成后,會下載Spring的jar包,并存放在項目的lib目錄下。
下載完成之后創建一個簡單的項目。
四個Java文件一個appContext.xml我貼在這里
public class Student { private String name; private Homework homework; public Student(){} public Student(String name, Homework homework) { this.name = name; this.homework = homework; } public void doHomeWork(){ System.out.println(homework.getContent()); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Homework getHomework() { return homework; } public void setHomework(Homework homework) { this.homework = homework; } }
public class Homework { private String content; public Homework(){} public Homework(String content) { this.content = content; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
public class Homework { private String content; public Homework(){} public Homework(String content) { this.content = content; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
import java.util.Date; public class CheckNowTime { public void beforDoHomework(){ System.out.println(new Date(System.currentTimeMillis())); } }
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml"); Student student = context.getBean(Student.class); System.out.println(student.getName()+"準備做作業了"); student.doHomeWork(); context.close(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 讓Spring管理 Student bean --> <bean id="student" class="demo1.Student"> <property name="name" value="小明"></property> <property name="homework" ref="homework"></property> </bean> <!-- 讓Spring管理Homework bean--> <bean id="homework" class="demo1.Homework"> <property name="content" value="how to calc 3+2 ?"></property> </bean> <!-- 切面定義--> <bean id="checktime" class="demo1.CheckNowTime"></bean> <aop:config> <aop:aspect ref="checktime"> <aop:pointcut id="dohomework" expression="execution(* *.doHomeWork(..))"/> <aop:before pointcut-ref="dohomework" method="beforDoHomework"></aop:before> </aop:aspect> </aop:config> </beans>
因為例子中使用Spring AOP,所以還需要引入一個jar包。我的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> </dependencies> </project>
然后你就可以運行自己的Spring項目啦,main方法在App類里面。
以上是IDEA+Maven創建Spring項目的方法步驟的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。