Spring MVC Learning Starter Notes

Source: Internet
Author: User

Using the spring steps
    1. Add dependency
    2. Creating an XML file
    3. Configuration (classes that need to be managed) bean
    4. Instantiating a context class
    5. Getbean Object

To create a new MAVEN project using idea, add dependencies in Pom.xml:

<Dependencies>        <!--Https://mvnrepository.com/artifact/org.springframework/spring-context -        <Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-context</Artifactid>            <version>4.3.13.RELEASE</version>        </Dependency></Dependencies>

Create an entity, Student.java:

 Package entity;  Public class Student {    publicvoid  hello () {        System.out.println ("Hello SPring mvc! ");    }}

Select Project Right--Create a spring Config:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">        <BeanID= "Stu"class= "entity. Student "></Bean></Beans>

Write a Main.java class under the entity package for testing:

 Packageentity;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {//Superclass ApplicationContext (is an interface)--so the new subclass is required//Classpath Class Path = = is put together with the code, under the main folderApplicationContext ApplicationContext =NewClasspathxmlapplicationcontext ("Springmvc.xml"); //ctrl+h (shortcut key)--See type hierarchyStudent Student = (Student) applicationcontext.getbean ("Stu"); The ID name of the XML Student.hello ();}

Project directory Structure:

Problem:

  

A: The delimiter can be mixed, can be used, respectively, (space) three symbols as separators, and: will be an error, the test code is as follows:

The code in the XML:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">    <BeanID= "Stu"name= "Student S,st"class= "entity. Student "></Bean>    <!--can be mixed with -    <!--<bean id= "Stu" Name= "Student,s" class= "entity. Student "></bean> <bean id=" Stu "Name=" Student;s "class=" entity. Student "></bean> -</Beans>

Code for the Main.java test:

New Classpathxmlapplicationcontext ("Springmvc.xml");         // ctrl+h-to-type hierarchical XSD        /* Student Student = (Student) applicationcontext.getbean ("Stu");        Student.hello ();        Student student1 = (Student) applicationcontext.getbean ("Student");        Student1.hello ();        Student Student2 = (Student) Applicationcontext.getbean ("s");        Student2.hello ();        Student Student3 = (Student) Applicationcontext.getbean ("St");        Student3.hello (); */

  <!--Create objects in three forms: 1. Direct new;2. Indirect use of factory class new-why use:; 3. Implement Interface--

The first: the one mentioned above

The second type:

Entity code:

 Package entity;  Public class Student {      publicstatic  Student Hello () {        System.out.println (" Hello SPring mvc! ");         return New Student ();    }}

XML code:

<id= "XX"  class= "entity." Student "  factory-method=" Hello "></bean>

Main test code:

Student student4 = (Student) applicationcontext.getbean ("xx");

The third type:

Entity code:

 Public Student Hello () {       System.out.println ("Hello SPring mvc! ");        return New Student ();   }

XML code (method one):

 <  bean  id  = "Factory"   class  = "entity. Student " ></ bean  >  <  bean  Span style= "COLOR: #ff0000" >id  = "yy"   class  = "entity. Student "  Factory-bean  =" Factory " Span style= "COLOR: #ff0000" > Factory-method  = "Hello"  ></ bean  >  

XML code (method two):

< can I write myself!--xml: no? What about aliases? Yes-
<BeanID= "Factory"class= "entity. Student "name= "Student S"></Bean>    <BeanID= "yy"class= "entity. Student "Factory-bean= "S"Factory-method= "Hello"></Bean>

Main test code:

Student student5 = (Student) applicationcontext.getbean ("yy");

  <!--XML configuration can go to ID, how to write--

Entity code:
 Public Static Student Hello () {        System.out.println ("Hello SPring mvc! ");         return New Student ();    }

XML code:

<class= "entity. Student "></bean>

Main test code:

Student student6 =  Applicationcontext.getbean (Student.  Class); Student6.hello ();



Spring MVC Learning Starter Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.