星空网 > 软件开发 > Java

springmvc笔记(来自慕课网)

1.准备工作:springmvc相关的jar包.

2.这里我们先用eclipse来操作.

首先看一个接口编程,后面的所有知识点都是通过这个接口编程引出的.

springmvc笔记(来自慕课网)

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("思思博士"));  }  }

这个地方可以通过接口的形式跑起来了.

下面看看使用springmc方式如何来跑起来这个项目

因为我们不是web项目,没有通过配置web.

只能手写读取配置文件.

springmvc笔记(来自慕课网)

getBeanBase.java

 1 package gys; 2  3 import org.springframework.context.support.ClassPath 4 //创建springmvc容器,获取配置文件中的bean. 5 public class GetBeanBase { 6   private ClassPath 7   private String spring 8   public GetBeanBase(){}; 9   10   public GetBeanBase(String spring11     this.springspring12   }13   14   public void start(){15     if(springnull||spring16       spring;17     }18     try {19       //创建spring容器20       context=new ClassPath));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.

 1 <??> 2 <beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p" ="http://www.springframework.org/schema/context" 3   ="http://www.springframework.org/schema/mvc" ="http://www.springframework.org/schema/tx" ="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.); 6     start(); 7   } 8   public void testHello(){ 9     OneInterface oneInterface=super.getBen("oneInterface");10     System.out.println(oneInterface.hello("传入的参数"));11     end();12     13   }14   15   public static void main(String[] args) {16     Run run=new Run();17     run.testHello();18   }19   20 }

通过这个方式也是可以做到同样的输出.这里的GetBeanBase在后面很多地方使用.

spring注入:在启动Spring容器加载bean配置的时候,完成对变量的赋值行为
常用的两种注入方式:
        设置注入
        构造注入

1.设置注入:

springmvc笔记(来自慕课网)

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("保存数据:"+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   //设置注入,这里的set方法spring会自动调用,无需手动调用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接受参数:"+info);18     info=info+":"+this.hashCode();19     injectionDAO.save(info);20   }21 }

spring-ioc.

 1 <??> 2 <beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p" ="http://www.springframework.org/schema/context" 3   ="http://www.springframework.org/schema/mvc" ="http://www.springframework.org/schema/tx" ="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   <!-- 设置注入 -->17   <bean id="injectionService" class="gys.service.InjectionServiceImpl">18   <!--InjectionServiceImpl类中必须有一个属性name,类型是ref,springmvc会自动调用这个属性的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.); 7     start(); 8   } 9   public void testSetter(){10     InjectionService service=super.getBen("injectionService");11     service.save("这是要保存的数据");12     end();13   }14   public static void main(String[] args) {15     Run run=new Run();16     run.testSetter();17   }18   19 }

2.构造注入:

对上面的代码做一下改变:

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   //构造器注入10   public InjectionServiceImpl(InjectionDAO injectionDAO){11     this.injectionDAO=injectionDAO;12   }13   14   @Override15   public void save(String info) {16     System.out.println("service接受参数:"+info);17     info=info+":"+this.hashCode();18     injectionDAO.save(info);19   }20 }

spring-ioc.

 1 <??> 2 <beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p" ="http://www.springframework.org/schema/context" 3   ="http://www.springframework.org/schema/mvc" ="http://www.springframework.org/schema/tx" ="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   <!-- 构造注入 -->17   <bean id="injectionService" class="gys.service.InjectionServiceImpl">18   <!--在类InjectionServiceImpl中有一个属性name,还必须必须有一个构造器,这个构造器的参数是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.); 8     start(); 9   }10   11   public void testCons(){12     InjectionService service=super.getBen("injectionService");13     service.save("这是要保存的数据");14     end();15   }16   17   public static void main(String[] args) {18     Run run=new Run();    19     run.testCons();20   }21   22 }

下班了,未完待续......

 




原标题:springmvc笔记(来自慕课网)

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Tiktok玩家第112篇:美区红利到底在哪?--和大卖交流后的思考:https://www.kjdsnews.com/a/1698235.html
SHEIN、TEMU引领全球空运量,航空货运最糟糕时期已过去?:https://www.kjdsnews.com/a/1698236.html
3c爆款大揭秘:明年的选品方向有了:https://www.kjdsnews.com/a/1698237.html
当前卖家面临着这三大广告误区(终章):https://www.kjdsnews.com/a/1698238.html
资质审核 重新激活账户步骤:https://www.kjdsnews.com/a/1698239.html
2023十大营销事件盘点:https://www.kjdsnews.com/a/1698240.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流