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思想还远远不够,这只是个人见解.

最新文章

  1. 【iCore3 双核心板_FPGA】实验二十四:Niosii——SDRAM读写实验
  2. ubuntu下取代ping的好工具tcpping
  3. EFcodeFirst+T4=操纵任意数据库
  4. 在CentOS 7 MySQL / MariaDB
  5. NGUI 实现 透明底图遮罩 &amp;&amp; 人物像素变黑
  6. JMeter监控服务器CPU, 内存,网络数据
  7. 用java在mysql中随机插入9000 000条数据
  8. jquery.validate.js实例演示
  9. 服务器环境搭建系列(二)-Tomcat篇
  10. linux的sudo apt-get install 和dpkg -i &lt;package.deb&gt;命令
  11. struts2 action重定向
  12. Oracle中NVARCHAR2与VARCHAR2的差别
  13. python_MySQL
  14. session_unset,session_destroy
  15. PAT甲级1061 Dating
  16. Node.js内置的文件系统模块(fs)
  17. 如何修改Linux的TTL值
  18. HDU 1686 Oulippo
  19. ORA-14452:试图创建,更改或删除正在使用的临时表中的索引
  20. css的盒模型手机端兼容写法应该是啥样的呢?

热门文章

  1. 无权二分图最大匹配 HDU2063 匈牙利算法 || Hopcroft-Karp
  2. POJ 3083_Children of the Candy Corn
  3. TOMCAT加载两次war包(重复加载)
  4. P1028 数的计算 洛谷
  5. 图解Windows下安装WebLogic
  6. uva 1411 Ants (权值和最小的完美匹配---KM算法)
  7. 用R进行微博分析的初步尝试
  8. CLR-基元类型以及溢出检查 (CLR-Via-C#) 类型基础
  9. C++求解数组中出现超1/4的三个数字。
  10. android XXXActivity和getApplicationContext()差别