码迷,mamicode.com
首页 > 编程语言 > 详细

Spring基本框架

时间:2016-12-15 20:20:16      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:存在   好的   方法   模板   utf-8   support   private   构造   lap   

1.Spring基本框架的概念

 Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring模块构建在核心容器之上,核心容器定义创建、配置和管理bean的方式。组成Spring框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现,一个支持DI/IOC和AOP开源的容器框架。

2.特点

DI依赖注入:组件以特定方式接收来自容器的资源注入

IOC控制反转:反转资源获取的方向

AOP面向切面编程

3.使用

(1)定义容器

技术分享

(2)得到容器

技术分享

具体的代码实现:

<?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"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    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.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <bean id="car1" class="com.hanqi.test.Car">
        <property name="carNumber" value="鲁C12345"></property>
    </bean>
        <bean id="car" class="com.hanqi.test.Car">
        <property name="carNumber" value="鲁C14345"></property>
    </bean>
    <bean id="p1" class="com.hanqi.test.Person">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="30"></constructor-arg>
        <property name="car" ref="car1"></property>
        <property name="bankCards">
            <list>
            <value>123456</value>
            <value>456798</value>
            <value>233339</value>
            </list>
        </property>
    </bean>
    <util:list id="bankCards">
        <value>12344</value>
        <value>13444</value>
        <value>15344</value>
    </util:list>
    <!-- 使用p标签 -->
    <bean id="p2" class="com.hanqi.test.Person" 
    p:name="李四" p:age="20" p:car-ref="car1" p:bankCards-ref="bankCards"></bean>
    <!-- 自动装配 
    <bean id="p3" class="com.hanqi.test.Person" autowire="byType"></bean>-->
    <!-- 自动装配 -->
    <bean id="p4" class="com.hanqi.test.Person" autowire="byName" p:name="王五" p:age="30"></bean>
    <bean id="p5" parent="p1" p:name="张小三" p:age="18"></bean>
    <!-- 定义模板,只能被继承,不能被外部使用 -->
    <bean id="m1" class="com.hanqi.test.Person"  abstract="true"
    p:name="默认名称" p:age="20" p:car-ref="car1" p:bankCards-ref="bankCards"></bean>
    <!-- 默认是单例的 -->
    <bean id="p6" scope="prototype" parent="m1" p:name="王芳" p:age="38"></bean>
    <!-- 加载外部配置文件 -->
    <context:property-placeholder location="classpath:person.properties"/>
    
    <bean id="p7" class="com.hanqi.test.Person" p:name="${name}" p:age="${age}"></bean>
</beans>

定义的Person类

package com.hanqi.test;

import java.util.List; 

public class Person {

    private String name;
    private int age;
    
    private List<String> bankCards;
    private Car car;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public List<String> getBankCards() {
        return bankCards;
    }
    public void setBankCards(List<String> bankCards) {
        this.bankCards = bankCards;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", bankCards=" + bankCards + ", car=" + car + "]";
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
        System.out.println("带参数的构造方法Person被实例化了");
    }
    public Person() {
        super();
        System.out.println("不带参数的构造方法Person被实例化了");
    }
    
    
}

定义的Car类

package com.hanqi.test;

public class Car {

    
        private String carNumber;

        public String getCarNumber() {
            return carNumber;
        }

        public void setCarNumber(String carNumber) {
            this.carNumber = carNumber;
        }

        @Override
        public String toString() {
            return "Car [carNumber=" + carNumber + "]";
        }

        public Car() {
            super();
            
        }

        public Car(String carNumber) {
            super();
            this.carNumber = carNumber;
        }
        
}

测试类

package com.hanqi.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestPerson {

    public static void main(String[] args)
    {
        // 1 加载容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        // 2 从容器中获取对象
        Person p1 =(Person)ac.getBean("p1");
        System.out.println(p1);
        
        Person p2 =(Person)ac.getBean("p2");
        System.out.println(p2);
        
//        Person p3 =(Person)ac.getBean("p3");
//        System.out.println(p3);
        
        Person p4 =(Person)ac.getBean("p4");
        System.out.println(p4);
        // 继承的对象
        Person p5 =(Person)ac.getBean("p5");
        System.out.println(p5);
        
        Person p6 =(Person)ac.getBean("p6");
        System.out.println(p6);
        
        Person p6_1 =(Person)ac.getBean("p6");
        System.out.println(p6_1);
        if(p6==p6_1)
        {
            System.out.println("p6 == p6_1");
        }
        else
        {
            System.out.println("p6 !=p6_1");
        }
        // 模板不能被调用
//        Person m1 =(Person)ac.getBean("m1");
//        System.out.println(m1);
        
        
        Person p7 =(Person)ac.getBean("p7");
        System.out.println(p7);
    }
}

 

Spring基本框架

标签:存在   好的   方法   模板   utf-8   support   private   构造   lap   

原文地址:http://www.cnblogs.com/miracle-0807/p/6184370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!