【Spring】笔记

IOC

  • 什么是IOC:
    IOC是Inversion of Control控制反转的缩写,把对象的创建和调用交给Spring进行,从而降低代码的耦合度。

  • IOC实现过程

    1. // 通过配置xml文件,创建对象
      <bean  scope="singleton" init-method="init" destroy-method="destory" id="mockServlet1" 
      class="net.spring.ioc.MockHttpServlet1">	
      
    2. //在工厂类中通过反射创建对象 获取字节码文件
       Class<?> clz = Class.forName("net.spring.ioc.pojo.User");
       
  • IOC接口
   1. BeanFactory
   2. ApplicationContext
  • IOC操作Bean管理(创建对象和注入属性)
  1. 基于xml操作Bean管理:
    1. 创建对象
    //id:唯一标识	class:类全路径(包路径)scope:作用域(单实例:singleton,多实例:prototype)
    <bean  id="mockServlet1" class="net.spring.ioc.MockHttpServlet1"></bean>
    
    2. 注入属性( DI:依赖注入)
	<bean  id="mockServlet1" class="net.spring.ioc.MockHttpServlet1">
		//String类型
		<property name="username">
			<value>LJ</value>
		</property>
		
		//对象类型
		<property name="MockServlet2" ref="mockServlet2"></property>
		
		//List类型
		<property name="ids">
			<list>
				<value>1</value>
				<value>2</value>
				<value>3</value>
			</list>
		</property>

		//Map类型
		<property name="map">
			<map>
				<entry key="key1" value="value1"></entry>
				<entry key="key1" value="value2"></entry>
			</map>
		</property>
	</bean>
   //必须有set方法
  1. 使用注解操作Bean管理

    (1)创建对象:

    //1. 开启注解扫,描制定扫描从哪个包开始
    <context:component-scan base-package="net"></context:component-scan>
    //2. 创建类,在类上面添加注解   
    @Component	
    @Service	
    @Controller	
    @Repository
    //sp:四个注解功能一样,都可以创建bean实例

(2)属性注入:

    @Autowired:根据属性类型自动装配
    @Qualifier:根据属性名称进行注入
    @Resource:可以根据类型注入,可以根据名称进行注入
    @Value:注入普通类型属性
   //首先检查容器中是否有当前类的实现,如果有且只有一个直接注入,
   //如果找到两个,再看成员变量名是否和beanname一直,如果有,则注入对应的bean,
   //如果没有,报Bug,需要通过@Qualifier("userServiceImpl2")来指定具体注入哪一个类

AOP

  • 什么是AOP:
    AOP是Aspect Oriented Programming(面向切面)缩写,可以不修改源码进行功能增强,如增加日志记录、性能监控等非业务逻辑功能,降低了代码的耦合性。

  • AOP实现原理(有接口情况下)
    使用JDK动态代理,创建接口实现类代理对象:

    1. Proxy.newProxyInstance(target.getClass().getClassLoader(), 
	   target.getClass().getInterfaces(), new ProxyProcessor(target,null))
	   //接收类(target),
	   //传入target的类加载器、
	   //target实现的接口、
	   //实现接口ProxyProcessor的代理对象
	2.//创建实现接口ProxyProcessor创建代理对象,写入增强逻辑
  • 连接点
    连接点是指类中可以被增强的方法。

  • 切入点
    切入点是指类中可以真正被增强的方法。

  • 通知
    通知是指实际增强的逻辑部分,有前置通知、后置通知、环绕通知、异常通知、最终通知五种类型。

  • 切面
    切面是一个动词,指把通知应用到切入点的这个动作

  • AOP注解实现方法

   1.<context:component-scan base-package="net"></context:component-scan>
   	  //xml中开启注解扫描
   
   2. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
   	  //xml中开启AOP注解支持 

   3.//在增强类中添加注解,在增强方法上面添加注解
   @Component
   @Aspect
   public class MyAspect {
   		@Pointcut("execution(* net.seehope..*.service.impl.*.*(..))")
   		public void pointCut() {	
		}
	
	//前置通知
		@Before("pointCut()")
		public boolean before(JoinPoint jp) {
			// TODO Auto-generated method stub
			System.out.println("before InterceptorImpl1");
			return true;
		}

   //环绕通知
   	@AfterReturning(pointcut="pointCut()",returning = "result")
		public void afterReturning(JoinPoint jp, Object result) {
			// TODO Auto-generated method stub
			System.out.println("InterceptorImpl1 后置:"+result);
		}
	}

JdbcTemplate

  • 使用方法:
	1.//在xml文件中配置数据库连接池
		<!-- jdbc操作解耦 -->
		<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
			<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
			<property name="url" 
				value=" jdbc:mysql://localhost:3306/test6?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC">
			</property>
			<property name="username" value="root"></property>
			<property name="password" value="lvjiajun"></property>
		</bean>
	
	

	2.//配置jdbcTemplate对象,注入dataSource
		<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
			<property name="dataSource" ref="dataSource"></property>
		</bean>

	3.//创建实现接口的Service类,添加注释自动注入
		@Service
		public class AccountServiceImpl implements AccountService{
			@Autowired
			...
			...
		}

	 4.//创建dao类,添加注释自动注入
		@Repository
		public class AccountDao {
			@Autowired
			private JdbcTemplate jdbcTemplate;
	
			public int addMoney(int id, int money) {
			return jdbcTemplate.update("update b set money = money + ? "
				+ "where id = ?",money,id);
			}
		}

事务

  • 什么是事务
    事务是数据库操作的基本单元,逻辑上的一组操作,要么全部操作失败,要么全部操作成功。比如售货机中,要么支付成功并出商品,要么支付失败并出商品失败,不能支付成功但出商品失败,或者支付失败并出商品成功。

  • 事务四个特性(ACID)

    • 原子性:整个过程不可分割,要么操作全失败,要么操作全成功。
    • 一致性:操作前后,总量不变。比如转账中2人余额之和操作前后不变。
    • 隔离性:事务必须在不干扰其他事务的前提下独立进行。
    • 持久性:执行事务过程中,对数据的所有改动都必须在事务成功前保存在存储设备中。
  • 使用方法

	1.//在xml中创建事务管理器
		<bean id="transactionManager" 
			class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"></property>
		</bean>
		
	2.//在xml中添加事务注解扫描
		<tx:annotation-driven/> 
		
	3. //在servelt类上方添加事务注解@Transactional
	 	@Transactional
	 	@Service
		public class AccountServiceImpl implements AccountService{
			@Autowired
			...
			...
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值