Spring
来源:互联网 发布:js获取file绝对路径 编辑:程序博客网 时间:2024/06/10 23:33
一、特点:
- 控制反转-Inversion Of Control
是以面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级spring框架的核心,也就是当我们需要对象的时候,不需要自己来直接创建,而是从工厂中获得.
IOC分为两种类型,依赖注入(Dependency Injection)和依赖查找(Dependency Lookup). - 面向切面编程Aspect Oriented Programming
通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术, 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有方法进行增强。
二、注解
1..@Component(@Repository@Service@Controller)
@Autowired@Qualifier(按照类型和名称注入)
2..@Configuration(表名当前是一个配置类)
@ComponentScan(basePackages={“day01”})(扫描的包)
@Import({JdbcConfig.class})()(导入其他的配置类,其他的配置类不需要再@Configuration)
@PropertySource({“classpath:config.properties”})(加载properties配置文件)
@Bean(只能使用在方法上,表明使用此方法创建一个对象,并且放入spring容器中)
3..@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={“classpath:applicationContext.xml”})
三、三种数据源()
- c3p0: com.mchange.v2.c3p0.ComboPooledDataSource
- dbcp: org.apache.commons.dbcp.BasicDataSource
- spring内置: org.springframework.jdbc.datasource.DriverManagerDataSource
四、
1、配置action的@Scope(作用范围)的参数
singleton :默认值,单例的.
prototype:多例的.
request:WEB项目中,Spring创建一个Bean的对象,将对象存入到request域中.
session:WEB项目中,Spring创建一个Bean的对象,将对象存入到session域中.
2、配置service的@Transactional(事务控制)的参数
read-only:是否是只读事务。默认false,不只读。
isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。
propagation:指定事务的传播行为。
timeout:指定超时时间。默认值为:-1。永不超时。
rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。没有默认值,任何异常都回滚。
no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,任何异常都回滚。
五、BeanFactory和ApplicationContext的区别
1、BeanFactory才是Spring容器中的顶层接口。ApplicationContext是它的子接口。
2、ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
BeanFactory:什么使用什么时候创建对象。
六、ClassPathXmlApplicationContext和FileSystemXmlApplicationContext区别
ClassPathXmlApplicationContext:它是从类的根路径下加载配置文件,推荐使用这种
FileSystemXmlApplicationContext:它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
Service cs = (ServiceImpl) ac.getBean(“service”);
六、AOP
@Pointcut("execution(* day02.aop..*.*(..))") private void pt(){} @Before("pt()")//id=表达式,由于我们使用的是注解,id就是方法名,不要括号 public void before(){//之前执行 System.out.println("before执行"); } @AfterReturning("pt()") public void after_returning(){//之后执行 System.out.println("after-returning执行"); } @AfterThrowing("pt()") public void after_throwing(){//异常执行 System.out.println("after-throwing执行"); } @After("pt()") public void after(){//最终执行 System.out.println("after执行"); } /** * 环绕通知 * 问题: * 当我们配置了环绕通知之后,切入点方法没有执行。只有环绕通知的代码执行了。 * 分析: * 根据动态代理可以得知,环绕通知指的是InvocationHandler中的invoke方法。 * 在该方法中有语句明确的调用切入点方法:method.invoke() * 而我们的环绕通知之中,没有这个明确的调用。 * 解决: * spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口可以作为环绕通知的参数来使用。 * 当程序运行时,spring框架会为我们提供该接口的实现类供我们在环绕通知中使用。 * 该接口中有一个方法:proceed()方法,它就相当于method.invoke() */ @Around("pt()") public void around(ProceedingJoinPoint pjp){ try { System.out.println("环绕通知的前置通知"); pjp.proceed();//切入点方法执行 System.out.println("环绕通知的后置通知"); } catch (Throwable e) { System.out.println("环绕通知的异常通知"); }finally { System.out.println("环绕通知的最终通知"); } }
- Spring
- Spring
- spring
- spring
- spring
- spring
- Spring
- spring
- spring
- Spring
- Spring
- spring
- Spring
- Spring
- spring
- spring
- spring
- Spring
- 表单对象属性选择器
- ConfigReader(十)—— ReadGuideBornNpcTaskConfig
- 排序 8
- MySQL在以下几种情况会创建临时表
- java打jar包,引用其他.jar文件
- Spring
- C#语言-编写一个控制台应用程序,可根据输入的月份判断所在季节
- SOM自组织神经网络学习
- 阿里云ECS CentOs7.3下搭建LAMP环境(Apache2.4 + Mysql5.7 + PHP5.6 + Laravel5.2)
- LeetCode 674. Longest Continuous Increasing Subsequence
- 2017-10-11-jquery-使用ajax传递参数遇到的一个问题
- Mysql之左连接右连接内连接——示例
- Html菜单折叠
- 生活小记23