zoukankan      html  css  js  c++  java
  • MyEclipse Spring 学习总结一 Spring IOC容器

    一、Spring IOC容器---- Spring AllicationContext容器

    程序的结构如下:

    1.首先在MyEclipse 创建创建Java Project

    2.创建好后,添加sping支持。在project上右击, MyEclipse->Add spring Capabilities.

    3.之后会自动生成applicationContent.xml文件

     1)创建HelloWorld.java

    public class HelloWorld {
    
    	private String message;
    	   public void setMessage(String message){
    	      this.message  = message;
    	   }
    	   public void getMessage(){
    	      System.out.println("Your Message : " + message);
    	   }
    }
    

    2)创建MainApp.java

    public class MainApp {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		ApplicationContext context = 
    				new ClassPathXmlApplicationContext("applicationContext.xml");
    		HelloWorld obj = (HelloWorld)context.getBean("helloWorld");
    		obj.getMessage();
    
    	}
    
    }
    

     3)在applicationContent.xml文件配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<bean id="helloWorld" class="bu.example.com.HelloWorld">
    		<property name="message" value="Hello World!!!" />
    	</bean>
    </beans>
    

    4.最后运行,结果如下:

      

     

     二、Spring IOC容器---- Spring BeanFactory容器

    只需修改MainApp.java文件

    public static void main(String[] args) {
    		XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
    		HelloWorld obj = (HelloWorld)factory.getBean("helloWorld");
    		obj.getMessage();
    
    	}
    

     两个输出的效果是一样的。 

    1.spring jar包下载地址

    spring官网的改变,导致找不到下载地址:

    spring framework download site

    http://maven.springframework.org/release/org/springframework/spring/

    (参考)

    2. spring教程 参考 使用Eclipse IDE 

  • 相关阅读:
    linux 命令
    Linux中zip压缩和unzip解压缩命令详解
    Sublime Text2.0.2注册码
    Yii框架入门教程(博客教程、权威指南、类手册)
    Redis在PHP中的基本使用案例
    Yii MemCache 应用实例
    javascript数组操作汇总
    CSS进阶学习
    暑期周总结八(2018.8.27-2018.9.2)
    3D轮播图
  • 原文地址:https://www.cnblogs.com/linlf03/p/5177045.html
Copyright © 2011-2022 走看看