Spring笔记

Spring是一个基于IOC和AOP的结构J2EE系统的框架
IOC 反转控制 是Spring的基础,Inversion Of Control
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象

DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

传统的方式:

通过new 关键字主动创建一个对象

IOC方式

对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。

IOC与传统对比
这里写图片描述
image

public class Category {
	private int id;
	private String name;
}

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" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<bean name="c" class="com.one.Category">
		<property name="name" value="category 1" />
		<!-- 通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中 -->
	</bean>

</beans>

测试Test通过spring获取Category对象,以及该对象被注入的name属性。

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
		Category c=(Category) context.getBean("c");
		//c.setName("wanffff");
		System.out.println(c.getName());
	}
}

使用注解的方式完成注入对象中的效果

  1. 修改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" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config /><!-- 告诉Spring要用注解的方式配置 -->
	<bean name="s" class="com.bean.Student">
		<property name="name" value="Student" />
	</bean>
	<bean name="t" class="com.bean.Teacher">
		<property name="name" value="Teacher" />
		<!-- <property name="student" ref="s" />注释掉这句 -->
	</bean><!-- 以上:注入对象行为注解 -->

</beans>

2.在Teacher.java的Student属性前加上@Autowired注解

package com.bean;
public class Teacher {
	private int id;
	private String name;
	// 添加Autowired注解,也可以在setStudent()位置加注解,效果一样
	 @Autowired
	private Student student;
}

3.运行测试

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// System.out.println("test page !!!");

		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
		// 通过Spring拿到的Student对象的name属性
		// Student s1 = (Student) context.getBean("s");
		// System.out.println(s1.getName());

		Teacher t = (Teacher) context.getBean("t");
		t.setName("teacher--1");
		t.setStudent(new Student("student--1"));
		System.out.println(t.getName());
		System.out.println(t.getStudent().getName());

	}

}

对Bean的注解

1、修改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" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.bean"/>
	<!-- 告诉Spring,bean都放在com.bean这个包下-->
</beans>

2、为Student、Teacher类加上@Component注解,即表明此类是bean

@Component("s")
public class Student

@Component("t")
public class Teacher

3、 运行测试同上

AOP 即 (Aspect Oriented Program)面向切面编程


	在面向切面编程的思想里面,核心业务功能和切面功能分别独立进行开发 然后把切面功能和核心业务功能 "编织" 在一起,叫做AOP。首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 
  • 核心业务:比如登陆,增加数据,删除数据都叫核心业务;
  • 周边功能:周边功能在Spring的面向切面编程AOP思想里,即被定义为切面;比如性能统计,日志,事务管理等等;
  • 辅助功能和核心业务功能彼此独立进行开发;
  • 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行;
  • 如果需要,就把"日志输出" 功能和 “登陆” 功能编织在一起,即登陆时,可以查看日志输出;
  • 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程

AOP切面编程思路
AOP切面编程思路

1、准备业务类TeacherService.java

public class TeacherService {
	public void doSomeService() {
		System.out.println("doSomeService");
	}
}

2、applicationContext.xml文件

<context:component-scan base-package="com.bean"/>
<bean name="ts" class="com.spring.service.TeacherService"></bean>

3、测试

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
TeacherService ts=(TeacherService) context.getBean("ts");
ts.doSomeService();

4、准备日志切面 LoggerAspect.java
该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。

public class LoggerAspect {
	public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("start log:" + joinPoint.getSignature().getName());
		Object object = joinPoint.proceed();
		// 就是将来与某个核心功能编织之后,用于执行核心功能的代码
		System.out.println("end log:" + joinPoint.getSignature().getName());
		return object;

	}
}

5、applicationContext.xml

<context:component-scan base-package="com.bean"/>
	<bean name="ts" class="com.spring.service.TeacherService"/>
	<bean id="loggerAspect" class="com.spring.aspect.LoggerAspect"/>
	
	<aop:config >
		<aop:pointcut expression="execution(* com.spring.service.TeacherService.*(..))" id="loggerCutpoint"/>
			<aop:aspect id="logAspect" ref="loggerAspect">
				<aop:around method="log" pointcut-ref="loggerCutpoint" />
			</aop:aspect>
	</aop:config>

AOP 即 (Aspect Oriented Program)面向切面编程


	在面向切面编程的思想里面,核心业务功能和切面功能分别独立进行开发 然后把切面功能和核心业务功能 "编织" 在一起,叫做AOP。首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 
  • 核心业务:比如登陆,增加数据,删除数据都叫核心业务;
  • 周边功能:周边功能在Spring的面向切面编程AOP思想里,即被定义为切面;比如性能统计,日志,事务管理等等;
  • 辅助功能和核心业务功能彼此独立进行开发;
  • 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行;
  • 如果需要,就把"日志输出" 功能和 “登陆” 功能编织在一起,即登陆时,可以查看日志输出;
  • 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程

AOP切面编程思路
AOP切面编程思路

1、准备业务类TeacherService.java

public class TeacherService {
	public void doSomeService() {
		System.out.println("doSomeService");
	}
}

2、applicationContext.xml文件

<context:component-scan base-package="com.bean"/>
<bean name="ts" class="com.spring.service.TeacherService"></bean>

3、测试

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
TeacherService ts=(TeacherService) context.getBean("ts");
ts.doSomeService();

4、准备日志切面 LoggerAspect.java
该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。

public class LoggerAspect {
	public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("start log:" + joinPoint.getSignature().getName());
		Object object = joinPoint.proceed();
		// 就是将来与某个核心功能编织之后,用于执行核心功能的代码
		System.out.println("end log:" + joinPoint.getSignature().getName());
		return object;

	}
}

5、applicationContext.xml

<context:component-scan base-package="com.bean"/>
	<bean name="ts" class="com.spring.service.TeacherService"/>
	<bean id="loggerAspect" class="com.spring.aspect.LoggerAspect"/>
	
	<aop:config >
		<aop:pointcut expression="execution(* com.spring.service.TeacherService.*(..))" id="loggerCutpoint"/>
			<aop:aspect id="logAspect" ref="loggerAspect">
				<aop:around method="log" pointcut-ref="loggerCutpoint" />
			</aop:aspect>
	</aop:config>

注解方式配置AOP

1 注解配置业务类

@Component("ts")
public class TeacherService

2 注解配置切面

//@Aspect 注解表示这是一个切面
//@Component 表示这是一个bean,由Spring进行管理
//@Around(value = "execution(* com.spring.service.TeacherService.*(..))") 
//表示对com.spring.service.TeacherService这个类中的所有方法进行切面操作
@Aspect
@Component
public class LoggerAspect {
	@Around(value="execution(* com.spring.service.TeacherService.*(..))")
	public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("start log:" + joinPoint.getSignature().getName());
		Object object = joinPoint.proceed();
		// 就是将来与某个核心功能编织之后,用于执行核心功能的代码
		System.out.println("end log:" + joinPoint.getSignature().getName());
		return object;

	}
}

3 applicationContext.xml

<context:component-scan base-package="com.spring.aspect"/>
	<context:component-scan base-package="com.spring.service"/>
	<aop:aspectj-autoproxy/>

4 运行测试

Spring 注解方式测试
applicationContext.xml

<context:component-scan base-package="tt"/>
//1. @RunWith(SpringJUnit4ClassRunner.class) 
//	表示这是一个Spring的测试类
//2. @ContextConfiguration("classpath:applicationContext.xml")
//	定位Spring的配置文件
//3. @Autowired 给这个测试类装配对象
//4. @Test 测试逻辑,打印对象的名称

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class teee {
	@Autowired
	Student student;
	
	@Test
	public void test() {
		System.out.println(student.getName());
	}

}
posted @ 2018-02-20 18:53  小沐酱  阅读(0)  评论(0编辑  收藏  举报  来源