Spring的下载及目录结构
1、使用浏览器访问Spring的官方下载地址(官网地址:Spring FrameworkLevel up your Java code and explore what Spring can do for you.https://spring.io/projects/spring-framework),访问org→springframework→spring路径,就可以看到Spring框架各个版本压缩包的下载链接,单击链接下载该文件。
2.下载完成后,将文件解压得到一个名为spring-framework-5.2.8.RELEASE的文件夹。
Spring的入门程序
1.在IDEA中创建名称为chapter06的Maven项目,然后在pom.xml文件中加载需使用到的Spring四个基础包以及Spring依赖包
2.在chapter06项目的src/main/java目录下中创建com.itheima包,并在该包下创建名为HelloSpring的类。在HelloSpring类中定义userName属性和show()方法。
3.在chapter06项目的src/main/resources目录下新建applicationContext.xml文件作为HelloSpring类的配置文件,并在该配置文件中创建id为helloSpring的Bean。
4.打开Spring目录结构下的docs文件夹,在spring-framework-reference文件夹的Spring的参考文件目录下找到index.html文件。
5.使用浏览器打开index.html。单击“Core”链接进入Core Technologies页面,单击1.The IoC container→1.2.Container overview→1.2.1.Configuration Metadata目录,可以查看配置文件的约束信息。
6.在chapter06项目的com.itheima文件夹下创建测试类TestHelloSpring,在main()方法中初始化Spring容器并加载applicationContext.xml配置文件,通过Spring容器获取HelloSpring类的helloSpring实例,调用HelloSpring类中的show()方法在控制台输出信息。
7.在IDEA中启动测试类TestHelloSpring,控制台会输出结果。
依赖注入的类型
1、编写用户类:在项目chapter06的com.itheima包下新建User1类,在User1类中定义id、name和password三个属性。
2、获取Bean的配置信息:在chapter06项目的src文件夹下创建applicationContext-User.xml文件,在该文件中添加User1类的配置信息。
3.编写测试类:在项目chapter06的com.itheima包下创建测试类TestUser1。
4.编写用户类:在项目chapter06的com.itheima包下新建User2类,在User2类中定义id、name和password三个属性。
5.获取Bean的配置信息:在chapter06项目的src文件夹下创建applicationContext-User2.xml文件,并在该文件的bean元素中添加User2类的配置信息。
6.编写测试类:在项目chapter06的com.itheima包下创建测试类TestUser2。
7(1).项目chapter06的com.itheima包下新建dao包,在dao包下创建接口UserDao.java,在UserDao.java接口中添加方法login(),用于实现登录功能。
7(2).编写DAO层的实现类:在com.itheima.dao包下创建impl包,在impl包下创建UserDao接口的实现类UserDaoImpl,在UserDaoImpl类中实现login()方法。
8(1).编写Service层:在项目chapter06的com.itheima包下新建service包,在service包下创建接口UserService.java,在接口中添加方法login()。
8(2).编写Service层实现类:在com.itheima.service包下创建impl包,在impl包下创建UserService接口的实现类UserServiceImpl,在UserServiceImpl类中实现login()方法。
9.编写applicationContext.xml配置文件:使用<bean>元素添加创建的UserDaoImpl类和UserServiceImpl类的实例,并配置其相关属性。
10.编写测试类:在com.itheima包中新建测试类TestSpring。
完。