初學spring之入門案列

spring實際上是一個很大的開源框架,而我學的就是spring framework,這只是spring其中的一小部分.有疑惑的能夠去官網去看看,spring官網我就不提供了.一百度確定有.和spring framework並行的有,這是一個網站,大家能夠看看:java

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

須要的jar包節點以下: express

 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.例子一:app

實體類以下:框架

 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 }

下面這是配置文件:ide

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.例子二(域屬性,簡稱複雜屬性,說簡單點就是在一個對象實體類中植入另一個對象實體=========注,因此須要兩個對象)網站

實體類以下:this

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思想還遠遠不夠,這只是我的看法.

相關文章
相關標籤/搜索