Spring学习笔记——第一天

一、Spring概念

1.核心两部分

(1)aop:面向切面编程,扩展功能不通过修改源代码实现

(2)ioc:控制反转。比如有一个类,若想调用里面的非静态方法,要创建该类的对象,使用对象调用方法,创建对象的过程,需要new出来对象。控制反转就是不通过new来创建对象,交给spring通过配置来创建

2.一站式框架

为javaee的每一层都提供了解决技术

web层:springMVC

service层:ioc

dao层:jdbcTemplate

二、Spring的Ioc操作

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

2.ioc操作两部分:

(1)ioc的配置文件方式

(2)ioc的注解方式

三、Ioc底层原理

1.使用技术

(1)xml配置文件

(2)dom4j解析xml

(3)工厂设计模式

(4)反射

四、Ioc入门案例

1.导入jar包

(1)做最基本的功能需要四个核心包和两个日志包

2.创建类,在类中创建方法

   原始写法

public class User {
	public void add(){
		System.out.println("add");
	}
	public static void main(String[] args) {
		User user = new User();
		user.add();
	}
}

3.创建spring配置文件,配置创建类

(1)文件的名称和位置并不固定,建议放到src下,名称为applicationContext.xml

(2)引入schema约束

(3)配置对象创建

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd">
	<bean id="user" class="cn.itcast.ioc.User"></bean>
</beans>

4.测试代码

public class TestIoc {
	@Test
	public void testUser(){
		//1.加载spirng配置文件
		ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
		//2.得到配置的对象
		User user= (User) context.getBean("user");
		System.out.println(user);
		user.add();
	}
}

五、Spring的bean管理(xml配置文件)

1.bean实例化的方式

第一种 使用类的无参构造创建(重点)

<bean id="user" class="cn.itcast.ioc.User"></bean>

第二种 使用静态工厂创建

创建静态方法,返回类对象

第三种 使用实例工厂创建

创建非静态方法,返回类对象 

2.bean标签常用属性

(1)id属性:代号而已

(2)class属性:对象所在类全路径

(3)name属性:功能与id属性一样

(4)scope属性:singleton默认值单例;prototype多例

六、属性注入

1.对象创建的时候,向对象的属性设置值

2.在spring框架中,有两种方式

(1)set方法注入(重点)

public class Book {
	private String bookname;

	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	
	public void demobook(){
		System.out.println("book " + bookname);
	}
}
	<bean id="book" class="cn.itcast.property.Book">
		<!-- 使用set方法注入属性 -->
		<property name="bookname" value="Java编程思想"></property>
	</bean>
public class TestBook {
	@Test
	public void testBook(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
		Book book = (Book) context.getBean("book");
		book.demobook();
	}
}

 

(2)有参数构造注入

public class Demo1 {
	private String username;
	
	public Demo1(String username) {
		this.username = username;
	}

	public void test1(){
		System.out.println("demo1" + " " + username);
	}
}
<bean id="demo1" class="cn.itcast.property.Demo1">
	<!-- 有参构造 -->
	<constructor-arg name="username" value="儒雅随和的金贡"></constructor-arg>
</bean>
public class TestDemo1 {
	@Test
	public void testDemo1(){
		ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
		Demo1 demo = (Demo1) context.getBean("demo1");
		demo.test1();
	}
}

七、注入对象类型的属性(重点)

1.创建service类和dao类

2.在service类中把dao作为类型属性

3.生成dao类型属性的set方法

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值