zoukankan      html  css  js  c++  java
  • 初识spring

    一,概念

    (此句为复制)

    Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AOP(Aspect Oriented Programming,面向切面编程

    二,IoC(Inversion of Control

    1,IoC介绍

    IoC也称控制反转,是一种概念,思想,即把对象的创建,初始化以及销毁统统交给spring容器来做,由spring来控制对象的生命周期。

    DI依赖注入:Dependency Injection。

        依赖注入DI是指程序运行过程中,若需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部容器,由外部容器创建后传递给程序。依赖注入是目前最优秀的解耦方式。依赖注入让Spring的Bean之间以配置文件的方式组织在一起,而不是以硬编码的方式耦合在一起的。

    2,XML配置

    1)构造注入

    利用反射,通过有参构造方法设置属性值;

    package com.pojo;
    
    public class User {
    
        private Integer id;
        
        private String name;
        
        private Integer age;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
        }
    
        /**
         * 构造注入  
         *     必须提供有参构造方法
         * @param id
         * @param name
         * @param age
         */
        public User(Integer id, String name, Integer age) {
            super();
            this.id = id;
            this.name = name;
            this.age = age;
        }
        
        
    }
    package com.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.bb.pojo.User;
    
    public class UserTest {
    
        @Test
        public void test() {
         //获取配置文件对象以及初始化spring容器 ApplicationContext ac
    = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = ac.getBean(User.class); System.out.println(user); } }

    最后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"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd">
     
     
         <bean class="com.pojo.User" id="user">
             <!-- 构造注入 ,可以通过name value 的方式设置值
             <constructor-arg name="id" value="666"/>
             <constructor-arg name="name" value="张三"/>
             <constructor-arg name="age" value="18"/>
             -->
            
                    <!-- 通过构造方法索引的方式设置值-->
             <constructor-arg index="0" value="999"/>
             <constructor-arg index="1" value="李四"/>
             <constructor-arg index="2" value="22"/>
         </bean>
         
     </beans>

    2)设值注入

    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"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd">
     
     
         <bean class="com.pojo.User" id="user">
             <!-- 设值注入 -->
             <property name="id" value="22"/>
             <property name="name" value="张三"/>
             <property name="age" value="33"/>
         </bean>
         
     </beans>
    <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
     
         <bean class="com.bb.pojo.Cat" id="catId">
             <property name="nick" value="花花"/>
             <property name="color" value="黑色"></property>
         </bean>
         
         <bean class="com.bb.pojo.User" id="user">
             <!-- 
                 通过设值注入cat对象 
                 DI:依赖注入 调用者与被调用者之间的关系,本质上还是IOC
             -->
             <property name="cat" ref="catId"></property>
             <!-- <property name="cat">
                 <bean class="com.bb.pojo.Cat">
                     <property name="nick" value="小明"/>
                     <property name="color" value="黄色"></property>
                 </bean>
             </property> -->
             
             <property name="games">
                 <!-- 数组 -->
                 <array>
                     <value>LOL1</value>
                     <value>LOL2</value>
                     <value>LOL3</value>
                 </array>
             </property>
             <property name="cats">
                 <list>
                     <bean class="com.pojo.Cat">
                         <property name="nick" value="小明1"/>
                         <property name="color" value="黄色1"/>
                     </bean>
                     <bean class="com.pojo.Cat">
                         <property name="nick" value="小明2"/>
                         <property name="color" value="黄色2"/>
                     </bean>
                     <bean class="com.pojo.Cat">
                         <property name="nick" value="小明3"/>
                         <property name="color" value="黄色3"/>
                     </bean>
                 </list>
             </property>
             <property name="map">
                 <map>
                     <entry key="id" value="5"/>
                     <entry key="name" value="王五"/>
                     <entry key="age" value="18"/>
                 </map>
             </property>
             <property name="prop" >
                 <props>
                     <prop key="url">http://xxxx/user</prop>
                     <prop key="username">admin</prop>
                     <prop key="password">123456</prop>
                 </props>
             </property>
         </bean>
         
     </beans>
  • 相关阅读:
    模板实现查找算法
    指针笔试题
    【查找算法】基于比较的查找算法(顺序查找、对分查找、分块查找)
    flask动态路由
    flask对json的内置处理模块jsonify
    pyquery和etree两个模块的区别
    单元测试初试
    求数组中子数组的最大值
    C语言小程序之读取文本文件中单词的个数并打印出频率最高的10个
    电梯去哪儿
  • 原文地址:https://www.cnblogs.com/hunt1coder/p/11129318.html
Copyright © 2011-2022 走看看