Springmvc notes (from MOOC), springmvc
1. Preparations: springmvc-related jar packages.
2. Here we first use eclipse for operations.
First, let's look at an interface programming. All the knowledge points in the future are drawn out through this interface programming.
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 "returned from the interface is:" + 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 ("Dr. Sisi "));}}
This place can be run in the form of an interface.
Next let's take a look at how to run this project using springmc.
Because we are not a web project and are not configured by configuring web. xml, we read the springmvc configuration file.
Only configuration files can be read by hand.
GetBeanBase. java
1 package gys; 2 3 import org. springframework. context. support. classPathXmlApplicationContext; 4 // create a springmvc container to obtain the bean in the configuration file. 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 // create spring container 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 ("input parameter"); 11 end (); 12 13} 14 15 public static void main (String [] args) {16 Run run = new Run (); 17 run. testHello (); 18} 19 20}
In this way, the same output can be achieved. GetBeanBase here is used in many places.
Spring injection: when the Spring container is started to load bean configurations, values are assigned to the variables.
There are two common injection methods:
Set Injection
Constructor Injection
1. Set the injection:
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 ("save data:" + 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 the injection. The set method spring will automatically call here, you do not need to manually call 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 accept parameter:" + 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: schem ALocation = "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 11 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http: // www. Springframework.org/schema/tx/spring-tx.xsd13 http://www.springframework.org/schema/aop14 Co., http://www.springframework.org/schema/aop/spring-aop.xsd. "> 15 16 <! -- Set injection --> 17 <bean id = "injectionService" class = "gys. service. InjectionServiceImpl"> 18 <! -- The InjectionServiceImpl class must have a property name with the type of ref. springmvc will automatically call the set Method of this property. --> 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 ("this is the data to be saved"); 12 end (); 13} 14 public static void main (String [] args) {15 Run run = new Run (); 16 run. testSetter (); 17} 18 19}
2. constructor injection:
Make a change to the above Code:
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 // constructor injects 10 public InjectionServiceImpl (InjectionDAO injectionDAO) {11 this. injectionDAO = injectionDAO; 12} 13 14 @ Override15 public void save (String info) {16 System. out. println ("service accept parameter:" + 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: schem ALocation = "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 11 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http: // www. Springframework.org/schema/tx/spring-tx.xsd13 http://www.springframework.org/schema/aop14 Co., http://www.springframework.org/schema/aop/spring-aop.xsd. "> 15 16 <! -- Construct an injection --> 17 <bean id = "injectionService" class = "gys. service. InjectionServiceImpl"> 18 <! -- In the class InjectionServiceImpl, there must be an attribute name and a constructor, the parameter value type of this constructor is 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 ("this is the data to be saved"); 14 end (); 15} 16 17 public static void main (String [] args) {18 Run run = new Run (); 19 run. testCons (); 20} 21 22}
It's off duty. It's not over yet ......