idea新建项目写html5,IDEA下创建Spring项目

IDEA下创建Spring项目

IDEA下创建JavaSESpring项目示例

1、创建项目

第4步:是否自动创建空的Spring容器配置文件,默认文件名是spring-config.xml。勾不勾选都行,如果没勾选,后面要自己创建。

第5步:设置如何添加Spring要用到的库?

Uselibrary:从磁盘上选择要添加的Spring的jar库(需要已下载Spring)

Download:由IDEA自动下载到项目的lib文件夹中,并自动添加到项目的类路径中。

Setuplibrarylater:后面再设置

下载了Spring的可以选第一个,否则选第二个。

2、在src下新建一个包my_package,包下新建一个接口Person,并定义一个抽象方法say()

1publicinterfacePeople{

2publicvoidsay();

3}

3、在包my_package下新建一个Student类,实现Person接口。

1publicclassStudentimplementsPeople{

2@Override

3publicvoidsay(){

4System.out.println("老师好!");

5}

6}

当前,可以省略第2步,不要Person接口,直接写Student类也行。

4、在src下的Spring容器配置文件中,添加要装载的Bean。

1<?xmlversion ="1.0"encoding="UTF-8"?>

2

3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

5

6

7

8

9

如过创建项目时勾选了“Createemptyspring-config.xml”,则配置文件是spring-config.xml,当然我们可以修改文件名。

如果没有勾选,则先在src下新建。对src单击右键:

文件名一般用applicationContext.xml或者beans.xml。

5、在包my_mypage下新建一个类Test用于测试

1publicclassTest{

2publicstaticvoidmain(String[]args){

3ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");//注意要换为你的Spring配置文件

4Studentstudent=(Student)applicationContext.getBean("student");

5//Studentstudent=applicationContext.getBean("student",Student.class)

6student.say();

7}

8}

可以看到,Spring不是像传统的Java一样new出一个对象,而是由Spring容器创建对象,由getBean()获取指定的对象。

6、配置测试环境,选择Application。

7、运行,可以看到控制台打印出“老师好!”。

说明:只有在Spring的配置文件中设置的了Bean,才会被Spring容器创建、管理,才可以通过getBean()获取对应的对象。

创建JavaWebSpring项目实例

1、创建项目

对第6步,建议选择“Download”,因为自己去下项目所需要的jar包,然后添加进来,很容易漏掉一些jar包,尤其是整合多个框架时,所需的jar包很多,自己添加很容易漏。

2、在src下新建包beans,在beans包下新建一个Student类(JavaBean),并写一个say()方法

1publicclassStudent{

2publicStringsay(){

3return"老师好!";

4}

5}

3、在src新建一个SpringConfig文件(Spring容器的配置文件),文件名为applicationContext.xml。装配我们刚才写的JavaBean。

1<?xmlversion ="1.0"encoding="UTF-8"?>

2

3xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

4xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">

5

6

7

8

4、在index.jsp文件中使用装配好的JavaBean。部分代码:

1

2

3ClassPathXmlApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");

4Studentstudent=applicationContext.getBean("student",Student.class);

5%>

6

7

5、配置Tomcat服务器

配置好以后,Tomcat图标上的红x会消失,如果红x还在,说明Tomcat的配置有问题。

6、调试,看到浏览器页面中显示“老师好!”。

如果看到控制台Tomcat在部署,但一直弹出浏览器页面,可能是项目依赖的jar包较多,复制到部署目录的lib下需要时间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值