麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

springmvc筆記(來自慕課網(wǎng))

2019-11-14 15:36:56
字體:
供稿:網(wǎng)友

1.準(zhǔn)備工作:sPRingmvc相關(guān)的jar包.

2.這里我們先用eclipse來操作.

首先看一個(gè)接口編程,后面的所有知識點(diǎn)都是通過這個(gè)接口編程引出的.

OneInterface.java

1 package gys;2 3 public interface OneInterface {4     String hello(String world);5 }

OneInterfaceImpl.java

 1 package gys; 2  3 public class OneInterfaceImpl implements OneInterface{ 4  5     @Override 6     public String hello(String world) { 7         return "從接口返回的是:"+world; 8     } 9     10 }

Run.java

package gys;public class Run{            public static void main(String[] args) {        OneInterface oif=new OneInterfaceImpl();        System.out.println(oif.hello("思思博士"));    }    }

這個(gè)地方可以通過接口的形式跑起來了.

下面看看使用springmc方式如何來跑起來這個(gè)項(xiàng)目

因?yàn)槲覀儾皇莣eb項(xiàng)目,沒有通過配置web.xml來配置,讀取springmvc配置文件.

只能手寫讀取配置文件.

getBeanBase.java

 1 package gys; 2  3 import org.springframework.context.support.ClassPathXmlapplicationContext; 4 //創(chuàng)建springmvc容器,獲取配置文件中的bean. 5 public class GetBeanBase { 6     private ClassPathXmlApplicationContext context; 7     private String springXmlpath; 8     public GetBeanBase(){}; 9     10     public GetBeanBase(String springXmlPath){11         this.springXmlpath=springXmlPath;12     }13     14     public void start(){15         if(springXmlpath.equals("")||springXmlpath==null||springXmlpath.isEmpty()){16             springXmlpath="classpath*:spring-*.xml";17         }18         try {19             //創(chuàng)建spring容器20             context=new ClassPathXmlApplicationContext(springXmlpath.split("[,//s]+"));21             context.start();22         } catch (Exception e) {23             e.printStackTrace();24         }25     }26     27     public void end(){28         context.destroy();29     }30     31     @SuppressWarnings("unchecked")32     protected <T extends Object> T getBen(String beanId){33         return (T) context.getBean(beanId);34     }35 36     protected <T extends Object> T GetBeanBase(Class<T> clazz){37         return context.getBean(clazz);38     }39 }

spring-ioc.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" 3     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 4     xsi:schemaLocation="   5         http://www.springframework.org/schema/beans  6         http://www.springframework.org/schema/beans/spring-beans.xsd  7         http://www.springframework.org/schema/context  8         http://www.springframework.org/schema/context/spring-context.xsd  9         http://www.springframework.org/schema/mvc10         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        11         http://www.springframework.org/schema/tx12         http://www.springframework.org/schema/tx/spring-tx.xsd13         http://www.springframework.org/schema/aop14         http://www.springframework.org/schema/aop/spring-aop.xsd">15 16         <bean id="oneInterface" class="gys.OneInterfaceImpl"></bean>17 </beans>

Run.java

 1 package gys; 2  3 public class Run extends GetBeanBase{ 4     public Run(){ 5         super("classpath*:spring-ioc.xml"); 6         start(); 7     } 8     public void testHello(){ 9         OneInterface oneInterface=super.getBen("oneInterface");10         System.out.println(oneInterface.hello("傳入的參數(shù)"));11         end();12         13     }14     15     public static void main(String[] args) {16         Run run=new Run();17         run.testHello();18     }19     20 }

通過這個(gè)方式也是可以做到同樣的輸出.這里的GetBeanBase在后面很多地方使用.

spring注入:在啟動(dòng)Spring容器加載bean配置的時(shí)候,完成對變量的賦值行為
常用的兩種注入方式:
        設(shè)置注入
        構(gòu)造注入

1.設(shè)置注入:

InjectionDao.java

package gys.dao;public interface InjectionDAO {    void save(String info);}

InjectionDAOImpl.java

package gys.dao;public class InjectionDAOImpl implements InjectionDAO{    @Override    public void save(String info) {        System.out.println("保存數(shù)據(jù):"+info);            }}

InjectionService.java

package gys.service;public interface InjectionService {    public void save(String info);}

InjectionServiceImpl.java

 1 package gys.service; 2  3 import gys.dao.InjectionDAO; 4  5 public class InjectionServiceImpl implements InjectionService{ 6  7     private InjectionDAO injectionDAO; 8              9     //設(shè)置注入,這里的set方法spring會自動(dòng)調(diào)用,無需手動(dòng)調(diào)用10     public void setInjectionDAO(InjectionDAO injectionDAO) {11         this.injectionDAO = injectionDAO;12     }13     14     15     @Override16     public void save(String info) {17         System.out.println("service接受參數(shù):"+info);18         info=info+":"+this.hashCode();19         injectionDAO.save(info);20     }21 }

spring-ioc.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" 3     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 4     xsi:schemaLocation="   5         http://www.springframework.org/schema/beans  6         http://www.springframework.org/schema/beans/spring-beans.xsd  7         http://www.springframework.org/schema/context  8         http://www.springframework.org/schema/context/spring-context.xsd  9         http://www.springframework.org/schema/mvc10         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        11         http://www.springframework.org/schema/tx12         http://www.springframework.org/schema/tx/spring-tx.xsd13         http://www.springframework.org/schema/aop14         http://www.springframework.org/schema/aop/spring-aop.xsd">15         16     <!-- 設(shè)置注入 -->17     <bean id="injectionService" class="gys.service.InjectionServiceImpl">18     <!--InjectionServiceImpl類中必須有一個(gè)屬性name,類型是ref,springmvc會自動(dòng)調(diào)用這個(gè)屬性的set方法. -->19         <property name="injectionDAO" ref="injectionDAO"></property>20     </bean>21     22     <bean id="injectionDAO" class="gys.dao.InjectionDAOImpl"></bean>23     24 25         26 </beans>

Run.java

 1 package gys; 2  3 import gys.service.InjectionService; 4 public class Run extends GetBeanBase{ 5     public Run(){ 6         super("classpath*:spring-ioc.xml"); 7         start(); 8     } 9     public void testSetter(){10         InjectionService service=super.getBen("injectionService");11         service.save("這是要保存的數(shù)據(jù)");12         end();13     }14     public static void main(String[] args) {15         Run run=new Run();16         run.testSetter();17     }18     19 }

2.構(gòu)造注入:

對上面的代碼做一下改變:

InjectionServiceImpl.java

 1 package gys.service; 2  3 import gys.dao.InjectionDAO; 4  5 public class InjectionServiceImpl implements InjectionService{ 6  7     private InjectionDAO injectionDAO; 8      9     //構(gòu)造器注入10     public InjectionServiceImpl(InjectionDAO injectionDAO){11         this.injectionDAO=injectionDAO;12     }13     14     @Override15     public void save(String info) {16         System.out.println("service接受參數(shù):"+info);17         info=info+":"+this.hashCode();18         injectionDAO.save(info);19     }20 }

spring-ioc.xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <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" 3     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 4     xsi:schemaLocation="   5         http://www.springframework.org/schema/beans  6         http://www.springframework.org/schema/beans/spring-beans.xsd  7         http://www.springframework.org/schema/context  8         http://www.springframework.org/schema/context/spring-context.xsd  9         http://www.springframework.org/schema/mvc10         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        11         http://www.springframework.org/schema/tx12         http://www.springframework.org/schema/tx/spring-tx.xsd13         http://www.springframework.org/schema/aop14         http://www.springframework.org/schema/aop/spring-aop.xsd">15     16     <!-- 構(gòu)造注入 -->17     <bean id="injectionService" class="gys.service.InjectionServiceImpl">18     <!--在類InjectionServiceImpl中有一個(gè)屬性name,還必須必須有一個(gè)構(gòu)造器,這個(gè)構(gòu)造器的參數(shù)是name值   類型是ref -->19         <constructor-arg name="injectionDAO" ref="injectionDAO" />20     </bean>21     22     <bean id="injectionDAO" class="gys.dao.InjectionDAOImpl"></bean>23     24 25         26 </beans>

 

Run.java

 1 package gys; 2  3 import gys.service.InjectionService; 4  5 public class Run extends GetBeanBase{ 6     public Run(){ 7         super("classpath*:spring-ioc.xml"); 8         start(); 9     }10     11     public void testCons(){12         InjectionService service=super.getBen("injectionService");13         service.save("這是要保存的數(shù)據(jù)");14         end();15     }16     17     public static void main(String[] args) {18         Run run=new Run();        19         run.testCons();20     }21     22 }

下班了,未完待續(xù)......

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美日韩免费在线观看视频 | 91精品国产综合久久男男 | 日韩中文字幕一区二区三区 | 羞羞视频免费网站 | 久久精品视频国产 | 成人毛片视频在线观看 | 成人在线视频一区 | 国产毛片毛片 | 九九色精品| 99精品无人区乱码在线观看 | 精品国产一区二区亚洲人成毛片 | 老师你怎么会在这第2季出现 | 欧美三级美国一级 | 高颜值美女啪啪 | 国产毛片在线 | 久久久一区二区三区精品 | 精品视频 久久久 | av免费在线免费观看 | 在线免费黄色网 | 亚洲五码在线观看视频 | 在线观看国产日韩 | 做爰xxxⅹ性护士hd在线 | 亚洲天堂在线电影 | 久久精品国产99久久6动漫亮点 | 超碰97人人艹 | 黄色va视频 | 国产精品久久久久久久久久东京 | 日产精品久久久一区二区开放时间 | 国产精品视频不卡 | 九九热在线精品视频 | 久草在线高清视频 | 密室逃脱第一季免费观看完整在线 | 国产精品av久久久久久久久久 | 91中文字幕在线观看 | 成人毛片免费 | 草草视频免费观看 | 久久国产一二区 | 3xxx| 一区二区久久 | 欧美精品久久久久久久久老牛影院 | 最近国产中文字幕 |