Create a package named com.mengma.ioc in the project’s SRC directory, then create an interface named PersonDao in that package and add an Add () method to the interface, as shown below. Download: Play Spring family barrel Package com.mengma.ioc; public interface PersonDao { public void add(); Create the PersonDao implementation class PersonDaoImpl under the com.mengma.ioc package and edit it as shown below. package com.mengma.ioc; Public class PersonDaoImpl implements PersonDao {@override public void add() {system.out.println (” Save () implements PersonDao.” ); In the above code, the PersonDaoImpl class implements the Add () method in the PersonDao interface and executes the output statement when the method is invoked. 4. Create the Spring configuration file. In the SRC directory, create the Spring core configuration file applicationContext. XML.
In the above code, lines 2 to 5 are Spring constraint configuration, and line 7 is to create an instance of a bean in the Spring container with id personDao, where ID represents the unique identifier in the file, The class attribute represents the real fully qualified class name (package name + class name) that specifies the Bean to be instantiated.
It is important to note that the Spring configuration file name is customizable, and it is usually named applicationContext.xml (or bean.xml). Create the test class FirstTest under the com.mengma.ioc package and add a method named test1() to the class, as shown below. 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() {// Define the path to the Spring configuration file. String xmlPath = “ApplicationContext.xml “; / / initialize the Spring container, loading the configuration file ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext (xmlPath); PersonDao personDao = (personDao) ApplicationContext.getBean (“personDao”); // Call personDao’s add() method persondao.add (); }} This code defines the path to the Spring configuration file, creates the Spring container, obtains the personDao instance from the Spring container, and finally calls the save() method of the instance.