码迷,mamicode.com
首页 > 编程语言 > 详细

关于spring

时间:2015-08-30 08:45:38      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

控制反转

     应用本身不负责依赖对象的创建及维护,而由外部容器负责。

 

技术分享

 

第一个实例:

1.导包

2.beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
</beans>

 3单元测试

技术分享

 

技术分享

 

package cn.itcast.service.impl;

import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {

    public void save(){
        System.out.println("我是save()方法");
    }
}

 

 

package cn.itcast.service;

public interface PersonService {

    public void save();

}

 

 

package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;

public class SpringTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @Test public void instanceSpring(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        PersonService personService = (PersonService)ctx.getBean("personService");
        personService.save();
    }
}

 

关于spring

标签:

原文地址:http://www.cnblogs.com/rixiang/p/4770300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!