spring(1)_控制反转_helloWorld_ioc
来源:互联网 发布:淘宝卖家版app叫什么 编辑:程序博客网 时间:2024/09/21 09:21
这是我学习spring第一个写的代码
使用spring的步骤
1.搭建环境拷贝jar包
2.新建一个applicationContext.xml文件(spring的配置文件)
spring的控制反转(ioc)的说明,ioc其实就是将对象的创建等过程交给spring容器来做,这就是控制反转
小demo
配置文件(这些约束可以从spring的文档自己抄)
<?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">
<!--
beans 存放了很多类
把一个类放在spring容器中就是一个bean
一个bean就是一个类
id:是标示符,命名规范:类的第一个字符小写,其他一样
class: 为类的全名
-->
<bean id="helloWorld" class="com.hxc.domain.HelloWorld">
</bean>
</beans>
HelloWorld类
package com.hxc.domain;
import java.io.Serializable;
public class HelloWorld implements Serializable{
public void helloWorld() {
System.out.println("你好,世界");
}
}
测试类
可以new一个ClassPathXmlApplicationContext,一定要在类路径下,就是项目的src目录下
package com.hxc.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hxc.domain.HelloWorld;
public class TestHellloWorld {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
helloWorld.helloWorld();
}
}
结果
九月 04, 2016 10:45:09 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2b159eb: display name [org.springframework.context.support.ClassPathXmlApplicationContext@2b159eb]; startup date [Sun Sep 04 22:45:09 CST 2016]; root of context hierarchy
九月 04, 2016 10:45:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
九月 04, 2016 10:45:10 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@2b159eb]: org.springframework.beans.factory.support.DefaultListableBeanFactory@38ff3be5
你好,世界
九月 04, 2016 10:45:10 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@38ff3be5: defining beans [helloWorld]; root of factory hierarchy
- spring(1)_控制反转_helloWorld_ioc
- Spring---IOC 控制反转--1
- 1 spring--IOC(控制反转,依赖注入)
- Spring:IOC—控制反转(1)
- Spring IOC 反转控制
- Spring-----> IOC(控制反转)
- spring之控制反转
- spring的控制反转
- Spring控制反转
- Spring控制反转
- spring之控制反转
- spring 控制反转
- Spring 控制反转
- Spring 控制反转
- Spring - 控制反转IOC
- Spring IoC[控制反转]
- Spring控制反转IOC
- spring 控制反转
- c++string 字符串类型用法
- RocketMQ 入门部署
- GridView
- Awesome IoT
- cocos2dx学习之路----第十三篇(动作类初探)
- spring(1)_控制反转_helloWorld_ioc
- 选择恐惧症的福音!教你认清MVC,MVP和MVVM
- Hust oj 1720 Fibonacci Numbers(水题)
- GPUImage 图像处理
- C++函数对象,Lambda,function,bind相关知识
- 文章标题
- Linux 系统中用户切换
- java数组中常见的排序和查找
- 无序向量简单操作分析(插入、删除、遍历)