日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring的使用

發(fā)布時(shí)間:2024/3/24 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring的簡單使用

1、傳統(tǒng)的三層架構(gòu)問題

M:Model

? pojo service DAO

V:View

? HTML jsp

C:Controller

? Servlet

Controller------------------>Service-------------------->DAO

Servlet 提供事務(wù) JDBC|dbutils

? 整合Controller的框架 以及DAO的框架

SpringMVC

我們可以這樣認(rèn)為:Controller-----依賴---->Service-----依賴----->DAO

2、Spring是什么

Spring是一個(gè)用來做對象創(chuàng)建、以及管理對象之間依賴關(guān)系的 這樣一個(gè)框架

管理依賴關(guān)系:Controller-----依賴---->Service-----依賴----->DAO

對象創(chuàng)建:原來創(chuàng)建對象(new 反射 對象克隆)----有了Spring之后(Spring幫我們自動創(chuàng)建)

Spring:春天 程序員的春天來了

3、Spring能干什么

3.1、Spring在開發(fā)中到底能干嗎?

1、為業(yè)務(wù)邏輯層提供事務(wù)

2、整合第三方框架 相當(dāng)于是整個(gè)結(jié)構(gòu)中的一個(gè)粘合劑

3、提供了控制器的解決方案

4、提供了控制器的解決方案 JdbcTemplate

5、提供了郵件的整合方案

6、提供了Aop編程的這種思想

7、提供了Web支持的解決方案

8、IOC、DI

3.2、Spring中的七大模塊以及功能

Spring Core :IOC DI

Spring Aop :面向切面編程的一些東西

Spring DAO:DAO層的解決方案 ----------->JdbcTemplate

Spring ORM:這個(gè)包簡單的說 就是提供了 第三方數(shù)據(jù)庫訪問的框架的整合 (iBatis、Hibernate)

? O:Obeject

? R:Relation

? M:Mapping

JAVA對象通過映射關(guān)系 直接映射到 數(shù)據(jù)庫表 的這樣一門技術(shù) 其實(shí)就是DAO層的解決方案

Spring Web:提供了Web項(xiàng)目的一些支持

Spring WebMVC:Spring MVC------控制器的解決方案

Spring Context:主要提供的是上下文的支持

3.3、Spring的家族都有哪些框架

Spring SpringBoot SpringSecurity SpringCloud(不是一個(gè)框架 是一系列做微服務(wù)框架的集合)

###4、Spring中的IOC的問題

IOC:控制反轉(zhuǎn)

誰控制了誰?

? 以前我們創(chuàng)建Java對象的時(shí)候 自己創(chuàng)建的 new這種方式去完成

? JAVA對象的創(chuàng)建 就不再由我們?nèi)?chuàng)建 而是由Spring自動去創(chuàng)建

? Spring控制了Java對象的創(chuàng)建

什么東西反轉(zhuǎn)了?

? 創(chuàng)建對象的權(quán)利給反轉(zhuǎn)了

5、Spring中DI問題

6、Spring的第一個(gè)HelloWorld程序

6.1、導(dǎo)包

<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.7.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.7.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-web --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.7.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.7.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.7.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.3.7.RELEASE</version></dependency>

6.2、在main下創(chuàng)建resources文件夾設(shè)置成資源文件

6.3、在resource下創(chuàng)建一個(gè)bean-base.xml文件

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd"> </beans>

6.4、準(zhǔn)備一個(gè)java類

@Data @AllArgsConstructor @NoArgsConstructor public class User {private int id;private String userName;private String password; }

6.5、在xml中進(jìn)行對象的申明

<!--說明需要生成一個(gè)java對象 然后放到IOC的容器中id:身份唯一的標(biāo)識class:標(biāo)識的是要生成java的類--><bean id="user" class="com.qf.cd.helloword.User"></bean>

6.6、測試文件

//第一步:我們需要獲取這個(gè)對象的容器(IOC容器)ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean-base.xml");//第二步:獲取這個(gè)Java對象User user = context.getBean(User.class);//第三步:打印這個(gè)對象System.out.println(user);

7、Spring的實(shí)現(xiàn)方式之XML

7.1、基本的配置

<!--說明需要生成一個(gè)java對象 然后放到IOC的容器中id:身份唯一的標(biāo)識class:標(biāo)識的是要生成java的類name:給當(dāng)前這個(gè)類的對象取名字scope="prototype":表示的是多例 :多例的時(shí)候 java對象并沒有 放到容器 是你請求的時(shí)候生成對象 返回給你的"singleton":表示的是單例init-method="init" :對象放到IOC之前 初始化數(shù)據(jù)用的destroy-method:對象銷毀之前 還原對象 用的--><bean id="user111"name="userbobo"scope="singleton"init-method="init"destroy-method="destory"class="com.qf.cd.helloword.User"></bean>

7.2、有參數(shù)對象的對象的構(gòu)建

<!--帶參數(shù)的對象怎么實(shí)例化?--><!--<bean id="user2" class="com.qf.cd.helloword.User">--><!--<constructor-arg index="0" value="1"></constructor-arg>--><!--<constructor-arg index="1" value="小波波"></constructor-arg>--><!--<constructor-arg index="2" value="小王子"></constructor-arg>--><!--</bean>--><!--<bean id="user3" class="com.qf.cd.helloword.User">--><!--<constructor-arg name="id" value="2"></constructor-arg>--><!--<constructor-arg name="userName" value="小波2"></constructor-arg>--><!--<constructor-arg name="password" value="小王2"></constructor-arg>--><!--</bean>-->

7.3、工廠創(chuàng)建對象的方式

<!--工廠創(chuàng)建對象的問題--><!--申明工廠--><!-- <bean id="userFactory" class="com.qf.cd.helloword.UserFactory"></bean>--><!--獲取Java對象factory-bean:這個(gè)是factory的工廠對象 注意:值是bean的id的值factory-method:工廠中創(chuàng)建對象的 非靜態(tài)的這個(gè)方法--><!-- <bean id="user4" factory-bean="userFactory" factory-method="getInstance1"></bean>--><!--靜態(tài)方法構(gòu)建這樣一個(gè)對象factory-method:因?yàn)檎{(diào)用的是靜態(tài)的方法 所以后面是靜態(tài)方法的這個(gè)名字--><bean id="user4" class="com.qf.cd.helloword.UserFactory" factory-method="getStaticInstance"></bean>

7.4、出現(xiàn)的問題

通過類型找對象的時(shí)候、如果當(dāng)前類的實(shí)例在IOC容器中存在多個(gè)的話 那么 是會報(bào)錯(cuò) 因?yàn)樗恢滥阏业绞悄囊粋€(gè)Java對象

<!--lazy-init:這個(gè)是延遲對象的初始化 在你 getBean的時(shí)候 進(jìn)行對象的初始化--><!--<bean id="user1" class="com.qf.cd.helloword.User" lazy-init="true"> </bean>--><!--<bean id="user2" class="com.qf.cd.helloword.User"></bean>--><!--depends-on="userDAO":這個(gè)屬性可以告訴咋們的IOC容器先初始化誰比較合適--><bean id="userService" class="com.qf.cd.helloword.UserService" depends-on="userDAO,user"></bean><bean id="user" class="com.qf.cd.helloword.User"></bean><bean id="userDAO" class="com.qf.cd.helloword.UserDAO"></bean>

8、Spring中的DI(依賴注入)

依賴:誰依賴于誰?

? 在開發(fā)中:Controller-----依賴于-----Service-------依賴于DAO

? 注入了什么東西?

? 1、JAVA對象

? 2、可以是注入的咋們的值

8.1、通過構(gòu)造器進(jìn)行注入(就是給這個(gè)對象賦值)

8.1.1、首先在bean.xml中進(jìn)行申明
<!--這個(gè)是DAO接口--><bean id="userDAO" class="com.qf.cd.di.UserDAO"></bean><!--這個(gè)是業(yè)務(wù)實(shí)現(xiàn)--><bean id="userService" class="com.qf.cd.di.UserService"><!--如果是一個(gè)具體的值 那么就寫 Value寫值就可以了如果要注入的是一個(gè)對象 那么直接使用ref進(jìn)行引用就可以了ref后面的值 一定是這個(gè)對象在容器中的這個(gè)id值--><constructor-arg name="userDAO" ref="userDAO"></constructor-arg></bean>
8.1.2、然后給一個(gè)構(gòu)造器
public class UserService {private UserDAO userDAO;public UserService(UserDAO userDAO){this.userDAO=userDAO;}/*** 登陸*/public void login(){userDAO.aa();}}

8.2、通過Set方法進(jìn)行注入

8.2.1、編寫類成員屬性的Set方法
private UserDAO userDAO1111;public void setUserDAO1111(UserDAO userDAO1111) {this.userDAO1111 = userDAO1111;}
8.2.2、編寫配置文件
<!--通過Set方法進(jìn)行注入--><bean id="userDAO" class="com.qf.cd.di.UserDAO"></bean><bean id="userService" class="com.qf.cd.di.UserService"><!--property:表示的屬性name:填寫的是 成員變量的名稱--><property name="userDAO1111" ref="userDAO"></property></bean>

8.3、p標(biāo)簽注入

8.3.1、首先編寫Set方法(p標(biāo)簽的實(shí)質(zhì)還是Set注入)
private UserDAO userDAO;// p:標(biāo)簽注入的時(shí)候 底層實(shí)際上還是 Set注入的方式// 所以必須要有Set方法public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}
8.3.2、編寫配置文件
<!--這個(gè)是UserDAO的對象--><bean id="userDAO" class="com.qf.cd.di.UserDAO"></bean><bean id="userService" class="com.qf.cd.di.UserService" p:userDAO-ref="userDAO"></bean>

8.4、內(nèi)部bean注入

8.4.1、編寫set方法
private UserDAO userDAO;// p:標(biāo)簽注入的時(shí)候 底層實(shí)際上還是 Set注入的方式// 所以必須要有Set方法public void setUserDAO(UserDAO userDAO) {this.userDAO = userDAO;}
8.4.2、編寫配置文件
<!--下面玩下 內(nèi)部bean注入--><bean id="userService" class="com.qf.cd.di.UserService"><property name="userDAO"><bean class="com.qf.cd.di.UserDAO"></bean></property></bean>

總結(jié)一下:對象的注入方式:Set注入、另外一種是構(gòu)造器注入

9、自動裝配模型

<bean id="userDAO" class="com.qf.cd.di.UserDAO"></bean><!--autowire:自動裝配byName:通過名字自動的去找 名字相同的java對象 然后賦值給 當(dāng)前類的成員變量byType:這個(gè)是通過類型進(jìn)行裝配--><bean id="userService" class="com.qf.cd.di.UserService"></bean>

10、注解模式實(shí)現(xiàn)IOC、DI功能

@Component:這個(gè)注解表示的意思是將類生成對象放到IOC容器中 @Autowired :先通過類型找對象 找不到 再通過名字來找對象 注意 @Resource(type = UserDAO.class) :這個(gè)注解是先通過名字 再通過注解來找對象 注意 @PostConstruct:對象放到IOC容器之前 進(jìn)行初始化的操作 @PreDestroy:對象要銷毀之前要進(jìn)行對象的回收操作 @Repository:這個(gè)表示的是對象放到IOC容器中去 //這個(gè)注解一般用在DAO層 @Service:這個(gè)注解也是將對象放到IOC容器中

注意

@Override protected void finalize() throws Throwable {super.finalize(); } 每個(gè)對象的這個(gè)方法都可以干預(yù)對象在JVM回收數(shù)據(jù)的時(shí)候 不被回收

11、代理的設(shè)計(jì)模式

代理的設(shè)計(jì)模式解決了什么問題?

代理的設(shè)計(jì)模式 解決的問題:能夠動態(tài)的監(jiān)控一個(gè)類 中所有方法的執(zhí)行 以及在這個(gè)方法的執(zhí)行的前后動態(tài)植入咋們的代碼

代理:簡單的說就是對原來的功能的增強(qiáng)

比如:為業(yè)務(wù)邏輯層提供事務(wù)

? @Configuration

? Spring整合iBatis

11.1、靜態(tài)代理

靜態(tài)代理的前提是 當(dāng)前的類 必須實(shí)現(xiàn)接口否則沒法玩

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-615h7stg-1592395564225)(pic\1592288049671.png)]

11.1.1、被代理類的接口
public interface IUserService {void zz(); }
11.1.2、被代理類
public class UserService implements IUserService{/*** 轉(zhuǎn)賬這個(gè)方法*/public void zz(){System.out.println("鎖定張三的賬戶");System.out.println("張三的賬戶預(yù)減1000元");System.out.println("檢測李四的賬戶是否可用");System.out.println("李四的賬戶增加1000元");} }
11.1.3、代理類
public class UserServiceProxy implements IUserService{private IUserService userService;public UserServiceProxy(IUserService userService ){this.userService=userService;}@Overridepublic void zz() {System.out.println("添加事務(wù)");userService.zz();System.out.println("提交事務(wù)");} }

11.2、動態(tài)代理(JDK代理)

動態(tài):被代理的類的對象是自動生成的

? 被代理的類是自動生成的

? 被代理的類的對象也是自動生成的

? JDK代理:原因是使用的是JDK本身的API

前提:被代理的類 必須要實(shí)現(xiàn)接口

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-mkxIXJu0-1592395564227)(pic\1592290057779.png)]

11.2.1、被代理類實(shí)現(xiàn)的接口
public interface IUserService {void zz(); }
11.2.2、被代理的類
public class UserService implements IUserService {/*** 轉(zhuǎn)賬這個(gè)方法*/public void zz(){System.out.println("鎖定張三的賬戶");System.out.println("張三的賬戶預(yù)減1000元");System.out.println("檢測李四的賬戶是否可用");System.out.println("李四的賬戶增加1000元");} }
11.2.3、代理類實(shí)現(xiàn)
public static void main(String[] args){/*** 第一個(gè)參數(shù):classloader:表示的是類加載器 寫法是死的 類.class.getClassLoader* 第二個(gè)參數(shù):interfaces:當(dāng)前類實(shí)現(xiàn)的接口 之所以要這個(gè)東西 是因?yàn)?生成代理類的時(shí)候 需要實(shí)現(xiàn)接口* 1:被代理的是個(gè)類:類.class.getInterface* 2:被代理的是是個(gè)接口: new Class[]{接口.class}* 第三個(gè)參數(shù):invationHandle* 方法的監(jiān)聽的回調(diào)接口----當(dāng)你執(zhí)行這個(gè)接口中的某一個(gè)方法的時(shí)候就會自動的回調(diào)到這個(gè)方法中來*/IUserService userService= (IUserService) Proxy.newProxyInstance(UserService.class.getClassLoader(),UserService.class.getInterfaces(),new InvocationHandler() { //執(zhí)行到每一個(gè)方法都會執(zhí)行到這里來/**** @param proxy :代理對象* @param method :當(dāng)前執(zhí)行的方法* @param args :當(dāng)前執(zhí)行方法的參數(shù)封裝* @return* @throws Throwable*/@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//在方法執(zhí)行的前后進(jìn)行拓展System.out.println("打開事務(wù)");Object invoke = method.invoke(new UserService(), args);System.out.println("關(guān)閉事務(wù)");return invoke;}});

11.3、CGLIB代理

思考一個(gè)問題:

靜態(tài)代理和動態(tài)代理都有一個(gè)前提:被代理的類必須實(shí)現(xiàn)接口

假設(shè)一個(gè)類 沒有實(shí)現(xiàn)接口需要增強(qiáng) 怎么辦呢?

CGLIB代理在這種場景下 就應(yīng)運(yùn)而生了…

CGLIB代理本身是可以代理任意的一個(gè)類 這個(gè)類可以不實(shí)現(xiàn)接口 也可以實(shí)現(xiàn)接口

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-qQ4u2oCu-1592395564229)(pic\1592291117537.png)]

11.3.1、被代理的類
public class UserService{/*** 轉(zhuǎn)賬這個(gè)方法*/public void zz(){System.out.println("鎖定張三的賬戶");System.out.println("張三的賬戶預(yù)減1000元");System.out.println("檢測李四的賬戶是否可用");System.out.println("李四的賬戶增加1000元");} }
11.3.2、代理的類
public class UserServiceProxy{/*** 獲取這個(gè)代理類* @return*/public static UserService getUserServiceProxy(){Enhancer enhancer=new Enhancer();//設(shè)置生成的類的爹是誰enhancer.setSuperclass(UserService.class);//設(shè)置方法監(jiān)聽的回調(diào)enhancer.setCallback(new MyMethodInterceptor());return (UserService) enhancer.create();}/*** 方法的監(jiān)聽*/static class MyMethodInterceptor implements MethodInterceptor{/**** @param obj:代理對象* @param method :當(dāng)前執(zhí)行的方法* @param args :當(dāng)前執(zhí)行方法的參數(shù)* @param proxy :這個(gè)是方法代理* @return* @throws Throwable*/@Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {//在這里進(jìn)行增強(qiáng)System.out.println("開啟事務(wù)");Object invoke = method.invoke(new UserService(), args);System.out.println("提交事務(wù)");return invoke;}} }

#####11.3.3、測試實(shí)現(xiàn)

public class Test001 {public static void main(String[] args){UserService userServiceProxy = UserServiceProxy.getUserServiceProxy();userServiceProxy.zz();} }

12、AOP面向切面的編程

什么叫面向切面編程?

就是在編程的時(shí)候 將我們的重復(fù)的代碼抽取出來形成方法 再形成一個(gè)類、這個(gè)類就是切面類、當(dāng)我們在執(zhí)行原來代碼的時(shí)候 使用代理的設(shè)計(jì)模式 動態(tài)植入我們抽取出來的代碼 的這種編程方式就稱為 面向切面編程

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-yY20B6hf-1592395564231)(pic\1592293344476.png)]

12.1、手動實(shí)現(xiàn)下面向切面編程(使用CGLIB代理來實(shí)現(xiàn))

12.1.1、編寫業(yè)務(wù)類
@Component public class UserService {/*** 這是一個(gè)add的方法*/public void add(){System.out.println("添加數(shù)據(jù)完成....");}}
12.1.2、編寫aop類
@Component public class Aop {public void begin(){System.out.println("開啟事務(wù)");}public void commit(){System.out.println("提交事務(wù)");}}
12.1.3、編寫代理類
@Component public class UserServiceProxy implements MethodInterceptor {@AutowiredAop aop;/*** 獲取代理類* @return*/public UserService getUserServiceProxy(){Enhancer enhancer=new Enhancer();enhancer.setSuperclass(UserService.class);enhancer.setCallback(this);return (UserService) enhancer.create();}/*** 這里就是方法的攔截* @param obj* @param method* @param args* @param proxy* @return* @throws Throwable*/@Overridepublic Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {aop.begin();Object invoke = method.invoke(new UserService(), args);aop.commit();return invoke;} }
12.1.4、編寫配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd"><context:component-scan base-package="com.qf.cd.aop.sd"></context:component-scan></beans>
12.1.5、編寫測試類
public class Test001 {public static void main(String[] args){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean-aop1.xml");UserService userServiceProxy = context.getBean(UserServiceProxy.class).getUserServiceProxy();userServiceProxy.add();} }

12.2、使用XML配置模式實(shí)現(xiàn)AOP編程

12.2.1、編寫業(yè)務(wù)類
public class UserService {public void zz(){System.out.println("轉(zhuǎn)賬功能實(shí)現(xiàn).....");//int k=1/0;}public void pp(){System.out.println("這里實(shí)現(xiàn)了ppp的功能.....");}public void kb(){System.out.println("這里實(shí)現(xiàn)了口碑這個(gè)功能.....");} }
12.2.2、編寫AOP的類
public class Aop {public void begin(){System.out.println("打開事務(wù)....");}public void commit(){System.out.println("提交事務(wù).....");}/*** 方法調(diào)用完成 執(zhí)行返回的時(shí)候 就會自動執(zhí)行這個(gè)方法*/public void returning(){System.out.println("這里是returning方法.....");}/*** 調(diào)用切入點(diǎn)表達(dá)式 的時(shí)候 如果 方法拋出了異常 那么 就執(zhí)行這個(gè)方法*/public void afterThroeing(){System.out.println("拋出異常了....");}/*** 環(huán)繞*/public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {//這里有點(diǎn)類似那個(gè) 過濾器中的放行System.out.println("這里是環(huán)繞前");proceedingJoinPoint.proceed();System.out.println("這里是環(huán)繞后");}}
12.2.3、編寫配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd"><!--第一步:申明UserService對象--><bean id="userService" class="com.qf.cd.aop.xml.UserService"></bean><bean id="aop" class="com.qf.cd.aop.xml.Aop"></bean><!--進(jìn)行aop的相關(guān)配置--><aop:config><!--配置切入點(diǎn)切入點(diǎn):代碼植入的地方 在哪里植入就配置哪里的路徑id:隨便配expression:表達(dá)式的意思execution:當(dāng)成語法記住第一個(gè)* :返回值不知道是啥類型 所以寫*com.qf.cd.aop.xml.* 這個(gè)*代表的是這個(gè)包下面的所有的類com.qf.cd.aop.xml.*.*第二個(gè)*代表的是這個(gè)類下面的所有方法(..):不知道方法的參數(shù)類型 所以寫..--><aop:pointcut id="pt" expression="execution(* com.qf.cd.aop.xml.*.*(..))"></aop:pointcut><!--這個(gè)里面就根據(jù)類型配置相應(yīng)的方法了--><!--下面配置的是切面類--><aop:aspect ref="aop"><!--在執(zhí)行上面切入點(diǎn)表達(dá)式的時(shí)候 之前要執(zhí)行的方法 (前置)--><aop:before method="begin" pointcut-ref="pt"></aop:before><!--表示的是一個(gè)后置表達(dá)式 表示的意思是 在執(zhí)行了 切入點(diǎn)表達(dá)式 方法之后 要執(zhí)行的方法--><aop:after method="commit" pointcut-ref="pt"></aop:after><!--方法調(diào)用完成 執(zhí)行返回的時(shí)候 就會自動執(zhí)行這個(gè)方法--><aop:after-returning method="returning" pointcut-ref="pt"></aop:after-returning><!--我們調(diào)用方法的時(shí)候 如果拋出了異常那么就會執(zhí)行這個(gè)--><aop:after-throwing method="afterThroeing" pointcut-ref="pt"></aop:after-throwing><!--環(huán)繞--><aop:around method="around" pointcut-ref="pt"></aop:around></aop:aspect></aop:config> </beans>
12.2.4、編寫測試文件
public static void main(String[] args){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean-aop2.xml");UserService userService = context.getBean(UserService.class);userService.zz();}

12.3、使用注解模式完成AOP編程

12.3.1、注解的含義
@Aspect :表示的是當(dāng)前這個(gè)類 是一個(gè)切面類 @Before:前置處理器 @After:后置處理器 @AfterReturning:這個(gè)表示的是在返回值的時(shí)候 進(jìn)行調(diào)用 @AfterThrowing:這個(gè)表示的是在拋出異常的時(shí)候調(diào)用 @Around:環(huán)繞通知 @Pointcut:這個(gè)表示的是一個(gè)切入點(diǎn)表達(dá)式
12.3.2、編寫配置文件
<!--如果要使用到aop 那么必須配置 aop的自動使能--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><context:component-scan base-package="com.qf.cd.aop.annotation"></context:component-scan>
12.3.3、編寫業(yè)務(wù)類
@Service public class UserService {public void zz(){System.out.println("轉(zhuǎn)賬功能實(shí)現(xiàn).....");//int k=1/0;}public void pp(){System.out.println("這里實(shí)現(xiàn)了ppp的功能.....");}public void kb(){System.out.println("這里實(shí)現(xiàn)了口碑這個(gè)功能.....");} }
12.3.4、編寫Aop的類
@Component //放到IOC容器 @Aspect //表明是一個(gè)切面類 public class Aop {/*** 定義一個(gè)切入點(diǎn)表達(dá)式*/@Pointcut(value = "execution(* com.qf.cd.aop.annotation.UserService.*(..))")public void pt(){}@Before(value = "pt()")public void begin(){System.out.println("打開事務(wù)....");}@After(value = "pt()")public void commit(){System.out.println("提交事務(wù).....");}/*** 方法調(diào)用完成 執(zhí)行返回的時(shí)候 就會自動執(zhí)行這個(gè)方法*/@AfterReturning(value = "pt()")public void returning(){System.out.println("這里是returning方法.....");}/*** 調(diào)用切入點(diǎn)表達(dá)式 的時(shí)候 如果 方法拋出了異常 那么 就執(zhí)行這個(gè)方法*/@AfterThrowing(value = "pt()")public void afterThroeing(){System.out.println("拋出異常了....");}/*** 環(huán)繞*/@Around(value = "pt()")public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {//這里有點(diǎn)類似那個(gè) 過濾器中的放行System.out.println("這里是環(huán)繞前");proceedingJoinPoint.proceed();System.out.println("這里是環(huán)繞后");} }
12.3.5、測試
public static void main(String[] args){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean-aop3.xml");UserService userService = context.getBean(UserService.class);userService.kb();}

13、Spring的DAO模塊

DAO模塊:類似于dbutils 這個(gè)工具類、主要用來訪問數(shù)據(jù)庫的

13.1、導(dǎo)入連接池的包

<!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.22</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.0.7</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.7.RELEASE</version></dependency>

13.2、配置JdbcTemplate

<!--配置數(shù)據(jù)源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="jdbc:mysql:///cd200101"></property><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><!--配置jdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><context:component-scan base-package="com.qf.cd.dao"></context:component-scan>

13.3、編寫DAO

@Component public class UserDAO {@Autowiredprivate JdbcTemplate jdbcTemplate;public void update(){jdbcTemplate.update("insert into t_user(userName,password) values(?,?)","小波波","123");} }

13.4、測試

public static void main(String[] args){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean-dao.xml");UserDAO userDAO = context.getBean(UserDAO.class);userDAO.update();}

14、Spring中的事務(wù)問題

說明個(gè)問題

事務(wù)無非就是具有原子性、一些列的操作 同時(shí)成功 或者同時(shí)失敗

假設(shè)你來設(shè)計(jì)個(gè)事務(wù):怎么保證這一些列的操作同時(shí)成功 或者同時(shí)失敗呢?

開啟事務(wù)的時(shí)候 ----- 開始做記錄

? 把每一個(gè)操作都做一個(gè)記錄

當(dāng)前的業(yè)務(wù) 如果做完了----如果沒有產(chǎn)生異常------直接結(jié)束就可以了

? 如果產(chǎn)生了異常---------把所有的記錄做一個(gè)還原

事務(wù)有哪些分類:

事務(wù)如果按照粗細(xì)程度來劃分:

? 粗粒度事務(wù):給整個(gè)方法添加事務(wù)

? 細(xì)粒度事務(wù):給這個(gè)方法中 某幾行代碼添加事務(wù)

事務(wù)如果按照范圍來劃分:

? 全局事務(wù):跨數(shù)據(jù)庫的這樣一個(gè)事務(wù)

? 局部事務(wù):所有的事務(wù)是在 一個(gè)庫里面

14.1、Spring中通過xml來配置事務(wù)

14.1.1、編寫數(shù)據(jù)訪問的類
@Component public class UserDAO implements IUserDAO{@Autowiredprivate JdbcTemplate jdbcTemplate;/*** 更新數(shù)據(jù)庫的操作*/public void update(){jdbcTemplate.update("update t_user set userName=?,password=?","鐵蛋","119");} }
14.1.2、編寫業(yè)務(wù)邏輯類
@Service public class UserService {@Autowiredprivate IUserDAO userDAO;public void update(){userDAO.update();int k=1/0;} }
14.1.3、編寫配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd"><!--配置數(shù)據(jù)源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="jdbc:mysql:///cd200101"></property><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><!--配置JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--給JdbcTemplate配置事務(wù)--><bean id="tx-manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置事務(wù)增強(qiáng)--><tx:advice id="tx-advice" transaction-manager="tx-manager"><tx:attributes><!--tx:method:表示要給哪些方法添加事務(wù)name:添加事務(wù)的方法的名字 * :表示的是給所有的方法都要添加事務(wù)timeout:設(shè)置請求的超時(shí)時(shí)間 默認(rèn)-1 永遠(yuǎn)等待read-only:事務(wù)的類型 false:表示的是不是只讀事務(wù)(就是讀寫事務(wù))查詢數(shù)據(jù)的時(shí)候 不用添加事務(wù)也是對的 那么這個(gè)時(shí)候?qū)嶋H上用的是默認(rèn)的事務(wù)類型只讀事務(wù)在進(jìn)行增刪改的時(shí)候 只讀事務(wù) 就不能滿足我們的要求了 這個(gè)時(shí)候就需要讀寫事務(wù)了propagation:事務(wù)的傳輸類型REQUIRED:REQUIRED_NEW場景:業(yè)務(wù)邏輯層有事務(wù) 調(diào)用的DAO也添加了事務(wù) 如果你設(shè)置了傳輸類型是REQUIRED那么恭喜你 會以業(yè)務(wù)邏輯層的事務(wù)優(yōu)先如果你設(shè)置的傳輸類型是REQUIRED_NEW 那么DAO層事務(wù) 和 業(yè)務(wù)邏輯層的事務(wù)是分割開的相互不會被影響isolation:設(shè)置的是事務(wù)的隔離級別 這個(gè)一般不設(shè)置no-rollback-for:表示的是出現(xiàn)某一類的異常不回滾rollback-for:出現(xiàn)某一類異常的時(shí)候 回滾--><tx:method name="*" read-only="false" /></tx:attributes></tx:advice><!--配置aop--><aop:config><!--配置一個(gè)切入點(diǎn)表達(dá)式--><aop:pointcut id="pt" expression="execution(* com.qf.cd.tx.UserService.*(..))"></aop:pointcut><!--配置事務(wù)增強(qiáng)--><aop:advisor advice-ref="tx-advice" pointcut-ref="pt"></aop:advisor></aop:config><!--配置aop的自動代理--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--配置包的掃描--><context:component-scan base-package="com.qf.cd.tx"></context:component-scan> </beans>
14.1.4、編寫測試類
public static void main(String[] args){ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean-tx1.xml");UserService userService = context.getBean(UserService.class);userService.update();}

14.2、Spring中通過注解的模式來實(shí)現(xiàn)事務(wù)配置

14.2.1、編寫DAO類

同上

#####14.2.2、編寫Service類

@Service public class UserService {@Autowiredprivate IUserDAO userDAO;@Transactional //這個(gè)注解使用在類上面 那么就對所有的方法添加了事務(wù)public void update(){userDAO.update();int k=1/0;}
14.2.3、編寫配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd"><!--配置數(shù)據(jù)源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="jdbc:mysql:///cd200101"></property><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><!--配置JdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><!--給JdbcTemplate配置事務(wù)--><bean id="tx-manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--使能事務(wù)的注解--><tx:annotation-driven transaction-manager="tx-manager"></tx:annotation-driven><!--配置aop的自動代理--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--配置包的掃描--><context:component-scan base-package="com.qf.cd.tx2"></context:component-scan> </beans>
14.2.4、使用那注解
@Transactional

15、事務(wù)中屬性的測試

16、Spring中Java編寫配置文件的問題

16.1、發(fā)展史

Spring在1.x時(shí)代--------最先推出的是xml編寫配置文件

Spring在2.x時(shí)代-------推出了使用注解模式代替XML配置

Spring在3.x的時(shí)代-----推出了使用Java來編寫配置文件

16.2、設(shè)計(jì)到的注解

@Configuration //這個(gè)注解就表示的是當(dāng)前的java文件是一個(gè)配置文件 //@ComponentScan(basePackages = {"com.qf.cd.javaconfig"}) //配置的是包的掃描 @PropertySource(value = "classpath:db.properties",ignoreResourceNotFound = false) //ignoreResourceNotFound這個(gè)表示的是沒找到這個(gè)文件是否要忽略 @ImportResource(value = {"classpath:bean-javaconfig.xml"}) //這個(gè)表示的是讀取外部的xml配置文件 @Value("${jdbcUrl}") //這個(gè)就表示的是將 properties中的鍵值對的值 映射到下面的成員變量中 private String jdbcUrl; @Bean //就表示的是要把下面方法返回的對象放到IOC容器中

16.3、測試的配置案例

@Configuration //這個(gè)注解就表示的是當(dāng)前的java文件是一個(gè)配置文件 //@ComponentScan(basePackages = {"com.qf.cd.javaconfig"}) //配置的是包的掃描 @PropertySource(value = "classpath:db.properties",ignoreResourceNotFound = false) //ignoreResourceNotFound這個(gè)表示的是沒找到這個(gè)文件是否要忽略 @ImportResource(value = {"classpath:bean-javaconfig.xml"}) //這個(gè)表示的是讀取外部的xml配置文件 public class AppConfig {@Value("${jdbcUrl}") //這個(gè)就表示的是將 properties中的鍵值對的值 映射到下面的成員變量中private String jdbcUrl;@Value("${driverClass}")private String driverClass;@Value("${user}")private String user;@Value("${password}")private String password;/*** 注意:* @return*/ // @Bean //就表示的是要把下面方法返回的對象放到IOC容器中 // public UserDAO userDAO(){ // return new UserDAO(); // }// @Bean // public UserService userService(){ // return new UserService(); // }// @Bean // public DruidDataSource dataSource(){ // DruidDataSource druidDataSource = new DruidDataSource(); // //1001個(gè)步驟 // druidDataSource.setUrl(jdbcUrl); // druidDataSource.setDriverClassName(driverClass); // druidDataSource.setUsername(user); // druidDataSource.setPassword(password); // return druidDataSource; // } }

總結(jié)

以上是生活随笔為你收集整理的Spring的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。