初学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 篇(二)>介绍了 ...
随机推荐
- Session&Cookie 的介绍和使用
Session介绍与使用 1.Session基本介绍 Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序 ...
- JSP中操作Java Beans
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/beans.html: JavaBean是在编写Java时专门创建的Java类,根据JavaBean AP ...
- Java:删除某文件夹下的所有文件
import java.io.File;public class Test{ public static void main(String args[]){ Test t = new Test(); ...
- SVG :可缩放矢量图形(Scalable Vector Graphics)。
SVG 意为可缩放矢量图形(Scalable Vector Graphics). SVG 使用 XML 格式定义图像. SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失 SVG 使用 XML ...
- 【c++】【转】如何只在heap上创建对象,如何只在stack上建立对象?
http://www.cnblogs.com/chio/archive/2007/10/23/934335.html http://blog.csdn.net/szchtx/article/detai ...
- sqlite自己主动更新数据库
写一个类继承自 SQLiteOpenHelper 系统会自己主动加入构造方法. onCreate方法.onUpgrade方法 当数据库里面数据或者表结构有所修改时.咱们须要升级数据库 这个时候.版本 ...
- 【Android】应用安全——反编译
用java开发最操心的就是得到反编译,所以作为开发人员我们须要知道怎么反编译,那样才干知道怎样防止反编译.保证代码安全. 以下来看下比較经常使用的方法! 第一种方式:利用apktool反编译 1,首先 ...
- MySQL-数据表锁定
MySQL允许客户端会话明确获取表锁,以防止其他会话在特定时间段内访问表.客户端会话只能为自己获取或释放表锁.它不能获取或释放其他会话的表锁. 创建一个数据表: CREATE DATABASE IF ...
- pagefile.sys
pagefile.sys
- OTN / SONET / SDH
①OTN(光传送网,OpticalTransportNetwork),是以波分复用技术为基础.在光层组织网络的传送网,是下一代的骨干传送网; ②SONET (Synchronous Optical N ...