springmvc笔记(来自慕课网)

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

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

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

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.xml来配置,读取springmvc配置文件.

只能手写读取配置文件.

getBeanBase.java

 1 package gys;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 //创建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             //创建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/mvc
10         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
11         http://www.springframework.org/schema/tx
12         http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/aop
14         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("传入的参数"));
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.设置注入:

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     @Override
16     public void save(String info) {
17         System.out.println("service接受参数:"+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/mvc
10         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
11         http://www.springframework.org/schema/tx
12         http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/aop
14         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.xml");
 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     @Override
15     public void save(String info) {
16         System.out.println("service接受参数:"+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/mvc
10         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
11         http://www.springframework.org/schema/tx
12         http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/aop
14         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.xml");
 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 }

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

原文地址:https://www.cnblogs.com/guoyansi19900907/p/4817198.html