原始spring学习笔记

快速入门

导入包

到lib
在这里插入图片描述

配置文件

src下的ApplicationContextxml文件

<?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 name = 'wo' class="com.web.service.WorkerService"></bean>
           		
           </beans>

写一个bean

在这里插入图片描述

package com.web.service;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.web.entity.Worker;

public class WorkerService {
	public void saveWorker(){
		 Worker w1 = new Worker();
		 w1.setWorkerAge(14);
		 w1.setWorkerName("76");
		 w1.setWorkerNumber("14509874597");
		 
		 //创建 Configuration
		 Configuration configuration = new Configuration();
				 configuration.configure();
		 //获取 SessionFactory
		 SessionFactory sessionFactory = configuration.buildSessionFactory();
		 //获取 Session
		 Session session = sessionFactory.openSession();
		Transaction tran = session.beginTransaction();
		session.save(w1);
		tran.commit();
		 session.close();
	}

}

测试类

package com.web.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.web.entity.Worker;
import com.web.service.WorkerService;



public class Htest {
	public static void main(String[] args) {

		ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		WorkerService ws = (WorkerService)classPathXmlApplicationContext.getBean("wo");
		ws.saveWorker();
	}
}

运行成功

在这里插入图片描述

静态工厂方法获取bean

静态工厂类

将上述的测试类中获取bean的过程提取
写一个静态工厂类
在这里插入图片描述

xml配置

beanfactor指定静态工厂类,factorymethod指定工厂中的方法
在这里插入图片描述

Bean的注入方式

在这里插入图片描述

Setter方式注入

在这里插入图片描述

基本数据类型string

使用value属性注入

复杂类型

引用类型

使用ref注入

list类型Set类型数据类型

使用list set array标签 指定ref和calue

map类型

map中使用用entry标签设置key和value或value-ref

properties类型

props 和prop标签
在这里插入图片描述

注解方式注入

需要导入aopjar包,在spring配置文件中开启注解扫描器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:aop="http://www.springframework.org/schema/beans"
		xmlns:context="http://www.springframework.org/schema/context"
       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
           http://www.springframework.org/scheam/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
           <context:component-scan base-package="com.web.service"/>
         
           		
           </beans>

xmlns是xml中用到的标签,xsi:schemaLocation表示这些标签的具体实现,两个必须一一对应

注解

相当于IOC,将bean的创建和初始化交给springIOC容器
在这里插入图片描述

autowired相当于DI依赖注入

@qualifier指定注入bean的id
在这里插入图片描述

其他注解

在这里插入图片描述

在这里插入图片描述

SpEL

spring expression language 用于提供程序运行时构造复杂表达式来完成对象属性的存储和方法调用。
#{表达式}
在xml文件内部调用对象的属性和方法

spring整合web项目

导入spring-web。jar包
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
配置applicationcontext的位置和监听器

  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

spring整合junit

不好用,建议使用spring boot提供的测试框架

1导入jar包
2添加测试类

package com.web.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.web.entity.Worker;
import com.web.service.WorkerService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class Htest {
	public static void main(String[] args) {

		ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		WorkerService ws = (WorkerService)classPathXmlApplicationContext.getBean("wo");
		ws.saveWorker();
	}
}

AOP

日志统计、性能统计、安全控制、事务处理、异常处理

AOP和OOP的区别

oop面向对象编程object orient program

基于接口的动态代理

静态方法 newProxyInstance
在这里插入图片描述
在这里插入图片描述

invoke方法的参数

在这里插入图片描述

没有接口的动态代理实现

在这里插入图片描述

spring中的AOP

常用术语

目标对象
切入点
连接点
切面
通知
代理对象
织入
引见

开发阶段

编写核心业务代码
把公用代码抽取出来,制作成通知
在配置文件中,生命切入点与通知间的关系,通知的类型包括before after around exception return

spring的AOP框架会监控切入点
动态创建目标对象,将通知织入目标对象形成代理类。最后完成代码逻辑运行

xml版AOP

1 导包
2 写目标对象
3 写advice
4 把写好的交给bean
5 导入aop命名空间 用aop:config配置aop
6 aop:aspect标签进行切面配置 id标识切面 ref表示bean中的id
7 配置通知类 指定增强方法何时执行
在这里插入图片描述

注解方式AOP

从第四步不同
4 通过注解配置通知类
@before @afterreturning @aroubd 后跟切入点表达式
5 通知类和目标类交由spring容器管理
@aspect @ compent
6 spring配置文件中开启aop注解


<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

7测试

springjdbctemplate

在这里插入图片描述

spring事务管理

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值