【Spring】创建一个Spring的入门程序

3、创建一个Spring的入门程序

简单记录 - Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)- Spring的基本应用

Spring与Spring MVC的关系Spring和Spring MVC两者名字类似,但是两者却有着本质的不同。Spring是一个巨大的容器,可以集成各种技术。Spring MVC是一个Web技术,Spring MVC可以集成到Spring中。用数学上集合的概念来解释,Spring MVC是Spring的一个子集。

创建一个空白maven项目来实现Spring的入门程序

在这里插入图片描述

在这里插入图片描述

在这个项目中,创建一个名为chapter01的Web项目Module

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

手动导入Jar包(注 : spring 需要导入commons-logging进行日志记录 . )

但我们可以利用maven , 他会自动下载对应的依赖项 .

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.23.RELEASE</version>
</dependency>

https://repo.spring.io/release/org/springframework/spring/

在这里插入图片描述

chapter01目录结构

在这里插入图片描述

(2)在src/main/java目录下,创建一个com.awen.ioc包,并在包中创建接口UserDao,然后在接口中定义一个say()方法,代码如下所示。

package com.awen.ioc;

/**
 * @author Liu Awen 
 */
public interface UserDao {
    public void say();
}

(3)在com.awen.ioc包下,创建UserDao接口的实现类UserDaoImpl,该类需要实现接口中的say()方法,并在方法中编写一条输出语句,代码如下所示。

package com.awen.ioc;

/**
 * @author Liu Awen
 */
public class UserDaoImpl implements  UserDao{
    public void say(){
        System.out.println("userDao say hello World!");
    }
}

(4)在src/main/resources目录下,创建Spring的配置文件applicationContext.xml,并在配置文件中创建一个id为userDao的bean,如文件所示。文件 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">
    <!--将指定类配置给Spring,让Spring创建其对象的实例    -->
    <bean id="userDao" class="com.awen.ioc.UserDaoImpl"></bean>
</beans>

在文件 applicationContext.xml中,前面几行代码是Spring的约束配置。该配置信息不需要手写,我们可以去Spring官网找

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-basics

在这里插入图片描述

也可以在Spring的帮助文档中找到。

快速获取配置文件的约束信息在Spring的配置文件中,包含了很多约束信息,如果我们自己动手去编写,不但浪费时间,还容易出错。其实,在Spring的帮助文档中,就可以找到这些约束信息,具体的获取方法如下。打开Spring解压文件夹中的docs目录,在spring-framework-reference文件夹下打开html文件夹,并找到index.html文件,如图所示。

在这里插入图片描述
图Spring的参考文件目录

使用浏览器打开index.html后,点击Core

在这里插入图片描述

在浏览器页面的下的1.2.1. 小节Configuration Metadata中,即可找到配置文件的约束信息,如图所示。

在这里插入图片描述
配置文件的约束信息在图中,标记处的配置信息就是Spring配置文件的约束信息。我们只需将标注处的信息复制到项目的配置文件中使用即可。

第7行代码表示在Spring容器中创建一个id为userDao的Bean实例,其中class属性用于指定需要实例化Bean的类。

(5)在src/test/java下,创建测试类TestIoC,并在类中编写main()方法。在main()方法中,需要初始化Spring容器,并加载配置文件,然后通过Spring容器获取userDao实例(即Java对象),最后调用实例中的say()方法,如文件所示。文件 TestIoC.java

import com.awen.ioc.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author Liu Awen 
 */
public class TestIoC {
    public static void main(String[] args) {
        // 1. 初始化Spring容器,加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 通过容器获取userDao实例
        UserDao userDao = (UserDao)applicationContext.getBean("userDao");
        // 3.调用实例中的say()方法
        userDao.say();
    }
}

执行程序后,控制台的输出结果如图所示。

D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=8142:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\JavaEE-enterprise-application-development-tutorial\chapter01\target\test-classes;D:\IdeaProjects\JavaEE-enterprise-application-development-tutorial\chapter01\target\classes;D:\Environments\apache-maven-3.6.2\maven-repo\junit\junit\4.11\junit-4.11.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-webmvc\5.2.3.RELEASE\spring-webmvc-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-aop\5.2.3.RELEASE\spring-aop-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-beans\5.2.3.RELEASE\spring-beans-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-context\5.2.3.RELEASE\spring-context-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-core\5.2.3.RELEASE\spring-core-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-jcl\5.2.3.RELEASE\spring-jcl-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-expression\5.2.3.RELEASE\spring-expression-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-web\5.2.3.RELEASE\spring-web-5.2.3.RELEASE.jar TestIoC
userDao say hello World!

Process finished with exit code 0

从运行结果可以看出,控制台已成功输出了UserDaoImpl类中的输出语句。userDao say hello World!

在TestIoc的main()方法中,并没有通过new关键字来创建UserDao接口的实现类对象,而是通过Spring容器来获取的实现类对象,这就是Spring IoC容器的工作机制。

Spring容器来完成。

问题:

Exception in thread "main" java.lang.ClassCastException: class com.awen.ioc.UserDaoImpl cannot be cast to class com.awen.ioc.UserDao (com.awen.ioc.UserDaoImpl and com.awen.ioc.UserDao are in unnamed module of loader 'app')
	at com.awen.ioc.TestIoC.main(TestIoC.java:13)
public class UserDaoImpl {
    public void say(){
        System.out.println("userDao say hello World!");
    }
}

解决原来是我写UserDaoImpl没有implements UserDao接口 , 那就加上吧。搞定

public class UserDaoImpl implements  UserDao{
    public void say(){
        System.out.println("userDao say hello World!");
    }
}
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值