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之入门案列的更多相关文章

  1. Quartz经典入门案列

    一.Quartz简介 Quartz是一个开放源码项目,专注于任务调度器,提供了极为广泛的特性如持久化任务,集群和分布式任务等.Spring对Quartz的集成与其对JDK Timer的集成在任务.触发 ...

  2. Hadoop入门案列,初学者Coder

    1.WordCount Job类: package com.simope.mr.wcFor; import org.apache.hadoop.conf.Configuration; import o ...

  3. Spring MVC的配置文件(XML)的几个经典案列

    1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...

  4. Spring MVC入门讲解

    一.Springmvc是什么? Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想, 将web层进行职责解耦,基 ...

  5. js闭包的作用域以及闭包案列的介绍:

    转载▼ 标签: it   js闭包的作用域以及闭包案列的介绍:   首先我们根据前面的介绍来分析js闭包有什么作用,他会给我们编程带来什么好处? 闭包是为了更方便我们在处理js函数的时候会遇到以下的几 ...

  6. Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理

    本文是接着上篇博客写的:Spring boot 入门(三):SpringBoot 集成结合 AdminLTE(Freemarker),利用 generate 自动生成代码,利用 DataTable 和 ...

  7. Spring boot入门(三):SpringBoot集成结合AdminLTE(Freemarker),利用generate自动生成代码,利用DataTable和PageHelper进行分页显示

    关于SpringBoot和PageHelper,前篇博客已经介绍过Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件,前篇博客大致讲述了S ...

  8. Java基础-SSM之Spring MVC入门篇

    Java基础-SSM之Spring MVC入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Spring MVC简介 1>.什么是Spring MVC 答:Sprin ...

  9. Spring Boot 入门之持久层篇(三)

    原文地址:Spring Boot 入门之持久层篇(三) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之 Web 篇(二)>介绍了 ...

随机推荐

  1. [bzoj2208][Jsoi2010]连通数_bitset_传递闭包floyd

    连通数 bzoj-2208 Jsoi-2010 题目大意:给定一个n个节点的有向图,问每个节点可以到达的点的个数和. 注释:$1\le n\le 2000$. 想法:网上有好多tarjan+拓扑序dp ...

  2. JVM(五):探究类加载过程-上

    JVM(五):探究类加载过程-上 本文我们来研究一个Java字节码文件(Class文件)是如何加载入内存中的,在這個过程中涉及类加载过程中的加载,验证,准备,解析(连接),初始化,使用,销毁过程,并探 ...

  3. JSP的异常处理

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/exception-handling.html: 当写JSP代码的时候,有可能会留下一个编码错误,并且它会 ...

  4. Jsp+servlet 验证码案例

    昨晚在csdn看到一位前辈写一个ajax+servlet+jsp验证.顿时心血来潮,在阅读前辈的代码下我亲手体验一下,做了一个验证码生成工具类.以供大家做个參考. 1:加入VeriyCodeUtils ...

  5. Android内存泄露之开篇

    先来想这三个问题 内存泄露是怎么回事 内存会泄露的原因 避免内存泄露 1.内存泄露怎么回事 一个程序中,已经不须要使用某个对象,可是由于仍然有引用指向它垃圾回收器就无法回收它,当然该对象占用的内存就无 ...

  6. Struts2.3动态调用报 No result defined for action 错误

    struts 2.3.16  採用动态调用发现不工作报404 not found,网上查找原因: 1.由于:struts2中默认不同意使用DMI 所以:须要在配置文件里打开: <constant ...

  7. Android基础新手教程——4.3.1 BroadcastReceiver牛刀小试

    Android基础新手教程--4.3.1 BroadcastReceiver牛刀小试 标签(空格分隔): Android基础新手教程 本节引言 本节我们将来学习Android四大组件中的第三个:Bro ...

  8. 《炉石传说》架构设计赏析(4):Asset管理

    欢迎转载,请注明作者[燕良@游戏开发]及原文地址:http://blog.csdn.net/neil3d/article/details/39580197 另外.欢迎大家来我的QQ群交流各种游戏引擎相 ...

  9. 【bzoj2748】[HAOI2012]音量调节

    设F[i][j]表示在第i首歌曲结束后,音量能否刚好为j 转移:F[i][j]=F[i][j-C[i]] or F[i][j+C[i]] 初始化:F[0][beginlevel]=true 最后在所有 ...

  10. P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)

    P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...