Spring框架的基本入门知识笔记(1)

Spring的概念

Spring是开源的轻量级框架
    1.核心:
        AOP:面向切面编程,扩展功能不需要修改源代码实现
        IOC:控制反转
    2.一站式框架
        在JavaWeb三层框架中每一层都有相对应的解决技术
        Web层:SpringMVC
        Service:IOC
        Dao层:MyBatis
    版本:4.1.3
  • IOC的操作(控制反转—通过Spring管理类的创建)
    -IOC入门案例
    1.导入jar包
    如图所示
    2.创建类,在类里面创建方法
    建一个Person类
package pojo;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 
 *这个注解相当于<bean id ="person" class = "pojo.Person"/>
 *通过注解配置IOC
 */
//@Component(value="person")
//@Scope(value="prototype")
public class Person {
    /**
     * @Value赋值
     * 
     */
    @Value(value="John")
    private String name;
    @Value(value="20")
    private int age;
    private String[] hobby;
    private List<String> hobby1;

    public Person(String name, int age, String[] hobby, List<String> hobby1) {
        super();
        this.name = name;
        this.age = age;
        this.hobby = hobby;
        this.hobby1 = hobby1;
    }

    public List<String> getHobby1() {
        return hobby1;
    }

    public void setHobby1(List<String> hobby1) {
        this.hobby1 = hobby1;
    }

    public Person(String name, int age, String[] hobby) {
        super();
        this.name = name;
        this.age = age;
        this.hobby = hobby;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public Person() {

    }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", hobby=" + Arrays.toString(hobby) + ", hobby1=" + hobby1
                + "]";
    }
    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 void run(){
        System.out.println("run......");
    }
}
    3.创建Spring配置文件,配置要创建对象相应的类
        (1).Spring核心配置文件名称和文职是不固定的,建议放在src下,名称官方建议applicationContext。xml
        (2).引入schema约束
<?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:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>
        (3).需要配置创建对象相应的类
        在<beans></beans>标签内配置
<!-- 
        id 为Bean 起名字,唯一,必须以字母开始
        name 没有ID用name
        单例的.........单例设计模式 懒汉式 饿汉式 两种方式的区别
        scope:
            prototype :多例
            默认是单例    singleton
        使用类的无参数构造方法创建:要求类当中要有无参的构造方法
     -->
    <bean id="person" class="pojo.Person" scope="prototype"></bean>
4.测试(代码仅用于测试)
    创建一个JUnit测试
    代码:  
package test;

import static org.junit.Assert.*;


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

import pojo.Car;
import pojo.Person;

public class IOCTest {

    @Test
    public void test() {
        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //创建对象
        Person p = (Person) context.getBean("person");
        p.run();

    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值