IDEA使用maven搭建spring项目
spring框架
Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。
◆目的:解决企业应用开发的复杂性
◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
◆范围:任何Java应用
Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。
优点
◆JAVA EE应该更加容易使用。
◆面向对象的设计比任何实现技术(比如JAVA EE)都重要。
◆面向接口编程,而不是针对类编程。Spring将使用接口的复杂度降低到零。(面向接口编程有哪些复杂度?)
◆代码应该易于测试。Spring框架会帮助你,使代码的测试更加简单。
◆JavaBean提供了应用程序配置的最好方法。
◆在Java中,已检查异常(Checked exception)被过度使用。框架不应该迫使你捕获不能恢复的异常。
IDEA使用maven搭建spring项目
idea建立spring项目相当方便 , 可以自动生成spring配置文件 , 和自动导入Spring所需jar包.
File—>new—>project—>Maven

选择本地的jdk,下一步

可以根据自己需求填写(没有什么限制)

选择项目存储的位置,在点击完成就可以。此时一个Maven已经建立成功。
下面,根据自己的需求添加spring依赖和jar包
在pom.xml的文件下添加依赖

<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<!--spring的核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<!--log4j的日志文件-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
加入这段代码,idea的右下角会出现让你导包的选项,你可以点击第一个,导入jar包
好的,现在spring的核心包已经导入,下面开始练习。
整体项目结果图

首先,建两个类
MessagesService :消息
MessagePrinter : 打印机
就是使用打印机打印消息,就这么简单。
也就是要MessagePrinter这个类调用MessagesService 类来输出消息。
MessagesService
package hello;
/**
* 打印
*/
public class MessagesService {
// 无参构造函数
public MessagesService() {
super();
System.out.println("MessageService..");
}
public String getMessage(){
return "Hello Word";
}
}
MessagePrinter
package hello;
/**
* 打印机
*/
public class MessagePrinter {
/**
* 无参构造函数
*/
public MessagePrinter() {
super();
System.out.println("MessagePinter..");
}
/**
* 建立和MessageService的关联关系
*/
private MessagesService service;
/**
* 设置service的值
* @param service
*/
public void setService(MessagesService service) {
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
(1)使用传统的调用方法
创建Application类。
Application
package hello;
public class Application {
public static void main(String[] args) {
System.out.println("appliction");
//创建打印机对象
MessagePrinter printer=new MessagePrinter();
//创建消息服务对象
MessagesService service=new MessagesService();
//设置打印机对象的service属性
printer.setService(service);
printer.printMessage();
}
}
点击绿色图标,点击第一个运行
运行结构

已经成功运行,并输出。
(2)spring的方法来进行调用
在resources下建立applicationContext.xml配置文件
applicationContext.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="service" class="hello.MessagesService"></bean>
<bean id="printer" class="hello.MessagePrinter">
<property name="service" ref="service"></property>
</bean>
</beans>
在java的hello下创建Applicationspring类
Applicationspring
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Applicationspring {
public static void main(String[] args) {
System.out.println("applictionspring");
//初始化spring容器
ApplicationContext context;
context = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取MessagePrinter对象
MessagePrinter printer=context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
点击运行
结果

(3)添加log4j.properties日志
log4j.properties
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG
再次运行,会出现各种相关的日志

IDEA使用maven搭建spring项目的更多相关文章
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- Myeclipse下使用Maven搭建spring boot项目
开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...
- Maven 搭建spring boot多模块项目
Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- Maven搭建Spring Security3.2项目详解
本来是打算在上一篇SpringMVC+Hibernate上写的,结果发现上面那篇 一起整合的,结果发现上一篇内容实在是太长了,就另起一篇,这篇主要是采用 Maven搭建Spring+SpringMVC ...
- Maven 搭建 SSM 项目 (oracle)
简单谈一下maven搭建 ssm 项目 (使用数据库oracle,比 mysql 难,所以这里谈一下) 在创建maven 的web项目时,常常会缺了main/java , main/test 两个文件 ...
- 【maven】 在 MyEcplise上使用maven搭建Web项目
二.在My Ecplise上使用Maven搭建Web项目 1.新建一个maven项目 2.create一个简单的骨架 3.就像在ecplise中一样设置项目的以下配置 4.新创建的项目结构如下 ...
- 如何使用maven搭建web项目
博客园注册了有二十多天了,还没有写过博客,今天就发一篇,也便于后面查找笔记. 我个人已经做了几年的java web开发了,由于所在的公司是业务型公司,用的都是一些老旧的稳定技术,很少接触到稍微新点的内 ...
- Spring入门(四):使用Maven管理Spring项目
让我们先回顾下本系列的前3篇博客: Spring入门(一):创建Spring项目 Spring入门(二):自动化装配bean Spring入门(三):通过JavaConfig装配bean 1.为什么要 ...
随机推荐
- xml的建模
xml建模的思路 1.分析需要被建模的文件中有那几个对象 2.每个对象拥有的行为以及属性 3.定义对象从小到大(从里到外) 4.通过23种的设计模式中的工厂模式,解析xml生产出指定对象 建模的好处 ...
- 6.GC垃圾回收算法和垃圾收集器的关系
JAVAGC垃圾回收机制和常见垃圾回收算法 推荐博客:JVM垃圾回收机制和常见垃圾回收算法 JVM的内存结构.垃圾回收算法
- C++ 模板类示例 template class
声明和实现在一个文件中: template<class T> class book { public: book(); ~book(); private: }; template<c ...
- lua 5 流程控制 if
条件判断中,0 表示 true,只有 nil 才是 false if(0) then -- 可以没有括号 print("0 为 true") elseif 1 then print ...
- Spring Boot 2.2.1 正式发布,需特别注意这个注解的使用!
Spring Boot 2.2.1 已于2019年11月7日正式发布. 该版本内容包含110项修复.改进和依赖升级. 如果开发者要从Spring Boot 2.2.0升级到2.2.1的话,这里要特别注 ...
- 第05组团队Github现场编程实战
第05组团队Github现场编程实战 一.组员职责分工 组员 分工 卢欢(组长) 前后端接口设计 严喜 寻找相关资料 张火标 设计并描述界面原型 钟璐英 编写随笔 周华 填写完善文档 古力亚尔·艾山 ...
- 冒泡排序法(C语言)
冒泡排序(Bubble Sort)一种计算机科学领域的较简单的排序算法.它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果顺序(如从大到小.首字母从从Z到A)错误就把他们交换过来.走访元素的工 ...
- SQL Server 迁移数据库 (四)备份和还原
1. 备份 2. 复制 3. 粘贴 4. 还原 截图软件出问题了,估计重启下就好,但是备份还原比较简单,懂的都懂,马上下班了就不贴图了.
- Wwise音频解决方案概述
Wwise(Wave Works Interactive Sound Engine,Wwise基础知识,wiki)是Audiokinetic公司提供的跨平台游戏音频解决方案,有着高效完整工作流和工具链 ...
- svg描边路径动画
svg描边路径动画<pre><!DOCTYPE html><html> <head> <meta charset="UTF-8" ...