IDEA+Maven創(chuàng)建Spring項(xiàng)目的實(shí)現(xiàn)步驟
這篇隨筆搭建的工程是普通的Spring工程,用于學(xué)習(xí)Spring框架,如果要搭建SpringMVC工程,可以參考另一篇
第一步:在IDEA點(diǎn)擊new -> project
左側(cè)選擇Maven,直接點(diǎn)擊Next。第一次使用IDEA的朋友,頂部還要選擇Project SDK路徑,就是Java的安裝路徑。
這里隨便填一下之后點(diǎn)擊Next
選擇項(xiàng)目存放路徑,或者保持默認(rèn),點(diǎn)擊Finish,來(lái)到工程頁(yè)面之后,在項(xiàng)目文件夾上右鍵并選擇Add Framework Support
在這個(gè)頁(yè)面找一下Spring,打鉤。點(diǎn)擊OK
完成后,會(huì)下載Spring的jar包,并存放在項(xiàng)目的lib目錄下。
下載完成之后創(chuàng)建一個(gè)簡(jiǎn)單的項(xiàng)目。
四個(gè)Java文件一個(gè)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()+"準(zhǔn)備做作業(yè)了"); 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>
因?yàn)槔又惺褂肧pring AOP,所以還需要引入一個(gè)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>
然后你就可以運(yùn)行自己的Spring項(xiàng)目啦,main方法在App類里面。
到此這篇關(guān)于IDEA+Maven創(chuàng)建Spring項(xiàng)目的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)IDEA+Maven創(chuàng)建Spring內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- IDEA2020.2創(chuàng)建springboot項(xiàng)目卡死在reading maven project的問(wèn)題
- Spring Boot maven框架搭建教程圖解
- Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟
- SpringBoot使用Maven打包異常-引入外部jar的問(wèn)題及解決方案
- SpringBoot中maven項(xiàng)目打成war包部署在linux服務(wù)器上的方法
- Maven搭建springboot項(xiàng)目的方法步驟
- SpringBoot Maven Clean報(bào)錯(cuò)解決方案
- 基于Spring的Maven項(xiàng)目實(shí)現(xiàn)發(fā)送郵件功能的示例
- Springboot基于maven打包分離lib及resource
相關(guān)文章
Java中的HashMap為什么會(huì)產(chǎn)生死循環(huán)
這篇文章主要介紹了Java中的HashMap為什么會(huì)產(chǎn)生死循環(huán),HashMap?死循環(huán)是一個(gè)比較常見(jiàn)、比較經(jīng)典的問(wèn)題,下面文章我們就來(lái)徹底理解死循環(huán)的原因。需要的小伙伴可以參考一下2022-05-05SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件詳解
Spring的出現(xiàn)是為了簡(jiǎn)化 Java 程序開(kāi)發(fā),而 SpringBoot 的出現(xiàn)是為了簡(jiǎn)化 Spring 程序開(kāi)發(fā),這篇文章主要介紹了SpringBoot項(xiàng)目創(chuàng)建使用+配置文件+日志文件,需要的朋友可以參考下2023-02-02Java中循環(huán)冗余校驗(yàn)(CRC32)的實(shí)現(xiàn)
CRC校驗(yàn)實(shí)用程序庫(kù)在數(shù)據(jù)存儲(chǔ)和數(shù)據(jù)通訊領(lǐng)域,為了保證數(shù)據(jù)的正確,就不得不采用檢錯(cuò)的手段,下面這篇文章主要給大家介紹了關(guān)于Java中循環(huán)冗余校驗(yàn)(CRC32)實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-10-10Mybatis配置文件之動(dòng)態(tài)SQL配置備忘錄
這篇文章主要介紹了Mybatis配置文件之動(dòng)態(tài)SQL配置備忘錄的相關(guān)資料,需要的朋友可以參考下2017-05-05基于Java實(shí)現(xiàn)計(jì)數(shù)排序,桶排序和基數(shù)排序
這篇文章主要為大家詳細(xì)介紹了計(jì)數(shù)排序,桶排序和基數(shù)排序的多種語(yǔ)言的實(shí)現(xiàn)(JavaScript、Python、Go語(yǔ)言、Java),感興趣的小伙伴可以了解一下2022-12-12