Spring基础视频

https://study.163.com/course/courseMain.htm?courseId=1005991005

《课时4》:6分左右,xml中特殊值书写;16分左右,autowire=“byName”; 30分左右,使用注解@Component("studao")和包扫描<context:component-scan base-package="com.sdh.dao"></context:component-scan>把id为studao的对象交给ioc容器
在这里插入图片描述

《课时6》aop之使用接口实现切面类:

<!-- 前置通知类 -->
<bean id="logBefore" class="aop.LogBefore"></bean>

<aop:config>
	<!-- 配置切入点(在哪里执行通知) -->
	<aop:pointcut expression="execution(public void com.sdh.dao.StudentDao.test()) or execution(public void com.sdh.dao.StudentDao.deleteById(int))" id="pc"/>
	<aop:advisor advice-ref="logBefore" pointcut-ref="pc"/>
</aop:config>
import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

@Component("logBefore")
public class LogBefore implements MethodBeforeAdvice {

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		System.out.println("前置通知");
	}

}

在这里插入图片描述

《课时9》:开启注解对aop的支持:

<!-- 开启注解对aop的支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component("logAspectAnnotation")
@Aspect
public class LogAspectAnnotation {
	
	@Before("execution(public * com.sdh.dao.StudentDao.*(..))")
	public void myBefore() {
		System.out.println("注解形式 前置通知");
	}
	
	@AfterReturning(value="execution(public * com.sdh.dao.StudentDao.*(..))",returning="returnValue")
	public void myAfter(JoinPoint jp, Object returnValue) {
		System.out.println("注解形式 后置通知 目标对象:"+jp.getTarget()+"\t返回结果:"+returnValue);
	}

}

《课时11》:为了用Spring开发java项目,需要监听到application的启动,立刻实例化一个Ioc容器。具体需要在web.xml中做如下图配置。如果没有设置contextConfigLocation,则按约定,Spring ioc配置文件的目录和文件名为:WEB-INF\applicationContext.xml

  <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>

《课时12》例子跑起来有问题,事实证明servlet被创建了两次,而我们默认同类型servlet只有一个实例,所以坑爹了。其中第一次是spring容器创建的,此时会调用servlet的无参构造器,不会调用init()方法。第二次是首次被http请求访问时创建的,此时会先调用servlet的无参构造器,然后调用init()方法。《课时13》的解释似乎有点小问题,但提供的解决办法可行(不知道是否最优):拿到spring上下文对象,再用它去拿bean:

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
testService = (ITestService) context.getBean("testServiceBean");

以上问题据说可用struts或者springMVC解决

《课时14》Spring整合Mybatis的关键代码:
applicationContext.xml:

<!--在spring ioc容器中创建Mybatis的核心类SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:conf.xml"></property>
	</bean>	
	<bean id="studentMapper" class="com.sdh.dao.impl.StudentDaoImpl">
	<!-- 将spring中配置的sqlSessionFactory对象交给dao层 -->
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>

Mybatis的配置文件conf.xml:

<mappers>
	<mapper resource="com/sdh/mapper/studentMapper.xml" />
</mappers>

Dao层接口:

public interface StudentMapper {
	void addStudent(Student stu);
}

Dao实现类:

public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {
	@Override
	public void addStudent(Student stu) {
		SqlSession session = getSqlSession();
		StudentMapper studentDao = session.getMapper(StudentMapper.class);
		studentDao.addStudent(stu);
	}
}

《课时15》 4:31 : spring默认事务自动commit。
对《课时14》的改进:

  1. 不再需要conf.xml, 在sqlSessionFactory中增加mapperLocations:
<property name="mapperLocations" value="com/sdh/mapper/*.xml"></property>
  1. 不再需要DaoImpl类,applicationContext.xml中对mapper(dao)的配置改为下图。其实MapperFactoryBean就是SqlSessionDaoSupport的直接子类
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.sdh.mapper.StudentMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
  1. 在第2项改进的基础上,自动批量生成mapper(dao): 删掉上一个bean,改用MapperScannerConfigurer。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.sdh.mapper"></property>
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

9:22 : @Autowired是byType,增加使用@Qualifier注解来按名字装配

《课时17》:@Configuration @Bean
32分左右:给@Configuration的扫描器制定规则 ,如排除等

《课时18》spring bean的默认作用域为单例:
在这里插入图片描述
Spring Boot的核心思想:条件注解,Condition接口、@Conditional

《课时20》Spring bean的生命周期:创建 -》注入属性-》init -> … -> destroy,详细见一文轻松搞懂Spring中bean的作用域与生命周期

《课时21》6:41 注入@Autowired的属性没有用set方法,但是可以用@Autowired修饰set方法:
在这里插入图片描述
在这里插入图片描述

@Resource如果没有指定属性,先根据名字查找,若没找到再根据类型查找;也可通过指定name或type属性,表示通过名字或类型查找

《课时22》@Profile

《课时23》:一文轻松搞懂Spring中bean的作用域与生命周期
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_23204557

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值