- Create Dynamic Webproject project
- Import the relevant coordinates (jar package) (spring-context, tomcat server, and
- Create an ApplicationContext.xml file under the sc directory (the header file that can be imported can be found on the Internet
- Create a dao package under the sc directory, create the interface TestDao (plus the method sayHello()) below, and use TestDaoimpl for the implementation class (inherit the method sayHello() in the TestDao package).
- Add
< in the ApplicationContext.xml filebean id="test" class="com.dao.TestDaoimpl"> <bean>
- Create an implementation class Test.java under the sc directory and call the getBean() method
- Add to the implementation class
package com.demo.controller;
import com.demo.dao.impl.TestDaoimpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestController {
public static void main(String[] args) {
ApplicationContext app= new ClassPathXmlApplicationContext("ApplicationContext.xml");
TestDaoimpl testDaoimpl= (TestDaoimpl) app.getBean( "test" );
testDaoimpl.sayHello();
}
}
Note: If an error is reported, it may be that the jar has not been imported, and the jar package needs to be imported again