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ù)......
新聞熱點(diǎn)
疑難解答
圖片精選