1、配置文件applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd" >
<bean id="helloWorldImpl" class="com.cc.dao.impl.HelloWorldImpl"></bean>
</beans>
2、创建一个接口和一个实现类
接口:
public interface HelloWorld {
public String sayHello();
}
实现类:
import com.cc.dao.HelloWorld;
public class HelloWorldImpl implements HelloWorld{
public String sayHello() {
return "Hello World!!!";
}
}
3、测试类
public class testOne {
@Test
public void test01(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld) app.getBean("helloWorldImpl");
System.out.println(hello.sayHello());
}
}
最后需要导入的包: