Java - Spring 基础学习笔记 上

为什么用spring?

这个就说到spring的两大特性 - IOC 和 AOP 我们一个一个介绍。

IOC

Dependency Injection,依赖注入。了解设计模式中工厂模式的方法是一种采用了IOC思想的实现方式。这里简单介绍一下。

Sound方法中需要调用cat和dog的实例,一般的方式是在Sound中分别实例化对象。
在这里插入图片描述
这种方式 Sound 方法与Cat Dog紧密关联,如果我们想降低这种关联程度,可以添加一个Animal类,和一个InjectionHelper类,Sound中只知道自己调用了Animal的sound,但是并不清楚自己调用的是cat还是dog。而通过set方法将 cat 传入Sound的这一过程,就是依赖注入。

在这里插入图片描述

AOP

AOP(Abstract Oriented Programming),面向切面编程。AOP是OOP的补充和完善,OOP通过封装继承多态建立一种从上到下的系统层次,但对于与系统业务逻辑无关,每一层次都涉及的功能却无能为力,例如日志。这种分散到各处的被称为横切cross-cutting。

切面(Aspect)
一个关注点的模块化,这个关注点横切多个对象。例如logging,caching,transaction

连接点(Joinpoint):
程序执行过程中明确的点,如方法的调用,特定的异常被抛出或属性的获取。

通知(Advice):
切面在特定连接点采取的行动。

切入点(Pointcut):
指定一个通知将被引发的一系列连接点的集合

引入(Introduction):
添加方法或字段到被通知的类。

织入(Weaving):
组装切面来创建一个被通知对象。这可以在编译时或加载时完成,也可以在运行时完成。

安装

  1. 下载 jar 包:https://repo.spring.io/release/org/springframework/spring/
  2. 解压并导入至 eclipse
    选中项目右键
    在这里插入图片描述
    Add External Jars 进入Spring解压目录 spring-framework-5.1.3.RELEASE-dist\spring-framework-5.1.3.RELEASE\libs 选中所有

在这里插入图片描述

  1. apply
  2. src 下建立xml文件命名随意,我这里叫做SpringConfig.xml
  3. 把一下内容填写到SpringConfig.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" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd ">
        
</beans>

简单理解Spring IOC

Spring xml 文件将object转化为bean标签,id为实例化对象的名称,class是所指向的Java类。Java类的属性通过标签写在对应的bean中。xml中所有bean标签代表的Object 就这样被Spring控制。Spring根据bean的id和class,创建实例,并通过set方法将bean标签下所有配置,注入到新建的实例中。由于这些操作Object是被动方,Spring是主动方,这种实现方式又叫做控制反转 。新建的这些实例,称作bean,放在Bean Factory中,外部Object可以通过实例化beanFactory取得与SpringContainer的联系,通过getBean方法,获取beanFactory 中的 bean。由于这些bean实际上就是类的实例,可以通过强转换方式,转化为所需的类型。
在这里插入图片描述

简单使用Spring

新建Cat

package hello;

public class Cat {
	private String name;
	
	//setters and getters
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public void sound() {
		System.out.println("Meow Meow Meow");
	}
}

Sound方法

public class Sound {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
		Cat cat = (Cat)context.getBean("cat");
		cat.sound();
	}
}

修改XML文件,添加bean id为cat,property标签是为普通的参数赋值,name指代cat类中的参数名

 <bean id="cat" class="hello.Cat">
       <property name="name" value="Jack" ></property>
 </bean>

为什么使用ApplicationContext方式,而不用BeanFactory?

ApplicationContext 是功能更加强大的 BeanFactory 子接口,在BeanFactory 的基础上 多了资源加载,事件触发,消息处理的功能,所以一般来说可以用ApplicationContext,就不用BeanFactory。

构造器注入

如果需要注入构造器,使用constructor-arg标签

// Cat.java
public class Cat {
	private String name;
	private String type;
	private int id;
	
	public Cat(String name, String type) {
		this.name = name;
		this.type = type;
	}
	public Cat(String name) {
		this.name = name;
	}
	public Cat(int id) {
		this.id=id;
	}
	
	public void sound() {
		System.out.println("Meow Meow Meow");
	}
}

// xml file
// 使用第一个构造器
<bean id="cat" class="hello.Cat">
        <constructor-arg type="String" value="Jack" />
</bean>
// 使用第二个构造器
<bean id="cat" class="hello.Cat">
        <constructor-arg type="int" value="Jack" />
</bean>
// 使用第三个构造器
<bean id="cat" class="hello.Cat">
        <constructor-arg index="0" value="Jack" />
        <constructor-arg index="1" value="British Shorthair" />
</bean>

对象注入

三种方式:

  1. xml 文件中使用 ref 指向 所需对象的bean id
// Cat.java
public class Cat {
	private Card card;
	
	public void sound() {
		System.out.println(this.card.toString()+" sound: Meow Meow Meow");
	}
	// setters and getters
	public Card getCard() {
		return card;
	}
	public void setCard(Card card) {
		this.card = card;
	}
}

// Card.java
public class Card {
	private String name;
	private String type;
	
	// setters and getters
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String toString() {
		return "name: "+ this.name +" type: "+this.type;
	}
}	

// xml
 <bean id="cat" class="hello.Cat">
 	<property name="card" ref="catCard"></property>
 </bean>
 <bean id="catCard" class="hello.Card">
 	<property name="name" value="Jack"></property>
 	<property name="type" value="cat"></property>
 </bean>
  1. 通过 innerbean
 <bean id="cat" class="hello.Cat">
 	<property name="card">
 		<bean id="catCard" class="hello.Card">
   			<property name="name" value="Jack"></property>
   			<property name="type" value="cat"></property>
	    </bean>
 	</property>
 </bean>
  1. 通过 autowire=“byName” 仅当 参数属性名称和bean id相同时,本例子中,名称都为card。“byType” 仅当一种类型只有一个对象可用。
<bean id="cat" class="hello.Cat" autowire="byName"></bean>

<bean id="card" class="hello.Card">
	<property name="name" value="Jack"></property>
	<property name="type" value="SmallCat"></property>
</bean>

collection 注入

使用 list 标签

// Cat.java
public class Cat {
	List<Card> cardList;
	
	public List getCardList() {
		return cardList;
	}
	public void setCardList(List<Card> cardList) {
		this.cardList = cardList;
	}
	public void sound() {
		for(Card card:this.cardList) {
			System.out.println(card.toString()+" sound: Meow Meow Meow");
		}  
	}
}
// xml
<bean id="cat" class="hello.Cat">
	<property name="cardList">
		<list>
			<ref bean="catCard1" />
			<ref bean="catCard2"/>
			<ref bean="catCard3"/>
		</list>
	</property>	
</bean>
<bean id="catCard1" class="hello.Card">
	<property name="name" value="Jack"></property>
	<property name="type" value="SmallCat"></property>
</bean>
<bean id="catCard2" class="hello.Card">
	<property name="name" value="Jack"></property>
	<property name="type" value="MidCat"></property>
</bean>
<bean id="catCard3" class="hello.Card">
	<property name="name" value="Jack"></property>
	<property name="type" value="Bigcat"></property>
</bean>

Bean Scope

Singleton: 一个Spring Container一个bean,默认模式
Propotype:一个程式一个bean
Session:一个session一个bean
Request: 一个servlet Request一个bean
Global Session: 一个Http session一个bean

<bean id="card" class="hello.Card" scope="propotype"></bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值