初学spring之入门案列
spring其实是一个很大的开源框架,而我学的就是spring framework,这只是spring其中的一小部分.有疑惑的可以去官网去看看,spring官网我就不提供了.一百度肯定有.和spring framework并行的有,这是一个网站,你们可以看看:
http://blog.csdn.net/hjd_love_zzt/article/details/12966273
需要的jar包节点如下:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
今天我敲的几个相对比较简单的列子:
1.例子一:
实体类如下:
package cn.ql.spring01; /**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 09:26
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class SomeService {
private String info; public String getInfo() {
return info;
} public void setInfo(String info) {
this.info = info;
} public void work() {
System.out.println("Hello" + info);
}
}
下面这是配置文件:
<!-- 第一个spring例子 -->
<bean id="someService" class="cn.ql.spring01.SomeService">
</bean>
这是测试类:
package cn.ql.spring01;/**
* Created by 123 on 2017/07/24.
*/ import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 09:31
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class TestSomeService { @Test
public void TestSomeService() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SomeService someService = (SomeService)ctx.getBean("someService"); someService.setInfo("spring"); someService.work();
}
}
2.例子二(域属性,简称复杂属性,说简单点就是在一个对象实体类中植入另外一个对象实体=========注,所以需要两个对象)
实体类如下:
Car实体类
package cn.ql.spring02;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 10:17
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class Car {
private String brand;
private String color; @Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", color='" + color + '\'' +
'}';
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
}
}
Student实体类
package cn.ql.spring02;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 10:20
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class Student {
private String name;
private int age; private Car car; //植入Car类型的复杂对象 public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", car=" + car +
'}';
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
}
}
配置文件如下:
<!--第二个spring例子-->
<bean id="car" class="cn.ql.spring02.Car">
<property name="brand" value="兰博基尼"></property>
<property name="color" value="绿色"></property>
</bean> <bean id="student" class="cn.ql.spring02.Student">
<property name="name" value="大哥"></property>
<property name="age" value="3"></property>
<property name="car" ref="car"></property>
</bean>
测试类如下:
package cn.ql.spring01;/**
* Created by 123 on 2017/07/24.
*/ import cn.ql.spring01.SomeService;
import cn.ql.spring02.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 09:31
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class TestCarAndStudent { @Test
public void TestCarAndStudent() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student) ctx.getBean("student");
System.out.println(stu);
}
}
也是没啥问题的.
3.例子三(打印机案例)
墨水接口
package cn.ql.spring03; /**
* Created by 123 on 2017/07/24.
*/
//墨水接口
public interface Ink {
//获取颜色的方法
public String getColor();
}
纸张接口
package cn.ql.spring03; /**
* Created by 123 on 2017/07/24.
*/
//纸张接口
public interface Paper {
//获取类型纸张的方法
public String getPage();
}
彩色墨水实现类
package cn.ql.spring03;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 10:36
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/ //墨水的实现类 彩色墨水
public class ColorInk implements Ink {
@Override
public String getColor() {
return "彩色";
}
}
灰色墨水实现类
package cn.ql.spring03;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 10:36
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
////墨水的实现类 灰色墨水
public class GrayInk implements Ink {
@Override
public String getColor() {
return "灰色";
}
}
A4纸张实现类
package cn.ql.spring03;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 10:39
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/ //纸张的实现类 A4纸张
public class A4Paper implements Paper {
@Override
public String getPage() {
return "我是一张A4纸";
}
}
B5纸张实现类
package cn.ql.spring03;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 10:40
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
//纸张的实现类 B5纸张
public class B5Paper implements Paper {
@Override
public String getPage() {
return "我是一张B5纸";
}
}
打印机实体类(其实就是植入两个复杂类型的对象)
package cn.ql.spring03;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 10:42
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class Print {
//植入两个复杂类型的对象
private Ink ink;
private Paper paper; public Ink getInk() {
return ink;
} public void setInk(Ink ink) {
this.ink = ink;
} //因为方便我获取颜色和类型纸张,所以直接就调用了复杂类型的get方法
@Override
public String toString() {
return "Print{" +
"ink=" + ink.getColor() +
", paper=" + paper.getPage() +
'}';
} public Paper getPaper() {
return paper;
} public void setPaper(Paper paper) {
this.paper = paper;
}
}
配置文件
<!--第三个spring例子 -->
<bean id="a4Paper" class="cn.ql.spring03.A4Paper"></bean>
<bean id="colorInk" class="cn.ql.spring03.ColorInk"></bean> <bean id="print" class="cn.ql.spring03.Print">
<property name="ink" ref="colorInk"></property> <!--ref可以说是间接调用了colorInkk的id的实现类-->
<property name="paper" ref="a4Paper"></property> <!---->
</bean>
测试类如下:
package cn.ql.spring01;/**
* Created by 123 on 2017/07/24.
*/ import cn.ql.spring03.Print;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.applet.AppletContext; /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 11:35
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class TestPrint { @Test
public void Testprint() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Print print = (Print) ctx.getBean("print");
//其实在这里调用也是没啥问题的
System.out.println(print);
}
}
4.例子四(spring之AOP概念,这个例子对于新手来说还是有难度的,所以我就把这个例子的目录结构贴出来)
红色标记的都是我这个案例使用到的类.
dao层:
package cn.ql.springAOP04.dao; import cn.ql.springAOP04.entity.User; /**
* Created by 123 on 2017/07/24.
*/
//用户的接口
public interface IUserDAO {
//保存用户
public void save(User user);
}
实现类:
package cn.ql.springAOP04.dao;/**
* Created by 123 on 2017/07/24.
*/ import cn.ql.springAOP04.entity.User; /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 12:04
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
//实现类
public class UserDAOImpl implements IUserDAO {
public void save(User user) {
System.out.println("save success");
}
}
实体类:
package cn.ql.springAOP04.entity;/**
* Created by 123 on 2017/07/24.
*/ /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 12:03
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
//用户实体类
public class User {
private String name;
private String eamil; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getEamil() {
return eamil;
} public void setEamil(String eamil) {
this.eamil = eamil;
}
}
service层:
package cn.ql.springAOP04.service; import cn.ql.springAOP04.entity.User; /**
* Created by 123 on 2017/07/24.
*/
public interface IUserService {
public void save(User user);
}
UserServiceImpl类
package cn.ql.springAOP04.service;/**
* Created by 123 on 2017/07/24.
*/ import cn.ql.springAOP04.dao.IUserDAO;
import cn.ql.springAOP04.dao.UserDAOImpl;
import cn.ql.springAOP04.entity.User; /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 12:07
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class UserServiceImpl implements IUserService { private IUserDAO dao; public IUserDAO getImpl() {
return dao;
} public void setImpl(IUserDAO dao) {
this.dao = dao;
} @Override
public void save(User user) {
dao.save(user);
}
}
AOP层(我只是测试了一下前置增强方法,大家有兴趣的,可以试试其他的):
package cn.ql.springAOP04.aop;/**
* Created by 123 on 2017/07/24.
*/ import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 12:11
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
//前置增强类
public class LoggerBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("========================记录日志");
}
}
配置文件(这个配置相对来说是比较多的,不懂得,可以去看看下面的注释):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--1.配置dao层 只能是实现类,不是接口-->
<bean id="userDAO" class="cn.ql.springAOP04.dao.UserDAOImpl"></bean> <!--2.service 植入对象-->
<bean id="userService" class="cn.ql.springAOP04.service.UserServiceImpl">
<property name="impl" ref="userDAO"></property>
</bean> <!--3 通知 advice:增强-->
<bean id="loggerBefore" class="cn.ql.springAOP04.aop.LoggerBeforeAdvice"></bean> <aop:config>
<!--配置切点 expression表达式 execution需要拦截的类 -->
<aop:pointcut id="mypointcut"
expression="execution(public void cn.ql.springAOP04.service.UserServiceImpl.save(cn.ql.springAOP04.entity.User))"></aop:pointcut> <!--织入 advice-ref 相当于引用loggerBefore pointcut-ref 引用了mypointcut -->
<aop:advisor advice-ref="loggerBefore" pointcut-ref="mypointcut"></aop:advisor>
</aop:config> </beans>
测试类:
package cn.ql.spring01;/**
* Created by 123 on 2017/07/24.
*/ import cn.ql.springAOP04.entity.User;
import cn.ql.springAOP04.service.IUserService;
import cn.ql.springAOP04.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* \* Created with IntelliJ IDEA.
* \* User: 123
* \* Date: 2017/07/24
* \* Time: 12:28
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \
*/
public class TestAOP04 { @Test
public void testAOP04() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextAop.xml");
IUserService service = (IUserService) ctx.getBean("userService");
User user = new User();
service.save(user); } }
其实吧,我感觉就是最后一个例子,有些难度,其余的还好.我感觉AOP编程,以我的理解就是把相同的步骤变得简易化.或者从某种程度上来说,就是使程序更健壮,同时也提高了编码的质量,可能我所理解的AOP思想还远远不够,这只是个人见解.
初学spring之入门案列的更多相关文章
- Quartz经典入门案列
一.Quartz简介 Quartz是一个开放源码项目,专注于任务调度器,提供了极为广泛的特性如持久化任务,集群和分布式任务等.Spring对Quartz的集成与其对JDK Timer的集成在任务.触发 ...
- Hadoop入门案列,初学者Coder
1.WordCount Job类: package com.simope.mr.wcFor; import org.apache.hadoop.conf.Configuration; import o ...
- Spring MVC的配置文件(XML)的几个经典案列
1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...
- Spring MVC入门讲解
一.Springmvc是什么? Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想, 将web层进行职责解耦,基 ...
- js闭包的作用域以及闭包案列的介绍:
转载▼ 标签: it js闭包的作用域以及闭包案列的介绍: 首先我们根据前面的介绍来分析js闭包有什么作用,他会给我们编程带来什么好处? 闭包是为了更方便我们在处理js函数的时候会遇到以下的几 ...
- Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理
本文是接着上篇博客写的:Spring boot 入门(三):SpringBoot 集成结合 AdminLTE(Freemarker),利用 generate 自动生成代码,利用 DataTable 和 ...
- Spring boot入门(三):SpringBoot集成结合AdminLTE(Freemarker),利用generate自动生成代码,利用DataTable和PageHelper进行分页显示
关于SpringBoot和PageHelper,前篇博客已经介绍过Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件,前篇博客大致讲述了S ...
- Java基础-SSM之Spring MVC入门篇
Java基础-SSM之Spring MVC入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spring MVC简介 1>.什么是Spring MVC 答:Sprin ...
- Spring Boot 入门之持久层篇(三)
原文地址:Spring Boot 入门之持久层篇(三) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之 Web 篇(二)>介绍了 ...
随机推荐
- [bzoj3252]攻略_dfs序_线段树_贪心
攻略 bzoj-3252 题目大意:给定一棵n个节点的有根树,点有点权.让你选出至多k个节点,使得他们到根的链的并最大. 注释:$1\le n\le 2\cdot 10^5$,$1\le val_i\ ...
- BNU2017校赛
A(模拟) 题意:求一个特殊图的最大流 分析:画画图发现就是for循环扫一遍 B(LCA) 题意:有n个点组成的树,有q个询问,每个询问(A,B,C),学生从B点走最短路径走到C点,再从C点走到根节点 ...
- Flink本地安装和创建Flink应用
本篇文章首发于头条号Flink本地安装和创建Flink应用,欢迎关注我的头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_tech)获取更多干货,也欢迎关注 ...
- 【转】关于easyui tab 加载 js ajax 不走后台的问题, 怕找不到 以防万一
一直以来群里里面很多人反应,在用tab加载界面的时候,界面里面的js不会执行.今天在此说明一下原因. 不管是window,dailog还是tab其实质最终都是继承了panel.panel有两种方式展示 ...
- Hibernate自定义简单主键生成
Hibernate自定义主键生成 当使用Hibernate定义pojo的时候,有时候需要生成一定规则的数据表主键,这时候我们可以采用自定义主键生成方式去生成主键. 例如: 1.在pojo属性中定义数据 ...
- 正则表达式lookahead and lookbehind zero-length assertions
正则表达式非常好的网站: https://www.regular-expressions.info/lookaround.html ---------------------------------- ...
- [Maid] Write Tasks in Markdown with Maid
Maid enables you to write your tasks in Markdown. Create a maidfile.md or a README.mdthen add Header ...
- 一个最简单的Servlet实例
先在tomcat的webapps目录下,新建一目录,如test.然后,在test目录下建立WEB-INF为名的目录.这个必须有. 然后,在WEB-INF目录下建立classes目录.用以存储所用到的c ...
- 记录一次node中台转发表单上传文件到后台过程
首发掘金 记录一次node中台转发表单上传文件到后台过程 本篇跟掘金为同一个作者leung 公司几个项目都是三层架构模式即前台,中台(中间层),后台.前台微信端公众号使用vue框架,后台管理前端使 ...
- mac 终端经常使用命令(三)
基本命令 1.列出文件 ls 參数 文件夹名 例: 看看驱动文件夹下有什么:ls /System/Library/Extensions 參数 -w 显示中文,-l 具体信息. -a 包含 ...