Spring4笔记2--Spring的第一个程序
Spring程序开发:
1. 导入jar包(略)
2. 创建Spring配置文件:
Spring 配置文件的文件名可以随意,但 Spring 建议的名称为 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,下面的注册,相当于在代码中写的
ISomeService someService = new SomeServiceImpl();
-->
<bean id="someService" class="com.tongji.service.SomeServiceImpl"/>
</beans>
测试类:
package com.tongji.test; import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; import com.tongji.service.ISomeService;
import com.tongji.service.SomeServiceImpl; public class MyTest { //不使用Spring容器
//代码的问题是:SomeServiceImpl这个类,完全耦合到了测试类中
@Test
public void test01() {
ISomeService someService = new SomeServiceImpl();
someService.doSome();
} //从容器中获取Bean,使Bean类与测试类解耦合了
@Test
public void test02() {
//创建容器
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
ISomeService someService = (ISomeService) ac.getBean("someService");
someService.doSome();
} //从D盘加载配置文件applicationContext.xml
@Test
public void test03() {
//创建容器
ApplicationContext ac =
new FileSystemXmlApplicationContext("d:/applicationContext.xml");
ISomeService someService = (ISomeService) ac.getBean("someService");
someService.doSome();
} //从项目的根下applicationContext.xml
@Test
public void test04() {
//创建容器
ApplicationContext ac =
new FileSystemXmlApplicationContext("applicationContext.xml");
ISomeService someService = (ISomeService) ac.getBean("someService");
someService.doSome();
} //直接使用BeanFactory容器
//ApplicationContext容器:在初始化容器时,就将容器中的所有对象进行了创建,内存占有大,但效率高
//BeanFactory容器:使用时才创建,相反
@Test
public void test05() {
//创建容器
BeanFactory bf =
new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
ISomeService someService = (ISomeService) bf.getBean("someService");
someService.doSome();
}
}
解释:ApplicationContext 用于加载 Spring 的配置文件,在程序中充当“容器”的角色。其实现类有两个:
1. Spring 配置文件存放在项目的类路径下,则使用 ClassPathXmlApplicationContext 实现类进行加载。
2. 若 Spring 配置文件存放在项目根路径或本地磁盘目录中,则使用 FileSystemXmlApplicationContext 实现类进行加载。
BeanFactory 接口对象也可作为 Spring 容器出现。BeanFactory 接口是 ApplicationContext接口的父类。 若要创建 BeanFactory 容器,需要使用其实现类 XmlBeanFactory进行加载。而 Spring 配置文件以资源 Resouce 的形式出现在 XmlBeanFactory 类的构造器参数中。Resouce 是一个接口,其具有两个实现类:
1. ClassPathResource:指定类路径下的资源文件
2. FileSystemResource:指定项目根路径或本地磁盘路径下的资源文件。
Spring4笔记2--Spring的第一个程序的更多相关文章
- Spring的第一个程序
目录 一.Spring概述 1. Spring是什么? 2. IOC控制反转 二.Spring的第一个程序 1. 创建Maven项目 2. 加入maven依赖pom.xml 3. 定义接口和实体类 4 ...
- 借助Maven入手Spring Boot第一个程序
目前网上有不少Spring Boot的入门文章,都很有帮助,本人最近在深入学习Spring Cloud,在搭建第一个Hello World程序时,感觉对于新手而言,介绍文章怎么详细都不为过,因为其中坑 ...
- Hibernate5笔记1--Hibernate简介和第一个程序
Hibernate简介: Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hib ...
- Go 基础学习笔记(3)| 第一个程序 “helloworld”
//第一个程序总要说的清楚才行. //建议先运行起第一个程序实践后,再看后面的具体解答 一.helloworld 编写运行 1.编写源程序,在 ~ /hello/src 编写hello.g ...
- Spring框架第一篇之Spring的第一个程序
一.下载Spring的jar包 通过http://repo.spring.io/release/org/springframework/spring/地址下载最新的Spring的zip包,当然,如果你 ...
- 听翁恺老师mooc笔记(2)-第一个程序--&运算符
使用devC++写hello world 第一步:文件-新建-源代码.然后输入"输出hello world"程序: 注意:写程序时关闭中文输入法,否则语句输入的分号可能会被识别为错 ...
- PyQt4学习笔记1:PyQt4第一个程序
创建一个 PyQt4 一般可以通过很少的步骤完成.通常的方法是用Qt 提供的QtDesigner工具创建界面.使用QtDesigner,可以方便地创建复杂的GUI界面.然后,可以在窗口上创建部件, 添 ...
- Python 自学笔记(二)第一个程序 Hello World
一 打印 Hello world 1,输入 Python “Hello world” 即可 2,脚本文件输出Hello World 在命令行(cmd),输入 python 文件路径+文件名 3,为什么 ...
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- Spring实战第一章学习笔记
Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...
随机推荐
- python 查看与更换工作目录
1. 进入python控制台 2. 查看工作路径,需要导入os包: import os 3. 查看工作路径的命令: os.getcwd() 4. 修改工作路径的命令: os.chdir("d ...
- 014 Java的反射机制
作者:nnngu GitHub:https://github.com/nnngu 博客园:http://www.cnblogs.com/nnngu 简书:https://www.jianshu.com ...
- Day22-Django之Form组件验证
1. Django里面的Form专门用来做验证. 用Form创建一个类,把用户发来的数据放到request.POST里面发给这个类,这个类会帮忙做验证. 返回3个结果:是否验证成功了,所有的正确信息, ...
- Gartner 2018 年WAF魔力象限报告:云WAF持续增长,Bot管理与API安全拥有未来
Gartner 2018 年WAF魔力象限报告:云WAF持续增长,Bot管理与API安全拥有未来 来源 https://www.freebuf.com/articles/paper/184903.ht ...
- python中OS模块操作文件和目录
在python中执行和操作目录和文件的操作是通过内置的python OS模块封装的函数实现的. 首先导入模块,并查看操作系统的类型: >>> import os os.name # ...
- 【省选水题集Day1】一起来AK水题吧! 题目(更新到B)
题解:http://www.cnblogs.com/ljc20020730/p/6937954.html 水题A: [AHOI2001]质数和分解 题目网址: https://www.luogu.or ...
- as, idea 出现 Gradle's dependency cache may be corrupt 错误分析
问题: Error:Failed to open zip file.Gradle's dependency cache may be corrupt (this sometimes occurs af ...
- Codeforces Round #532
以后不放水题了 C.NN and the Optical Illusion 复习一下高中数学即可 $\frac{ans}{ans+r}=\sin \frac{\pi}{n}$ 解方程 #include ...
- Access to a protected network share using Win32 C++
WNetAddConnection2 DWORD WNetAddConnection2A( LPNETRESOURCEA lpNetResource, LPCSTR lpPassword, LPCST ...
- where EXISTS (子查询)多对多中通过中间表查对方列表
用户表A,小组表B,小组和用户是多对多关系,中间有个中间表M 已知 小组 id 即teamId ,想知道这个小组中的用户列表信息,可以如下写sql: select * from A a where E ...