初学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 篇(二)>介绍了 ...
随机推荐
- Layui动画、按钮、表单
Layui动画.按钮.表单 在实用价值的前提之下,我们并没有内置过多花俏的动画.而他们同样在 layui 的许多交互元素中,发挥着重要的作用.layui 的动画全部采用 CSS3,因此不支持ie8和部 ...
- Linux下使用make install安装的软件如何卸载
如果是Ubuntu的系统,那么可以使用checkinstall来生成deb包来安装,然后卸载 参考:http://blog.sina.com.cn/s/blog_4178f4bf0101cmt7.ht ...
- Servlet处理日期
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/handling-date.html: 使用Servlet的最重要的优势之一是可以使用核心Java ...
- sum over使用方法,以及与group by的差别
1.sum over使用方法 sum(col1) over(partition by col2 order by col3 ) 以上的函数能够理解为:按col2 进行分组(partition ),每组 ...
- (源代码分析)Android-Universal-Image-Loader (图片异步载入缓存库)的使用配置
转载请注明出处:http://blog.csdn.net/u011733020 前言: 在Android开发中,对于图片的载入能够说是个老生常谈的问题了,图片载入是一个比較坑的地方.处理不好,会有各种 ...
- .a 文件解析
首先先准备一个静态库.a文件,比如叫staticLibrary.a,放在桌面的test目录里. 分离arch 首先先file一下staticLibrary.a,看一下该文件包含几种arch. ~ cd ...
- UVA1523-Helicopter(暴力+全排列)
题目链接 题意:有八个乘客坐在直升机上,求重心M最小值. 思路:依据题目所给的公式,我们能够知道要使得M最小.也就是要使得Mv和Mh的和最小,我们能够使用全排列,分别将每一个值放在各个位子上,然后更新 ...
- JavaScript图片裁剪
1.jquery 图片裁剪库选择 Jcrop:http://deepliquid.com/content/Jcrop.html imgareaselect:http://odyniec.net/pro ...
- java SocketChannel and ServerSocketChannel
1 SocketChannel 1.1 打开一个SocketChannel SocketChannel socketChannel = SocketChannel.open(); socketChan ...
- mysqld 与 python 邮件监控脚本 内存消耗对比
top - 21:38:40 up 1 day, 10:38, 5 users, load average: 0.00, 0.01, 0.17Tasks: 88 total, 1 running, 8 ...