初学spring之入门案列

spring其实是一个很大的开源框架,而我学的就是spring framework,这只是spring其中的一小部分.有疑惑的可以去官网去看看,spring官网我就不提供了.一百度肯定有.和spring framework并行的有,这是一个网站,你们可以看看:

http://blog.csdn.net/hjd_love_zzt/article/details/12966273

需要的jar包节点如下: 

 1 <dependency>
 2             <groupId>org.springframework</groupId>
 3             <artifactId>spring-beans</artifactId>
 4             <version>4.3.9.RELEASE</version>
 5         </dependency>
 6         <dependency>
 7             <groupId> org.aspectj</groupId >
 8             <artifactId> aspectjweaver</artifactId >
 9             <version> 1.8.7</version>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework</groupId>
13             <artifactId>spring-context</artifactId>
14             <version>4.3.9.RELEASE</version>
15         </dependency>
16         <dependency>

今天我敲的几个相对比较简单的列子:

1.例子一:

实体类如下:

 1 package cn.ql.spring01; /**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 /**
 6  * \* Created with IntelliJ IDEA.
 7  * \* User: 123
 8  * \* Date: 2017/07/24
 9  * \* Time: 09:26
10  * \* To change this template use File | Settings | File Templates.
11  * \* Description:
12  * \
13  */
14 public class SomeService {
15     private String info;
16 
17     public String getInfo() {
18         return info;
19     }
20 
21     public void setInfo(String info) {
22         this.info = info;
23     }
24 
25     public void work() {
26         System.out.println("Hello" + info);
27     }
28 }

下面这是配置文件:

1 <!--  第一个spring例子  -->
2     <bean id="someService" class="cn.ql.spring01.SomeService">
3     </bean>

这是测试类:

 1 package cn.ql.spring01;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 /**
 9  * \* Created with IntelliJ IDEA.
10  * \* User: 123
11  * \* Date: 2017/07/24
12  * \* Time: 09:31
13  * \* To change this template use File | Settings | File Templates.
14  * \* Description:
15  * \
16  */
17 public class TestSomeService {
18 
19     @Test
20     public void TestSomeService() {
21         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
22         SomeService someService = (SomeService)ctx.getBean("someService");
23 
24         someService.setInfo("spring");
25 
26         someService.work();
27     }
28 }

 

 

2.例子二(域属性,简称复杂属性,说简单点就是在一个对象实体类中植入另外一个对象实体=========注,所以需要两个对象)

实体类如下:

Car实体类

 1 package cn.ql.spring02;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 /**
 6  * \* Created with IntelliJ IDEA.
 7  * \* User: 123
 8  * \* Date: 2017/07/24
 9  * \* Time: 10:17
10  * \* To change this template use File | Settings | File Templates.
11  * \* Description:
12  * \
13  */
14 public class Car {
15     private String brand;
16     private String color;
17 
18     @Override
19     public String toString() {
20         return "Car{" +
21                 "brand='" + brand + '\'' +
22                 ", color='" + color + '\'' +
23                 '}';
24     }
25 
26     public String getColor() {
27         return color;
28     }
29 
30     public void setColor(String color) {
31         this.color = color;
32     }
33 
34     public String getBrand() {
35         return brand;
36     }
37 
38     public void setBrand(String brand) {
39         this.brand = brand;
40     }
41 }

 Student实体类

 1 package cn.ql.spring02;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 /**
 6  * \* Created with IntelliJ IDEA.
 7  * \* User: 123
 8  * \* Date: 2017/07/24
 9  * \* Time: 10:20
10  * \* To change this template use File | Settings | File Templates.
11  * \* Description:
12  * \
13  */
14 public class Student {
15     private String name;
16     private int age;
17 
18     private Car car;    //植入Car类型的复杂对象
19 
20     public Car getCar() {
21         return car;
22     }
23 
24     public void setCar(Car car) {
25         this.car = car;
26     }
27 
28     @Override
29     public String toString() {
30         return "Student{" +
31                 "name='" + name + '\'' +
32                 ", age=" + age +
33                 ", car=" + car +
34                 '}';
35     }
36 
37     public String getName() {
38         return name;
39     }
40 
41     public void setName(String name) {
42         this.name = name;
43     }
44 
45     public int getAge() {
46         return age;
47     }
48 
49     public void setAge(int age) {
50         this.age = age;
51     }
52 }

 

配置文件如下:

 1 <!--第二个spring例子-->
 2     <bean id="car" class="cn.ql.spring02.Car">
 3         <property name="brand" value="兰博基尼"></property>
 4         <property name="color" value="绿色"></property>
 5     </bean>
 6 
 7     <bean id="student" class="cn.ql.spring02.Student">
 8         <property name="name" value="大哥"></property>
 9         <property name="age" value="3"></property>
10         <property name="car" ref="car"></property>
11     </bean>

 

测试类如下:

 1 package cn.ql.spring01;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 import cn.ql.spring01.SomeService;
 6 import cn.ql.spring02.Student;
 7 import org.junit.Test;
 8 import org.springframework.context.ApplicationContext;
 9 import org.springframework.context.support.ClassPathXmlApplicationContext;
10 
11 /**
12  * \* Created with IntelliJ IDEA.
13  * \* User: 123
14  * \* Date: 2017/07/24
15  * \* Time: 09:31
16  * \* To change this template use File | Settings | File Templates.
17  * \* Description:
18  * \
19  */
20 public class TestCarAndStudent {
21 
22     @Test
23     public void TestCarAndStudent() {
24         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
25         Student stu = (Student) ctx.getBean("student");
26         System.out.println(stu);
27     }
28 }

也是没啥问题的.

 

3.例子三(打印机案例)

墨水接口

 1 package cn.ql.spring03;
 2 
 3 /**
 4  * Created by 123 on 2017/07/24.
 5  */
 6 //墨水接口
 7 public interface Ink {
 8     //获取颜色的方法
 9     public String getColor();
10 }

纸张接口

 1 package cn.ql.spring03;
 2 
 3 /**
 4  * Created by 123 on 2017/07/24.
 5  */
 6 //纸张接口
 7 public interface Paper {
 8     //获取类型纸张的方法
 9     public String getPage();
10 }

彩色墨水实现类

 1 package cn.ql.spring03;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 /**
 6  * \* Created with IntelliJ IDEA.
 7  * \* User: 123
 8  * \* Date: 2017/07/24
 9  * \* Time: 10:36
10  * \* To change this template use File | Settings | File Templates.
11  * \* Description:
12  * \
13  */
14 
15 //墨水的实现类    彩色墨水
16 public class ColorInk implements Ink {
17     @Override
18     public String getColor() {
19         return "彩色";
20     }
21 }

灰色墨水实现类

 1 package cn.ql.spring03;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 /**
 6  * \* Created with IntelliJ IDEA.
 7  * \* User: 123
 8  * \* Date: 2017/07/24
 9  * \* Time: 10:36
10  * \* To change this template use File | Settings | File Templates.
11  * \* Description:
12  * \
13  */
14 墨水的实现类    灰色墨水
15 public class GrayInk implements Ink {
16     @Override
17     public String getColor() {
18         return "灰色";
19     }
20 }

A4纸张实现类

 1 package cn.ql.spring03;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 /**
 6  * \* Created with IntelliJ IDEA.
 7  * \* User: 123
 8  * \* Date: 2017/07/24
 9  * \* Time: 10:39
10  * \* To change this template use File | Settings | File Templates.
11  * \* Description:
12  * \
13  */
14 
15 //纸张的实现类    A4纸张
16 public class A4Paper implements Paper {
17     @Override
18     public String getPage() {
19         return "我是一张A4纸";
20     }
21 }

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纸";
    }
}

打印机实体类(其实就是植入两个复杂类型的对象)

 

 1 package cn.ql.spring03;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 /**
 6  * \* Created with IntelliJ IDEA.
 7  * \* User: 123
 8  * \* Date: 2017/07/24
 9  * \* Time: 10:42
10  * \* To change this template use File | Settings | File Templates.
11  * \* Description:
12  * \
13  */
14 public class Print {
15     //植入两个复杂类型的对象
16     private Ink ink;
17     private Paper paper;
18 
19     public Ink getInk() {
20         return ink;
21     }
22 
23     public void setInk(Ink ink) {
24         this.ink = ink;
25     }
26 
27 
28     //因为方便我获取颜色和类型纸张,所以直接就调用了复杂类型的get方法
29     @Override
30     public String toString() {
31         return "Print{" +
32                 "ink=" + ink.getColor() +
33                 ", paper=" + paper.getPage() +
34                 '}';
35     }
36 
37     public Paper getPaper() {
38         return paper;
39     }
40 
41     public void setPaper(Paper paper) {
42         this.paper = paper;
43     }
44 }

配置文件

1 <!--第三个spring例子 -->
2     <bean id="a4Paper" class="cn.ql.spring03.A4Paper"></bean>
3     <bean id="colorInk" class="cn.ql.spring03.ColorInk"></bean>
4 
5     <bean id="print" class="cn.ql.spring03.Print">
6         <property name="ink" ref="colorInk"></property>     <!--ref可以说是间接调用了colorInkk的id的实现类-->
7         <property name="paper" ref="a4Paper"></property>    <!---->
8     </bean>

测试类如下:

 1 package cn.ql.spring01;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 import cn.ql.spring03.Print;
 6 import org.junit.Test;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 import java.applet.AppletContext;
11 
12 /**
13  * \* Created with IntelliJ IDEA.
14  * \* User: 123
15  * \* Date: 2017/07/24
16  * \* Time: 11:35
17  * \* To change this template use File | Settings | File Templates.
18  * \* Description:
19  * \
20  */
21 public class TestPrint {
22 
23     @Test
24     public void Testprint() {
25         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
26         Print print = (Print) ctx.getBean("print");
27         //其实在这里调用也是没啥问题的
28         System.out.println(print);
29     }
30 }

 

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层:

 1 package cn.ql.springAOP04.service;
 2 
 3 import cn.ql.springAOP04.entity.User;
 4 
 5 /**
 6  * Created by 123 on 2017/07/24.
 7  */
 8 public interface IUserService {
 9     public void save(User user);
10 }

UserServiceImpl类

 1 package cn.ql.springAOP04.service;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 import cn.ql.springAOP04.dao.IUserDAO;
 6 import cn.ql.springAOP04.dao.UserDAOImpl;
 7 import cn.ql.springAOP04.entity.User;
 8 
 9 /**
10  * \* Created with IntelliJ IDEA.
11  * \* User: 123
12  * \* Date: 2017/07/24
13  * \* Time: 12:07
14  * \* To change this template use File | Settings | File Templates.
15  * \* Description:
16  * \
17  */
18 public class UserServiceImpl implements IUserService {
19 
20 
21     private IUserDAO dao;
22 
23     public IUserDAO getImpl() {
24         return dao;
25     }
26 
27     public void setImpl(IUserDAO dao) {
28         this.dao = dao;
29     }
30 
31     @Override
32     public void save(User user) {
33         dao.save(user);
34     }
35 }

AOP层(我只是测试了一下前置增强方法,大家有兴趣的,可以试试其他的):

 1 package cn.ql.springAOP04.aop;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 import org.springframework.aop.MethodBeforeAdvice;
 6 
 7 import java.lang.reflect.Method;
 8 
 9 /**
10  * \* Created with IntelliJ IDEA.
11  * \* User: 123
12  * \* Date: 2017/07/24
13  * \* Time: 12:11
14  * \* To change this template use File | Settings | File Templates.
15  * \* Description:
16  * \
17  */
18 //前置增强类
19 public class LoggerBeforeAdvice implements MethodBeforeAdvice {
20     @Override
21     public void before(Method method, Object[] objects, Object o) throws Throwable {
22         System.out.println("========================记录日志");
23     }
24 }

配置文件(这个配置相对来说是比较多的,不懂得,可以去看看下面的注释):

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5 
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 8 ">
 9     <!--1.配置dao层    只能是实现类,不是接口-->
10     <bean id="userDAO" class="cn.ql.springAOP04.dao.UserDAOImpl"></bean>
11 
12     <!--2.service 植入对象-->
13     <bean id="userService" class="cn.ql.springAOP04.service.UserServiceImpl">
14         <property name="impl" ref="userDAO"></property>
15     </bean>
16 
17     <!--3 通知  advice:增强-->
18     <bean id="loggerBefore" class="cn.ql.springAOP04.aop.LoggerBeforeAdvice"></bean>
19 
20     <aop:config>
21         <!--配置切点  expression表达式      execution需要拦截的类   -->
22         <aop:pointcut id="mypointcut"
23                       expression="execution(public void cn.ql.springAOP04.service.UserServiceImpl.save(cn.ql.springAOP04.entity.User))"></aop:pointcut>
24 
25         <!--织入   advice-ref   相当于引用loggerBefore      pointcut-ref  引用了mypointcut      -->
26         <aop:advisor advice-ref="loggerBefore" pointcut-ref="mypointcut"></aop:advisor>
27     </aop:config>
28 
29 </beans>

测试类:

 1 package cn.ql.spring01;/**
 2  * Created by 123 on 2017/07/24.
 3  */
 4 
 5 import cn.ql.springAOP04.entity.User;
 6 import cn.ql.springAOP04.service.IUserService;
 7 import cn.ql.springAOP04.service.UserServiceImpl;
 8 import org.junit.Test;
 9 import org.springframework.context.ApplicationContext;
10 import org.springframework.context.support.ClassPathXmlApplicationContext;
11 
12 /**
13  * \* Created with IntelliJ IDEA.
14  * \* User: 123
15  * \* Date: 2017/07/24
16  * \* Time: 12:28
17  * \* To change this template use File | Settings | File Templates.
18  * \* Description:
19  * \
20  */
21 public class TestAOP04 {
22 
23     @Test
24     public void testAOP04() {
25         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextAop.xml");
26         IUserService service = (IUserService) ctx.getBean("userService");
27         User user = new User();
28         service.save(user);
29 
30     }
31 
32 }

其实吧,我感觉就是最后一个例子,有些难度,其余的还好.我感觉AOP编程,以我的理解就是把相同的步骤变得简易化.或者从某种程度上来说,就是使程序更健壮,同时也提高了编码的质量,可能我所理解的AOP思想还远远不够,这只是个人见解.

转载于:https://www.cnblogs.com/bdqnquliang/p/7235837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值