您好,登录后才能下订单哦!
这篇文章主要介绍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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。