spring的IoC环境搭建
来源:互联网 发布:mac phpmyadmin 配置 编辑:程序博客网 时间:2024/05/22 15:57
1.拷贝jar包(4个核心加一个依赖)
spring-framework-3.0.2.RELEASE-dependencies
spring-framework-3.2.0.RELEASE-dist
2.编写目标类IoC实现
(1)提供接口和实现类
public interface UserService {public void addUser();}
public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("a_ico add user");}}
(2)获得实现类的实例
3配置文件
位置任意,名称任意(src)
<?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.xsd"><!-- 配置service <bean> 配置需要创建的对象id :用于之后从spring容器获得实例时使用的class :需要创建实例的全限定类名--><bean id="userServiceId" class="com.yang01.ioc.UserServiceImpl"></bean></beans>4测试类
package com.yang01.ioc;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestIoC {@Testpublic void demo01(){//之前开发UserService userService = new UserServiceImpl();userService.addUser();}@Testpublic void demo02(){//从spring容器获得//1 获得容器String xmlPath = "com/yang01/ioc/beans.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);//2获得内容 --不需要自己new,都是从spring容器获得UserService userService = (UserService) applicationContext.getBean("userServiceId");userService.addUser();}}
十月 05, 2016 11:40:15 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17392df: startup date [Wed Oct 05 11:40:15 CST 2016]; root of context hierarchy十月 05, 2016 11:40:15 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [com/yang01/ioc/beans.xml]十月 05, 2016 11:40:15 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1a43bd4: defining beans [userServiceId]; root of factory hierarchya_ico add user
0 0
- spring的IoC环境搭建
- Spring环境搭建,IoC容器初体验
- Spring 环境搭建与IoC和AOP
- Spring的环境搭建
- Spring环境的搭建
- spring环境的搭建
- spring的环境搭建
- IOC、Spring的IOC
- 2.Spring 环境的搭建
- Struts+Spring环境的搭建
- 基于springmvc spring ioc + mybatis 的web项目搭建
- SSM搭建-Spring之IOC的XML注入(5)
- SSM搭建-Spring之IOC的注解注入(6)
- SSM搭建-Spring之IOC的自动装配(7)
- Spring环境搭建之:控制反转(IoC Inversion of Control)与依赖注入(DI Depenency Injection)
- Spring环境搭建之:控制反转(IoC Inversion of Control)与依赖注入(DI Depenency Injection)
- ssh整合环境搭建和spring IoC-xml文件配置初体验
- spring ioc的使用
- 【JavaWeb-17】国际化、自定义拦截器、struts2文件上传与下载、OGNL、contextMap
- jquery eval解析JSON中的注意点介绍
- Find The Multiple(POJ1426)(入门)
- windows 10 VPN 禁用全局路由
- [剑指offer学习心得]之:替换空格
- spring的IoC环境搭建
- gtags运行时报错 input buffer overflow 解决办法
- C语言中的初等数据结构
- leetCode_Patching Array
- 怎么组队打求生之路(局域网中)
- Catch That Cow(POJ3278)(入门)
- uva 11374/Airport Express
- 幻灯片PPT(PowerPoint)绘制的图形如何转为EPS格式
- 409. Longest Palindrome