一:准本工作(下载需要的jar包)
1.下载准备Spring-framework-4.2.0
链接为:
http://repo.springsource.org/libs-release-local/org/springframework/spring/
截图:
2.具体的spring-framework4.2.0
3.下载log
链接为:
http://commons.apache.org/proper/commons-logging/download_logging.cgi
截图:
二:建项目
1.新建普通的java项目
2.新建lib文件夹,并将jar包复制粘贴进去
3.将lib包进行buid path
然后将需要的jar从firstSpring中导入
然后reply和ok
4.这时的效果
5.新建包
类包:
XML包
6.新建类
第一个接口
1 package main.java.com.sommer.learn; 2 3 public interface HelloWorld { 4 public String sayHi(); 5 }
实现类
1 package main.java.com.sommer.learn; 2 3 public class HelloWorldImpl implements HelloWorld{ 4 @Override 5 public String sayHi() { 6 return "hahahahahha"; 7 } 8 9 }
7.新建xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 7 8 <bean id="helloWorld" class="main.java.com.sommer.learn.HelloWorldImpl"></bean> 9 </beans>
8.新建主函数
1 package main.java.com.sommer.learn; 2 import org.springframework.context.ApplicationContext; 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class Main { 6 public static void main(String[] args) { 7 ApplicationContext apc = new ClassPathXmlApplicationContext("springXML/HelloWorld.xml"); 8 HelloWorld hello = apc.getBean("helloWorld",HelloWorld.class); 9 System.out.println(hello.sayHi()); 10 } 11 }
9.运行
时间: 2024-10-12 03:56:53