Spring入门案例 IoC

Spring入门案例 Ioc

Spring的两个核心之一,IOC(Inversion of Control)控制反转。

控制反转的意义在于,以前需要实例对象的时候,都是自己new一个。现在需要实例对象的时候,从SPRING工厂(容器)中获得,需要将实现类的全限定名称配置到xml文件中。

1、 新建一个Spring的项目。

2、 导入Jar包:4+1

4个核心(beans、core、context、expression) + 1个依赖(commons-loggins...jar)

spring-beans-3.2.0.RELEASE.jar      【管理bean】

spring-core-3.2.0.RELEASE.jar             【核心】

spring-context-3.2.0.RELEASE.jar            【上下文(配置文件)】

spring-expression-3.2.0.RELEASE.jar     【SpEL表达式】

com.springsource.org.apache.commons.logging-1.1.1.jar    【依赖】

3、 写一个接口

public interface UserService {
	
	public void addUser();

}



4、写接口的实现类


public classUserServiceImpl implements UserService {
 
   @Override
   public voidaddUser() {
      System.out.println("a_icoadd user");
   }
 
}

5、写xml配置文件

在classpath/src下新建一个applicationContext.xml,添加Schema约束。

约束文件的位置:spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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.xsd">
    <!-- 配置service
         <bean> 配置需要创建的对象
             id :用于之后从spring容器获得实例时使用的
             class :需要创建实例的全限定类名
    -->
    <bean id="userServiceId" class=" Try_IOC.UserServiceImpl">
</bean>     
</beans>     

           

 

6、编写JUnit测试类

public classtestIoc {
 
    @Test
    public void test() {
        //从spring容器获得
                //1 获得容器
       
                StringxmlPath= "Try_IOC/applicationContext.xml";           
                ApplicationContext applicationContext= newClassPathXmlApplicationContext(xmlPath);
           
                //2获得内容 --不需要自己new,都是从spring容器获得
                UserServiceuserService= (UserService) applicationContext.getBean("userServiceId");
               
                userService.addUser();
 
    }
 
}

 

Debug:第一次运行报了这个错:

六月 04, 2017 10:47:34下午 org.springframework.context.support.AbstractApplicationContextprepareRefresh

信息: Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@197392df:startup date [Sun Jun 04 22:47:34 CST 2017]; root of context hierarchy

六月 04, 2017 10:47:34下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions

信息: Loading XML beandefinitions from class path resource[LilyLearnSpring/src/applicationContext.xml]

 

因为复制路径的时候出错了,复制的路径竟然是包括了Javaproject 的名称和SRC的名称,但是这个只要包名就行,所以修改之后得到了正确的输出:

 

六月 04, 2017 11:01:23下午 org.springframework.context.support.AbstractApplicationContextprepareRefresh

信息: Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@69e8c998:startup date [Sun Jun 04 23:01:23 CST 2017]; root of context hierarchy

六月 04, 2017 11:01:23下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions

信息: Loading XML beandefinitions from class path resource [Try_IOC/applicationContext.xml]

六月 04, 2017 11:01:23下午 org.springframework.beans.factory.support.DefaultListableBeanFactorypreInstantiateSingletons

信息: Pre-instantiatingsingletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@6b46a226:defining beans [userServiceId]; root of factory hierarchy

IoC实例程序:addUser()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值