Spring实例化
来源:互联网 发布:2017年网络大电影 编辑:程序博客网 时间:2024/06/10 13:28
对于Spring真的很迷茫,不明白,都说是属性注入,到底怎么注入的啊???
没办法了 , 还是一点一点学吧!!!!! 本人纯属娱乐并为自己学过的东西做一下整理,如有错误,还望各位多多包涵,*^_^*
1 在Eclipse中搭建环境
File-->new--->JavaProject 新建java项目Spring实例化
右键项目名-->new-->folder 新建bin文件夹 把commons-logging-1.0.4.jar,spring.jar两个包加载到bin下
右键项目名-->Properties-->Java Build Path-->Libraries-->Add JARs 加载2个文件 -->ok
2 新建2个文件夹
1--存放配置文件 2--存放java类
1------------------
右键项目名-->new-->SourceFolder 新建是src/java文件夹 注update勾必须打上
2-------------------
同上 新建是src/resouce文件夹
3 加配置文件 在src/resouce下
1—————————applicationContext-web.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"><beans><bean id="formaterBean" class="com.neusoft.test.spring.impl.UpperFormat"></bean></beans>
6 在src/java下新建4个类
1---Foramt接口
package com.neusoft.test.spring.format;public interface Foramt{ String format(String text); }
2---UpperFormat 类实现接口
package com.neusoft.test.spring.impl;import com.neusoft.test.spring.format.Foramt;public class UpperFormat implements Foramt{ public String format(String text) { return text.toUpperCase(); }}
3---LowerFormat 类实现接口
package com.neusoft.test.spring.impl;import com.neusoft.test.spring.format.Foramt;public class LowerFormat implements Foramt{ public String format(String text) { return text.toLowerCase();}}
4---测试类
import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.core.io.ClassPathResource;import com.neusoft.test.spring.format.Foramt;public class Test{ public static void main(String[] args) { //第一部分 基础 //第一种实例化 不常用 XmlBeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext-web.xml")); Foramt format1 = (Foramt)xmlBeanFactory.getBean("formaterBean"); String temp1 = format1.format("teAAFAtt"); System.out.println(temp1); //第二种实例化 多用 实现接口多 new String[]{"applicationContext-web.xml"}或 new String("applicationContext-web.xml) //new String[]作用:可以多个xml文件 ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext(new String[]{"applicationContext-web.xml"}); Foramt format2 = (Foramt)applicationContext1.getBean("formaterBean"); String temp2 = format2.format("hanCHAOb323"); System.out.println(temp2); //第三种实例化 FileSystemXmlApplicationContext在文件系统中找路径D:\\eclipse\\basic\\HelloSpring\\src\\resource\\applicationContext-web.xml ApplicationContext applicationContext2=new FileSystemXmlApplicationContext(new String[]{"D:/eclipse/basic/HelloSpring/src/resource/applicationContext-web.xml"}); Foramt format3 =(Foramt)applicationContext2.getBean("formaterBean"); String temp3=format3.format("testMecond3"); System.out.println(temp3);
代码结构:
运行结果:
`(*∩_∩*)′ 娱乐一下了!!!!
2012-3-22 11:15:46 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext-web.xml]TEAAFATT2012-3-22 11:15:46 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18e2b22: display name [org.springframework.context.support.ClassPathXmlApplicationContext@18e2b22]; startup date [Thu Mar 22 11:15:46 CST 2012]; root of context hierarchy2012-3-22 11:15:46 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext-web.xml]2012-3-22 11:15:46 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@18e2b22]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1abab882012-3-22 11:15:46 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1abab88: defining beans [formaterBean]; root of factory hierarchyHANCHAOB3232012-3-22 11:15:46 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@147c5fc: display name [org.springframework.context.support.FileSystemXmlApplicationContext@147c5fc]; startup date [Thu Mar 22 11:15:46 CST 2012]; root of context hierarchy2012-3-22 11:15:46 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from file [D:\eclipse\basic\HelloSpring\src\resource\applicationContext-web.xml]2012-3-22 11:15:46 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory信息: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@147c5fc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1ac1fe42012-3-22 11:15:46 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1ac1fe4: defining beans [formaterBean]; root of factory hierarchyTESTMECOND3
- Spring 实例化bean
- Spring实例化
- spring实例化bean
- Spring的实例化
- spring 实例化
- Spring实例化方式
- 实例化Spring容器
- Spring Beans实例化
- Spring实例化bean
- 实例化Spring容器,实例化bean
- spring实例化IOC容器
- SPRING BEAN的实例化
- spring静态工厂实例化
- spring bean 实例化问题
- 实例化Spring IoC容器
- spring中实例化bean
- Spring的方法实例化
- 模拟spring容器实例化
- Hibernate学习体会
- 理解HashMap
- poj1840
- Snooper taoism 2版常用cla ins组合
- SQL Server 2008中SQL应用系列及BI学习笔记系列--目录索引
- Spring实例化
- VS2005的小BUG及解决方法
- 第5周-师生互动
- div 隐藏和显示
- ios 画图总结
- abstract让你的远程代码承受住数据结构的变化
- Android环境变量的配置
- 基本文本聚类方法
- 转发到新浪微博QQ腾迅微博代码