第一章 Spring简介

1 spring概念

(1) spring是开源的轻量级框架

(2)spring核心主要两部分

IOC:控制反转

- 比如创建一个类A,类中添加了一个非静态方法method() ,在另外一个类B中调用此类A的方法,都是通过new一个A对象,再通过此A对象来调用method()。

- IOC把对象的创建不是通过new方式实现,而是交给spring配置创建类对象

AOP:面向切面编程,扩展功能不是修改源代码实现

(3) spring是一站式框架

springJ2EE三层结构中,每一层都提供不同的解决技术

扫描二维码关注公众号,回复: 14662466 查看本文章

- web层:springMVC

- service层:springioc

- dao层:spring的jdbcTemplate

2 Spring中的IOC(重要)

1 把对象的创建交给spring进行管理

2 ioc操作大致分三部分:

1ioc的配置文件方式(详见第二章)

2ioc的注解方式(详见第二章)

3)自动装配(详见第二章)

 

 

 

3 第一个 IOC程序

(1)需要导入的jar包 

 (2)定义Person类

package com.inspur.demo1;

public class Person {
	private String tool;
	
	public String getTool() {
		return tool;
	}
	public void setTool(String tool) {
		this.tool = tool;
	}
	public  void UseTool(){
		System.out.println("人使用"+this.tool);
	}
}

(3)在src目录下创建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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="p1" class="com.inspur.demo1.Person" scope="prototype"> 
          <!-- DI 依赖注入(属性注入) -->
           <property name="tool" value="锤子"></property>
        </bean> 
  </beans>

(4)写测试代码 

package com.inspur.demo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1、加载配置文件
		ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
		//2、创建对象
		Person p1=(Person) ac.getBean("p1");
		p1.UseTool();
	}
}

 (5)代码执行结果

猜你喜欢

转载自blog.csdn.net/xiaotan0814/article/details/94123617