好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

idea创建Spring项目的方法步骤(图文)

spring介绍

spring概述

spring是一个开源框架,spring是2003年兴起的轻量级java开发框架,由rod johnson 在其著作 expert one-on-one j2ee development and design 中阐述的部分理念和原形衍生而来。它是为了解决企业级开发的复杂性而创建的。spring使用基本的javabaen来完成以前只可能由ejb完成的事情,然而spring的用途不仅限于服务器端的开发,从简单性、可测试性、低耦合的角度而言任何java应用都可以在spring中受益。

简单的来说spring就是轻量级的控制反转(ioc)和面向切面(aop)的容器框架。

spring的好处

方便解耦,简化开发

spring就是一个大工厂,专门负责生成bean,可将所有对象,和依赖关系维护由spring管理。

aop编程的支持

spring提供面向切面编程,可以方便的实现对程序进行拦截、运行监控等功能。

声明事务的支持

只需要通过配置就可以对事务进行管理,而无需手动编程。

方便程序的测试

spring对junit支持,可以通过注解方便的测试spring程序。

方便集成各种优秀框架

spring不排斥各种优秀的框架,其内部提供了对各种优秀框架的支持,比如struts2、hibernate、mybatis等。

降低javaee api的使用难度

对java ee 开发中一些难用的api(jdbc、javamail)等都提供了封装 ,使这些api应用难度大大降低。

spring体系结构

spring框架是一个分层架构,它包含一系列的功能要素,并被分为大约20个模块,这些模块分为:

core container、data access/intergration、web、aop(aspect oriented programming)、instrumentation和测试部分,如下图所示:

在项目中的架构

web层:struts2、springmvc

dao层:hibernate、mybatis

spring入门案例

编写流程

创建项目

导入jar

然后点击next

然后点击finish

下面是创建好的项目结构,我创建了bean包,测试类、和配置文件

idea 可以自动生成是spring的配置文件  步骤是在需要生成配置文件的目录上鼠标右键--》new--》xml configuration file--》spring config--》然后起文件名点击finish

下面是写好的测试 person类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

package com.spring.bean;

 

/**

  * @author: 007

  * @date: 2019/1/27/0027 10:20

  * @version 1.0

  * @description: 创建测试用的bean

  */

public class person {

   private string name;    //姓名

   private int age;      //年龄

 

   public person() {

   }

 

   public person(string name, int age) {

     this .name = name;

     this .age = age;

   }

 

   public string getname() {

     return name;

   }

 

   public void setname(string name) {

     this .name = name;

   }

 

   public int getage() {

     return age;

   }

 

   public void setage( int age) {

     this .age = age;

   }

 

   @override

   public string tostring() {

     return "person{" +

         "name='" + name + '\ '' +

         ", age=" + age +

         '}' ;

   }

}

配置spring的核心xml文件

?

1

2

3

4

5

6

7

8

9

10

11

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://HdhCmsTestspringframework.org/schema/beans"

     xmlns:xsi= "http://HdhCmsTestw3.org/2001/xmlschema-instance"

     xsi:schemalocation= "http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd" >

     <!--配置bean

     id:给配置的类起个后续在容器中获取用的id

     class :类所在的路径

     -->

 

   <bean id= "person" class = "com.spring.bean.person" />

</beans>

在程序中读取spring的配置文件来获取bean(bean其实就是一个new好的对象)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package com.spring.test;

 

import com.spring.bean.person;

import org.springframework.context.applicationcontext;

import org.springframework.context.support.classpathxmlapplicationcontext;

 

/**

  * @author: 007

  * @date: 2019/1/27/0027 10:21

  * @version 1.0

  * @description:

  */

public class test {

   public static void main(string[] args) {

     //查询类路径 加载配置文件

     applicationcontext applicationcontext = new classpathxmlapplicationcontext( "beans.xml" );

     //根据id获取bean

     //spring就是一个大工厂(容器)专门生成bean bean就是对象

     person person = (person) applicationcontext.getbean( "person" );

     //输出获取到的对象

     system.out.println( "person = " + person);

   }

}

下面是测试完成后的截图

至此入门案例编写完成

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:http://HdhCmsTestcnblogs测试数据/jungejava/p/10325151.html

查看更多关于idea创建Spring项目的方法步骤(图文)的详细内容...

  阅读:13次