• spring实践1_IOC/DI


    写在前面spring框架的火热不言而喻,为此学习spring是有必要的。热衷于实践的我更喜欢用案例来分析与理解。从今天开始,我们来走近spring,

             希望通过这些简单的案例能够让我们对spring有个简单的了解,并达到入门水平。

    准备材料:1.IDE:eclipse,

           2.JDK:1.8,

           3.JARS:spring核心包

    学习目标:1.spring注入属性

           2.spring注入对象

           3.spring注解注入方式

    一.spring注入属性

      1.pojo实体类Category

    package com.practice.pojo;
    
    public class Category {
        private int id;
        private String name;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }

      2.spring核心配置文件。applicationContext.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
       <!-- 配置第一个bean -->
           <bean id="c" class="com.practice.pojo.Category">
               <property name="name"  value="category1"></property>
           </bean>
           
    </beans>

      3.编写测试类

    package com.practice.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.practice.pojo.Category;
    
    public class TestSpring {
        public static void main (String args[]){
            ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
            Category c1=(Category)context.getBean("c");
            System.out.println(c1.getName());
        }
    }

      4.运行结果

    九月 24, 2019 1:59:05 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e5d6d97: startup date [Tue Sep 24 13:59:05 CST 2019]; root of context hierarchy
    九月 24, 2019 1:59:05 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    category1

    二.spring注入对象

      1.pojo实体类Product

    package com.practice.pojo;
    
    public class Product {
        private int id;
        private String name;
        private Category category;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Category getCategory() {
            return category;
        }
        public void setCategory(Category category) {
            this.category = category;
        }
        
    }

      2.Spring核心配置文件。applicationContext.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
       <!-- 配置第一个bean -->
           <bean id="c" class="com.practice.pojo.Category">
               <property name="name"  value="category1"></property>
           </bean>
           <!-- 配置第二个bean -->
           <bean id="p" class="com.practice.pojo.Product">
               <property name="category" ref="c"></property>
           </bean>
    </beans>

      3.测试类

    package com.practice.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.practice.pojo.Product;
    
    public class TestSpring {
        public static void main (String args[]){
            ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
    //        Category c1=(Category)context.getBean("c");
    //        System.out.println(c1.getName());
            Product p1=(Product)context.getBean("p");
            System.out.println(p1.getCategory().getName());
        }
    }

      4.运行结果

    九月 24, 2019 2:46:56 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6433a2: startup date [Tue Sep 24 14:46:56 CST 2019]; root of context hierarchy
    九月 24, 2019 2:46:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    category1

    三.spring注解注入方式(注解属性对象)

      
      1.对上面已经存在的文件进行修改,修改Spring核心配置文件。applicaitonContext.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
       <!-- 开放注解功能 -->
       <context:annotation-config></context:annotation-config>
       <!-- 配置第一个bean -->
           <bean id="c" class="com.practice.pojo.Category">
               <property name="name"  value="category1"></property>
           </bean>
           <!-- 配置第二个bean -->
           <bean id="p" class="com.practice.pojo.Product">
               <!--  <property name="category" ref="c"></property>-->
           </bean>
           
    </beans>

      2.Product实体类

    package com.practice.pojo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class Product {
        private int id;
        private String name;
        //这一行的下一行  " @Autowired " 可以换成  " @Resource(name="c")"
        @Autowired 
        private Category category;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Category getCategory() {
            return category;
        }
        //如果上面的注解你都没用,那么你在下一行可以写上注解  “@Autowired" 
        public void setCategory(Category category) {
            this.category = category;
        }
        
    }
    
    //以上三种注解方式都可以实现同样的预期结果。

      3.运行结果

    九月 24, 2019 2:46:56 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6433a2: startup date [Tue Sep 24 14:46:56 CST 2019]; root of context hierarchy
    九月 24, 2019 2:46:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    category1

    四.spring注解注入方式(注解bean)

      1.修改Spring配置文件applicaitonContext.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context     
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
      <context:component-scan base-package="com.practice.pojo"></context:component-scan>
           
    </beans>

      2.修改Product类

    package com.practice.pojo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    @Component("p")
    public class Product {
        private int id;
        private String name="product1";
        //这一行的下一行  " @Autowired " 可以换成  " @Resource(name="c")"
        @Autowired 
        private Category category;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Category getCategory() {
            return category;
        }
        //如果上面的注解你都没用,那么你在下一行可以写上注解  “@Autowired" 
        public void setCategory(Category category) {
            this.category = category;
        }
        
    }
    
    //以上三种注解方式都可以实现同样的预期结果。

      3.修改Category类

    package com.practice.pojo;
    
    import org.springframework.stereotype.Component;
    
    @Component("c")
    public class Category {
        private int id;
        private String name="category1";
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }

     4.运行结果

    九月 24, 2019 3:22:23 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6433a2: startup date [Tue Sep 24 15:22:23 CST 2019]; root of context hierarchy
    九月 24, 2019 3:22:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    category1

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    -------------------------------------------分割线,以上我相信很容易看出异同,学习一个东西,首先是模仿,以后我会讲一讲我对控制反转和依赖注入的深入理解-------------------------------

  • 相关阅读:
    北斗对时设备(GPS校时产品)在数字城市系统中的应用
    京准电子,北斗授时产品取代GPS对时设备服务各行业
    北斗校时系统(GPS授时设备)几种时间同步方法
    网络对时服务器(NTP校时服务器)应用港口信息化系统
    关于医院NTP授时服务器(NTP校时服务器)部署工作
    变电站里NTP校时器(gps网络时间服务器)产品的应用
    C++学习(二十九)(C语言部分)之 顺序表
    C++学习(二十八)(C语言部分)之 文件操作
    C++学习(二十七)(C语言部分)之 预处理命令
    C++学习(二十六)(C语言部分)之 结构体3(联合,枚举)
  • 原文地址:https://www.cnblogs.com/zuoluwo/p/11577137.html
Copyright © 2020-2023  润新知