Spring实例化

对于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]
TEAAFATT
2012-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 hierarchy
2012-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@1abab88
2012-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 hierarchy
HANCHAOB323
2012-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 hierarchy
2012-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@1ac1fe4
2012-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 hierarchy
TESTMECOND3



 




 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值