
原因
耦合度偏高
<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 id="bookDao" class="com.shanks.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.shanks.service.impl.BookServiceImpl"/>
beans>
初始化IOC容器,通过容器获取bean
package com.shanks;
import com.shanks.dao.BookDao;
import com.shanks.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//获取IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
System.out.println("-----------------");
BookService bookService = (BookService) ctx.getBean("bookService");
bookService.save();
}
}






容器关闭前出发bean的销毁
关闭容器方式;
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
向一个类中传递数据的方式有几种

引用类型

引用类型

1、强制依赖使用构造器进行,使用setter注入有概率不进行注入导致null对象出现
2、可选依赖使用setter注入进行,灵活性强
3、Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化、相对严谨
4、如果有必要可以两者同时使用,使用构造器注入完成强制依赖注入,使用setter注入完成可选依赖的注入
5、实际开发过程中还需要根据实际情况分析,如果受控对象没有提供setter方法就是必须使用构造器注入
IOC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配



注解
@ComponentScan({"com.shanks.service","com.shanks.dao"}

@Service
public void BookService implements BookService{
@Autowired
@Qualifier("bookDao")
private BookDao bookDao;
}
@Qualifer注解无法单独使用,必须配合@Autowired注解使用
@Configurantion
@ComponetScan("com.shanks")
@PropertySource("classpath:xxxx.properties")
public class SpringConfig{
}
路径仅支持单一文件配置,多文件请使用数组格式配置,不允许使用通配符 *
AOP面向切面编程,一种编程范式

1、Spring容器启动
2、读取所有切面配置中的切入点
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(void com.shanks.dao.BookDao.update())")
private void pt(){}
@Before("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}
3、初始化bean,判定bean对应的类中的方法是否匹配到任意切入点
目标对象(Target):原始功能去掉共性功能对应的类产生的对象,这种对象是无法直接完成最终工作的
代理(Proxy):目标对象无法直接完成工作,需要对其进行功能回填,通过原始对象的代理对象实现

package com.shanks.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAdvice {
@Pointcut("execution(void com.shanks.dao.BookDao.update())")
private void pt(){}
@Pointcut("execution(int com.shanks.dao.BookDao.select())")
private void pt2(){}
//@Before("pt()")
public void before(){
System.out.println("before....");;
}
//@After("pt()")
public void after(){
System.out.println("before....");;
}
// @Around("pt()")
public void around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around before advice");
//表示对原始操作的调用
pjp.proceed();
System.out.println("around after advice");
}
//@Around("pt2()")
public Object aroundSelect(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("around before advice");
//表示对原始操作的调用
Object proceed = pjp.proceed();
System.out.println("around after advice");
return proceed;
}
//@AfterReturning("pt2()")
public void afterReturning(){
System.out.println("afterReturning...");
}
@AfterThrowing("pt2()")
public void afterThrowing(){
System.out.println("afterThrowing .. advice");
}
}

事务作用:在数据层保障一系列的数据库操作同成功同失败
Spring事务作用:在数据层或业务层保障一系列的数据库操作同成功同失败

事务管理员:发起事务方法,在Spring中通常指代业务层开启事务的方法
事务协调员:加入事务方,在Spring中通常指代数据层方法,也可以是业务层方法