IDEA+Maven創(chuàng)建Spring項目的實現(xiàn)步驟
這篇隨筆搭建的工程是普通的Spring工程,用于學習Spring框架,如果要搭建SpringMVC工程,可以參考另一篇
第一步:在IDEA點擊new -> project

左側(cè)選擇Maven,直接點擊Next。第一次使用IDEA的朋友,頂部還要選擇Project SDK路徑,就是Java的安裝路徑。

這里隨便填一下之后點擊Next

選擇項目存放路徑,或者保持默認,點擊Finish,來到工程頁面之后,在項目文件夾上右鍵并選擇Add Framework Support

在這個頁面找一下Spring,打鉤。點擊OK

完成后,會下載Spring的jar包,并存放在項目的lib目錄下。
下載完成之后創(chuàng)建一個簡單的項目。

四個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()+"準備做作業(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>
因為例子中使用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類里面。
到此這篇關(guān)于IDEA+Maven創(chuàng)建Spring項目的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)IDEA+Maven創(chuàng)建Spring內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- IDEA2020.2創(chuàng)建springboot項目卡死在reading maven project的問題
- Spring Boot maven框架搭建教程圖解
- Idea+maven搭建SSH(struts2+hibernate5+spring5)環(huán)境的方法步驟
- SpringBoot使用Maven打包異常-引入外部jar的問題及解決方案
- SpringBoot中maven項目打成war包部署在linux服務(wù)器上的方法
- Maven搭建springboot項目的方法步驟
- SpringBoot Maven Clean報錯解決方案
- 基于Spring的Maven項目實現(xiàn)發(fā)送郵件功能的示例
- Springboot基于maven打包分離lib及resource
相關(guān)文章
Java入門基礎(chǔ)之常規(guī)的命名方法和變量的值及其引用
這篇文章主要介紹了Java的命名方法和變量的值及其引用,是Java入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解
這篇文章主要為大家介紹了Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法
Netty提供異步的、基于事件驅(qū)動的網(wǎng)絡(luò)應(yīng)用程序框架,用以快速開發(fā)高性能、高可靠性的網(wǎng)絡(luò) IO 程序,是目前最流行的 NIO 框架,這篇文章主要介紹了SpringBoot 整和 Netty 并監(jiān)聽多端口,需要的朋友可以參考下2023-10-10
Spring?Security認證器實現(xiàn)過程詳解
一些權(quán)限框架一般都包含認證器和決策器,前者處理登陸驗證,后者處理訪問資源的控制,這篇文章主要介紹了Spring?Security認證器實現(xiàn)過程,需要的朋友可以參考下2022-06-06
Mybatis實體類屬性與數(shù)據(jù)庫不一致解決方案
這篇文章主要介紹了Mybatis實體類屬性與數(shù)據(jù)庫不一致解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10

