springmvc笔记(来自慕课网)
1.准备工作:springmvc相关的jar包.
2.这里我们先用eclipse来操作.
首先看一个接口编程,后面的所有知识点都是通过这个接口编程引出的.
OneInterface.java
package gys; public interface OneInterface {
String hello(String world);
}
OneInterfaceImpl.java
package gys; public class OneInterfaceImpl implements OneInterface{ @Override
public String hello(String world) {
return "从接口返回的是:"+world;
} }
Run.java
package gys; public class Run{ public static void main(String[] args) {
OneInterface oif=new OneInterfaceImpl();
System.out.println(oif.hello("思思博士"));
} }
这个地方可以通过接口的形式跑起来了.
下面看看使用springmc方式如何来跑起来这个项目
因为我们不是web项目,没有通过配置web.xml来配置,读取springmvc配置文件.
只能手写读取配置文件.
getBeanBase.java
package gys; import org.springframework.context.support.ClassPathXmlApplicationContext;
//创建springmvc容器,获取配置文件中的bean.
public class GetBeanBase {
private ClassPathXmlApplicationContext context;
private String springXmlpath;
public GetBeanBase(){}; public GetBeanBase(String springXmlPath){
this.springXmlpath=springXmlPath;
} public void start(){
if(springXmlpath.equals("")||springXmlpath==null||springXmlpath.isEmpty()){
springXmlpath="classpath*:spring-*.xml";
}
try {
//创建spring容器
context=new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
} catch (Exception e) {
e.printStackTrace();
}
} public void end(){
context.destroy();
} @SuppressWarnings("unchecked")
protected <T extends Object> T getBen(String beanId){
return (T) context.getBean(beanId);
} protected <T extends Object> T GetBeanBase(Class<T> clazz){
return context.getBean(clazz);
}
}
spring-ioc.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="oneInterface" class="gys.OneInterfaceImpl"></bean>
</beans>
Run.java
package gys; public class Run extends GetBeanBase{
public Run(){
super("classpath*:spring-ioc.xml");
start();
}
public void testHello(){
OneInterface oneInterface=super.getBen("oneInterface");
System.out.println(oneInterface.hello("传入的参数"));
end(); } public static void main(String[] args) {
Run run=new Run();
run.testHello();
} }
通过这个方式也是可以做到同样的输出.这里的GetBeanBase在后面很多地方使用.
spring注入:在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
常用的两种注入方式:
设置注入
构造注入
1.设置注入:
InjectionDao.java
package gys.dao; public interface InjectionDAO {
void save(String info);
}
InjectionDAOImpl.java
package gys.dao; public class InjectionDAOImpl implements InjectionDAO{ @Override
public void save(String info) {
System.out.println("保存数据:"+info); } }
InjectionService.java
package gys.service; public interface InjectionService {
public void save(String info);
}
InjectionServiceImpl.java
package gys.service; import gys.dao.InjectionDAO; public class InjectionServiceImpl implements InjectionService{ private InjectionDAO injectionDAO; //设置注入,这里的set方法spring会自动调用,无需手动调用
public void setInjectionDAO(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
} @Override
public void save(String info) {
System.out.println("service接受参数:"+info);
info=info+":"+this.hashCode();
injectionDAO.save(info);
}
}
spring-ioc.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 设置注入 -->
<bean id="injectionService" class="gys.service.InjectionServiceImpl">
<!--InjectionServiceImpl类中必须有一个属性name,类型是ref,springmvc会自动调用这个属性的set方法. -->
<property name="injectionDAO" ref="injectionDAO"></property>
</bean> <bean id="injectionDAO" class="gys.dao.InjectionDAOImpl"></bean> </beans>
Run.java
package gys; import gys.service.InjectionService;
public class Run extends GetBeanBase{
public Run(){
super("classpath*:spring-ioc.xml");
start();
}
public void testSetter(){
InjectionService service=super.getBen("injectionService");
service.save("这是要保存的数据");
end();
}
public static void main(String[] args) {
Run run=new Run();
run.testSetter();
} }
2.构造注入:
对上面的代码做一下改变:
InjectionServiceImpl.java
package gys.service; import gys.dao.InjectionDAO; public class InjectionServiceImpl implements InjectionService{ private InjectionDAO injectionDAO; //构造器注入
public InjectionServiceImpl(InjectionDAO injectionDAO){
this.injectionDAO=injectionDAO;
} @Override
public void save(String info) {
System.out.println("service接受参数:"+info);
info=info+":"+this.hashCode();
injectionDAO.save(info);
}
}
spring-ioc.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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 构造注入 -->
<bean id="injectionService" class="gys.service.InjectionServiceImpl">
<!--在类InjectionServiceImpl中有一个属性name,还必须必须有一个构造器,这个构造器的参数是name值 类型是ref -->
<constructor-arg name="injectionDAO" ref="injectionDAO" />
</bean> <bean id="injectionDAO" class="gys.dao.InjectionDAOImpl"></bean> </beans>
Run.java
package gys; import gys.service.InjectionService; public class Run extends GetBeanBase{
public Run(){
super("classpath*:spring-ioc.xml");
start();
} public void testCons(){
InjectionService service=super.getBen("injectionService");
service.save("这是要保存的数据");
end();
} public static void main(String[] args) {
Run run=new Run();
run.testCons();
} }
下班了,未完待续......
springmvc笔记(来自慕课网)的更多相关文章
- jq1.6版本前后,attr()和prop()的区别,来自慕课网的回答
jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值时,会返回 property 的值,这就导致了结果的不一致.从 jQuery 1.6 开始, .prop()方法 方法返 ...
- 网站优化之-SEO在网页制作中的应用(信息来自慕课网课程笔记)
一.SEO基本介绍. 1.搜索引擎工作原理. 2.seo简介:SEarch Engine Optimization,搜索引擎优化.为了提升网页在搜索引擎自然搜索结果中的收录数量及排序位置而做的优化行为 ...
- PHP性能优化学习笔记--PHP周边性能优化--来自慕课网Pangee http://www.imooc.com/learn/205
PHP一般运行于Linux服务器中,周边主要包括:Linux运行环境.文件存储.数据库.缓存.网络 常见PHP场景的开销次序: 读写内存<<读写数据库(使用内存作为缓存.异步处理)< ...
- PHP性能优化学习笔记--语言级性能优化--来自慕课网Pangee http://www.imooc.com/learn/205
使用ab进行压力测试 ab -n行数 -c并发数 url 重点关注下面两点: 1.Request per secend : 每秒可接收的请求数 2.Time per request : 每次请求所耗费 ...
- 安卓开发_慕课网_Fragment实现Tab(App主界面)
学习内容来自“慕课网” 这里用Fragment来实现APP主界面 思路: 底部横向排列4个LinearLayout,每个LinearLayout包含一个图片按钮和一个文字 1.默认显示第一个功能(微信 ...
- 安卓开发_慕课网_ViewPager与FragmentPagerAdapter实现Tab实现Tab(App主界面)
学习内容来自“慕课网” ViewPager与FragmentPagerAdapter实现Tab 将这两种实现Tab的方法结合起来.效果就是可以拖动内容区域来改变相应的功能图标亮暗 思路: Fragme ...
- 安卓开发_慕课网_ViewPager实现Tab(App主界面)
学习内容来自“慕课网” 网站上一共有4种方法来实现APP主界面的TAB方法 这里学习第一种 ViewPager实现Tab 布局文件有7个, 主界面acitivity.layout <Linear ...
- es6 Object.assign ECMAScript 6 笔记(六) ECMAScript 6 笔记(一) react入门——慕课网笔记 jquery中动态新增的元素节点无法触发事件解决办法 响应式图像 弹窗细节 微信浏览器——返回操作 Float 的那些事 Flex布局 HTML5 data-* 自定义属性 参数传递的四种形式
es6 Object.assign 目录 一.基本用法 二.用途 1. 为对象添加属性 2. 为对象添加方法 3. 克隆对象 4. 合并多个对象 5. 为属性指定默认值 三.浏览器支持 ES6 O ...
- react入门——慕课网笔记
一. jsx 1. 被称为语法糖:糖衣语法,计算机语言中添加的某种语法,对语言的功能没有影响,更方便程序员使用,增加程序的可读性,降低出错的可能性 类似的还有(coffeescript,typescr ...
随机推荐
- linux包之coreutils之du和df命令
[root@localhost ~]# rpm -qf /usr/bin/ducoreutils-8.4-31.el6.x86_64[root@localhost ~]# rpm -qf /bin/d ...
- HTML5鼠标hover的时候图片放大的效果展示
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 安卓中Activity的onStart()和onResume()的区别是什么
知道Activity的四种状态:Active/Runing 一个新 Activity 启动入栈后,它在屏幕最前端,处于栈的最顶端,此时它处于可见并可和用户交互的激活状态. Paused 当 Activ ...
- .NET分布式事务未提交造成6107错误或系统被挂起的问题分析定位
问题描述: 系统中多个功能不定期出现“Unable to get error message (6107) (0).”错误,即分布式事务超时,但报出错误的部分功能根本没有使用分布式事务. 原因分析: ...
- 【python】unittest中常用的assert语句
下面是unittest模块的常用方法: assertEqual(a, b) a == b assertNotEqual(a, b) a != b assertTrue(x) b ...
- 【jmeter】目录介绍
JMeter也学了一阵子了,对于基本的操作已了解,再回过头来看看Jmeter的目录,本篇是对于它的目录进行一些简单的介绍. JMeter解压之后打开,根目录如下图: 1.bin:可执行文件目录 2.d ...
- Ansible之playbook
简介 playbook是一个非常简单的配置管理和多主机部署系统.可作为一个适合部署复杂应用程序的基础.playbook可以定制配置,可以按指定的操作步骤有序执行,支持同步和异步方式.playbook是 ...
- C语言每日一题之No.2
题目:已知三个整型数8,12,6,按公式s=a+b*c计算,并显示结果 思路:定义三个整型变量a,b,c 定义一个变量s用来保存运算结果 输出 程序: #include <stdio.h> ...
- 黄聪:主机宝安装wordpress注意事项
1.web环境安装PHP使用5.4.21-nts-03版本 2.web环境安装Mysql使用5.5.45版本 3.创建好站点后,给站点的public_html目录添加IIS_xxx用户最高权限,添加N ...
- 黄聪:Discuz!的SEO优化策略二:如何去掉页脚多余的信息
论坛搭建好,首先是把多余的东西都砍掉. 页脚的信息在我看来,都是很多余的信息,如下图: 要怎么消灭掉它们呢? 1.进入 全局 -- 站点信息 2.站点名称改为你的论坛名称,它会出现在内页的标题最末位. ...