首页 > 编程语言 > 详细

spring ico

时间:2016-04-14 17:56:03      阅读:195      评论:0      收藏:0      [点我收藏+]

代码非常简单。如果缺少jar包将导致报错。

需要5个spring jar包和1个logging jar,否则报错。 

org.springframework.asm.jar
org.springframework.core.jar
org.springframework.beans.jar
org.springframework.context.jar
org.springframework.expression.jar

定义一个接口,一个实现类。java project项目。

package ioc;
public interface HelloApi {
    public void sayHello();  
}

 

package ioc;
public class Hello implements HelloApi{
    @Override
    public void sayHello() {  
        System.out.println("Hello World!");  
 } 
}
package ioc;

import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class HelloTest {    
    public static  void main(String[] args) {  
        ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");  
         HelloApi helloApi = context.getBean("hello", HelloApi.class);  
         helloApi.sayHello();         
  }  
}

  

<?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"  
xsi:schemaLocation="  
http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  <!-- id 表示你这个组件的名字,class表示组件类 -->  
<bean id="hello" class="ioc.Hello"></bean>  
</beans> 

 不清楚如何找到该xml文件。

 ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml"); 

 

spring ico

原文:http://www.cnblogs.com/lucika/p/spring.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!