1.准备工作:sPRingmvc相关的jar包.
2.这里我们先用eclipse来操作.
首先看一个接口编程,后面的所有知识点都是通过这个接口编程引出的.
OneInterface.java
1packagegys;23publicinterfaceOneInterface {4String hello(String world);5}
OneInterfaceImpl.java
1packagegys;23publicclassOneInterfaceImplimplementsOneInterface{45@Override6publicString hello(String world) {7return"从接口返回的是:"+world;8}910}
Run.java
packagegys;publicclassRun{publicstaticvoidmain(String[] args) {
OneInterface oif=newOneInterfaceImpl();
System.out.println(oif.hello("思思博士"));
}
}
这个地方可以通过接口的形式跑起来了.
下面看看使用springmc方式如何来跑起来这个项目
因为我们不是web项目,没有通过配置web.xml来配置,读取springmvc配置文件.
只能手写读取配置文件.
getBeanBase.java
1packagegys;23importorg.springframework.context.support.ClassPathXmlapplicationContext;4//创建springmvc容器,获取配置文件中的bean.5publicclassGetBeanBase {6privateClassPathXmlApplicationContext context;7privateString springXmlpath;8publicGetBeanBase(){};910publicGetBeanBase(String springXmlPath){11this.springXmlpath=springXmlPath;12}1314publicvoidstart(){15if(springXmlpath.equals("")||springXmlpath==null||springXmlpath.isEmpty()){16springXmlpath="classpath*:spring-*.xml";17}18try{19//创建spring容器20context=newClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));21context.start();22}catch(Exception e) {23e.printStackTrace();24}25}2627publicvoidend(){28context.destroy();29}3031@SuppressWarnings("unchecked")32protected<TextendsObject>T getBen(String beanId){33return(T) context.getBean(beanId);34}3536protected<TextendsObject> T GetBeanBase(Class<T>clazz){37returncontext.getBean(clazz);38}39}
spring-ioc.xml
1<?xml version="1.0" encoding="UTF-8"?>2<beansxmlns="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"3xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"4xsi:schemaLocation="5http://www.springframework.org/schema/beans6http://www.springframework.org/schema/beans/spring-beans.xsd7http://www.springframework.org/schema/context8http://www.springframework.org/schema/context/spring-context.xsd9http://www.springframework.org/schema/mvc10http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd11http://www.springframework.org/schema/tx12http://www.springframework.org/schema/tx/spring-tx.xsd13http://www.springframework.org/schema/aop14http://www.springframework.org/schema/aop/spring-aop.xsd">1516<beanid="oneInterface"class="gys.OneInterfaceImpl"></bean>17</beans>
Run.java
1packagegys;23publicclassRunextendsGetBeanBase{4publicRun(){5super("classpath*:spring-ioc.xml");6start();7}8publicvoidtestHello(){9OneInterface oneInterface=super.getBen("oneInterface");10System.out.println(oneInterface.hello("传入的参数"));11end();1213}1415publicstaticvoidmain(String[] args) {16Run run=newRun();17run.testHello();18}1920}
通过这个方式也是可以做到同样的输出.这里的GetBeanBase在后面很多地方使用.
spring注入:在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
常用的两种注入方式:
设置注入
构造注入
1.设置注入:
InjectionDao.java
packagegys.dao;publicinterfaceInjectionDAO {voidsave(String info);
}
InjectionDAOImpl.java
packagegys.dao;publicclassInjectionDAOImplimplementsInjectionDAO{
@Overridepublicvoidsave(String info) {
System.out.println("保存数据:"+info);
}
}
InjectionService.java
packagegys.service;publicinterfaceInjectionService {publicvoidsave(String info);
}
InjectionServiceImpl.java
1packagegys.service;23importgys.dao.InjectionDAO;45publicclassInjectionServiceImplimplementsInjectionService{67privateInjectionDAO injectionDAO;89//设置注入,这里的set方法spring会自动调用,无需手动调用10publicvoidsetInjectionDAO(InjectionDAO injectionDAO) {11this.injectionDAO =injectionDAO;12}131415@Override16publicvoidsave(String info) {17System.out.println("service接受参数:"+info);18info=info+":"+this.hashCode();19injectionDAO.save(info);20}21}
spring-ioc.xml
1<?xml version="1.0" encoding="UTF-8"?>2<beansxmlns="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"3xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"4xsi:schemaLocation="5http://www.springframework.org/schema/beans6http://www.springframework.org/schema/beans/spring-beans.xsd7http://www.springframework.org/schema/context8http://www.springframework.org/schema/context/spring-context.xsd9http://www.springframework.org/schema/mvc10http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd11http://www.springframework.org/schema/tx12http://www.springframework.org/schema/tx/spring-tx.xsd13http://www.springframework.org/schema/aop14http://www.springframework.org/schema/aop/spring-aop.xsd">1516<!--设置注入-->17<beanid="injectionService"class="gys.service.InjectionServiceImpl">18<!--InjectionServiceImpl类中必须有一个属性name,类型是ref,springmvc会自动调用这个属性的set方法.-->19<propertyname="injectionDAO"ref="injectionDAO"></property>20</bean>2122<beanid="injectionDAO"class="gys.dao.InjectionDAOImpl"></bean>23242526</beans>
Run.java
1packagegys;23importgys.service.InjectionService;4publicclassRunextendsGetBeanBase{5publicRun(){6super("classpath*:spring-ioc.xml");7start();8}9publicvoidtestSetter(){10InjectionService service=super.getBen("injectionService");11service.save("这是要保存的数据");12end();13}14publicstaticvoidmain(String[] args) {15Run run=newRun();16run.testSetter();17}1819}
2.构造注入:
对上面的代码做一下改变:
InjectionServiceImpl.java
1packagegys.service;23importgys.dao.InjectionDAO;45publicclassInjectionServiceImplimplementsInjectionService{67privateInjectionDAO injectionDAO;89//构造器注入10publicInjectionServiceImpl(InjectionDAO injectionDAO){11this.injectionDAO=injectionDAO;12}1314@Override15publicvoidsave(String info) {16System.out.println("service接受参数:"+info);17info=info+":"+this.hashCode();18injectionDAO.save(info);19}20}
spring-ioc.xml
1<?xml version="1.0" encoding="UTF-8"?>2<beansxmlns="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"3xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"4xsi:schemaLocation="5http://www.springframework.org/schema/beans6http://www.springframework.org/schema/beans/spring-beans.xsd7http://www.springframework.org/schema/context8http://www.springframework.org/schema/context/spring-context.xsd9http://www.springframework.org/schema/mvc10http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd11http://www.springframework.org/schema/tx12http://www.springframework.org/schema/tx/spring-tx.xsd13http://www.springframework.org/schema/aop14http://www.springframework.org/schema/aop/spring-aop.xsd">1516<!--构造注入-->17<beanid="injectionService"class="gys.service.InjectionServiceImpl">18<!--在类InjectionServiceImpl中有一个属性name,还必须必须有一个构造器,这个构造器的参数是name值 类型是ref-->19<constructor-argname="injectionDAO"ref="injectionDAO"/>20</bean>2122<beanid="injectionDAO"class="gys.dao.InjectionDAOImpl"></bean>23242526</beans>
Run.java
1packagegys;23importgys.service.InjectionService;45publicclassRunextendsGetBeanBase{6publicRun(){7super("classpath*:spring-ioc.xml");8start();9}1011publicvoidtestCons(){12InjectionService service=super.getBen("injectionService");13service.save("这是要保存的数据");14end();15}1617publicstaticvoidmain(String[] args) {18Run run=newRun();19run.testCons();20}2122}
下班了,未完待续......