Wu Yuxiong - a natural framework for the development of natural JAVA SPRING study notes: First Spring program

1 . Create a project
Creating Web projects springDemo01 in MyEclipse, copy the desired packet to the Spring Framework lib JAR directory of the project, and add to the class path, after adding FIG items

 

 

2 Create PersonDao Interface
Src directory created in a project called com.mengma.ioc the package, and then create an interface called the PersonDao in the packet, and adds an add () method, as shown in the interface.
package com.mengma.ioc;
public interface PersonDao {
    public void add();
}
3 . Creating an interface implementation class PersonDaoImpl
Create implementation class PersonDaoImpl PersonDao at com.mengma.ioc package, edited as shown below.
package com.mengma.ioc;
public class PersonDaoImpl implements PersonDao {
    @Override
    public void add() {
        System.out.println ( "the Save () is executed ..." );
    }
} 
Above code, PersonDaoImpl add PersonDao class implements the interface () method, and the output statement is executed when the method is called.
4 . Create a Spring configuration file
Create Spring's core configuration file applicationContext.xml in the src directory, as shown in the following edited.
<?xml version="1.0" encoding="UTF-8"?>
<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    <! - create the class instance of an object by the Spring container ->
    <bean id="personDao" class="com.mengma.ioc.PersonDaoImpl" />
</ beans> 
the above code, the first 2 to 5 lines of code constraint is arranged Spring, Line 7 represents the creation of a bean id in Example personDao Spring container, where id is a unique identifier file, class attributes indicates that the specified real need to instantiate Bean fully qualified class name (package name + class name).
It should be noted, Spring configuration file name can be customized, under normal circumstances, will the configuration file named applicationContext.xml (or bean.xml).
5 . Writing test class
Creating a Test Class FirstTest at com.mengma.ioc packet, and adds a method named test1 () in the class, as shown in the following edited.
package com.mengma.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FirstTest {
    @Test
    public  void testl () {
         // definition of the configuration file Spring Path 
        String XMLPath = "the applicationContext.xml" ;
         // initialize Spring container, load the configuration files 
        the ApplicationContext applicationContext = new new the ClassPathXmlApplicationContext (
                xmlPath);
        // Get instance through the vessel PersonDao 
        the PersonDao PersonDao = (the PersonDao) applicationContext
                .getBean ( "personDao" );
         // call personDao the add () method 
        personDao.add ();
    }
}
The above code, the first defined path Spring configuration file, and then creating Spring container, followed by the acquired Spring container personDao example, an example of the last call save () method.
6 . Run the project and view the results
After using JUnit test test1 () method is successful, as shown in FIG console output

The output can be seen, the program has been successfully exported the "save () is executed ..." statement. During program execution, by creating objects is not new a class to complete, but is managed by the Spring container to achieve. This is the working mechanism of the Spring IoC container ideas.

 

Guess you like

Origin www.cnblogs.com/tszr/p/12128846.html