您的位置:首页 > 编程语言 > Java开发

Spring入门案例 IoC

2017-06-04 23:06 405 查看

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()

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string JAVA