Spring 笔记总结
1、Spring框架的核心是提供一个容器(BeanFactory 或 ApplicationContext),提供以下功能:
1)创建和销毁组件对象,类似“工厂类”
2)采用不同的模式创建对象
3)IOC
4)AOP
2、Bean组件配置
<bean id="标识符" class="完整类路径" scope="type" init-method="方法名" destroy-method="方法名"/>
type:1)singleton:单例模式,在容器实例化时创建
2)prototype:在调用getBean()时创建
指定销毁方法是在容器关闭时触发,只适用于singleton
使用时:
ApplicationContext ac = new ClassPathXmlApplicationContext("path/of/applicationContext.xml");
Bean bean = (Bean)ac.getBean("beanid");
3、Spring IOC(Inverse of Controller)
将对象关系的指定,对象创建,初始化和销毁等逻辑交给Spring负责
4、DI(Dependency Injection)
Spring采用DI技术实现IOC控制思想
注入的两种方法:
1)setter方式注入,使用set方法注入,在<bean>中采用如下描述:
<property name="属性名" ref="注入Bean对象的ID" />
2)构造方法注入,提供构造方法注入,少用,<bean>中配置:
<constructor-arg index ="参数索引,从0开始计数" ref="注入Bean对象的ID" />
Spring中各种类型的数据注入
1)Bean对象注入
<property name="属性名" ref="注入Bean对象的ID" />
2)基本数据的注入
<property name="属性名" value="值" />
3)集合的注入
(1)List Set
<property name="属性名" >
<list>
<value>value0</value>
<value>value1</value>
</list>
</property>
<!-- 普通值用<value>标签,对象用<bean>标签:<ref bean="beanid" />,如果是集合,<list> 标签改成 <set> -->
(2)Map
<property name="属性名" >
<map>
<entry key="键0" value="值0" />
<entry key="键1" value="值1" />
</map>
</property>
<!-- 普通值用<value>标签,对象用: <entry key="键0" value-ref="beanid" />-->
(3)properties
<property name="属性名" >
<props>
<prop key="键0"> value<prop/>
<prop key="键1"> value<prop/>
</props>
</property>
5、AOP(Aspect Orented Programming)
1)aspect:插入的通用处理功能组件
2)joinpoint:连接点,aspect插入点
3)pointcut:joinpoint组成pointcut
4)advice:通知,aspect 和 joinpoint间的发生次序
5)target:目标,利用切入点指定的组件和方法
6)autoproxy:AOP利用动态代理实现AOP
例子:
   <aop:config>
        <aop:aspect id="TestAspect" ref="aspectBean">
            <!--配置com.spring.service包下所有类或接口的所有方法-->
            <aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(..))" />
            <aop:before pointcut-ref="businessService" method="doBefore"/>
            <aop:after pointcut-ref="businessService" method="doAfter"/>
            <aop:around pointcut-ref="businessService" method="doAround"/>
            <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>
        </aop:aspect>
    </aop:config>  
    <bean id="aspectBean" class="com.spring.aop.TestAspect" />
    <bean id="aService" class="com.spring.service.AServiceImpl"></bean>
    <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  
切入点定义:
1)方法限定表达式:execution(修饰符(可无) 返回类型 方法名(参数列表) 异常(可无))
2)类型限定表达式:with(类型)
3)Bean名称限定:Bean(id)
4)args参数限定:args(类型)
6、log4j
log4j主要有以下3部分构成
1)日志器(logger)
2)输出器(appender)
3)布局器(格式器,layout)
如:
#1. 设置输出级别
log4j.rootLogger=info,myconsole,myfile,dateFile,MAIL
#2. 设置具体的配置信息
log4j.appender.myconsole=org.apache.log4j.ConsoleAppender
log4j.appender.myconsole.target=System.out
log4j.appender.myconsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myconsole.layout.conversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} %l %m %n 
#2. 设置具体的配置信息(文件中)
log4j.appender.myfile=org.apache.log4j.FileAppender
log4j.appender.myfile.file=d:/log4j.txt
log4j.appender.myfile.layout=org.apache.log4j.PatternLayout
log4j.appender.myfile.layout.conversionPattern=%d{yyyy-MM-dd HH\:mm\:ss.SSS} %l %m %n
#2. 设置具体的配置信息(文件中,每天一个)
log4j.appender.dateFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dateFile.File=d\:/my.html
log4j.appender.dateFile.layout=org.apache.log4j.HTMLLayout
log4j.appender.dateFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
#2. 设置具体的配置信息(发送邮件)
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
#控制当前级别: log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.bufferSize=100
log4j.appender.MAIL.From=zuxia@qq.com
log4j.appender.MAIL.SMTPHost=127.0.0.1
log4j.appender.MAIL.Subject=Log4J Message
log4j.appender.MAIL.To=zuxia@qq.com
log4j.appender.MAIL.layout=com.zuxia.test.MyLayoutPattern #此类是用户自定义的哦 重写了HTMLLayout类
log4j.appender.MAIL.layout.ConversionPattern=%d - %c -%-4r [%t] %-5p %c %x - %m%n
在log4j.properties如果配置多种输出方式,其均有效
获取logger时:
Logger logger = Logger.getLogger(??)
??在打印日志的时候会显示出来
7、Spring注解配置
1)组件扫描:
@Component:其他组件
@Controller:Action组件
@Service:Service组件
@Repository:Dao组件
注解只能用在类定义前,方法定义前,成员变量定义前,上述注解只是推荐用法
2)开启组件扫描方法:
<context:component-scan base-package="package.path" />
4)注入注解,注入标记在成员变量定义前使用
@Resource:默认按类型匹配注入,如有多个符合要求类型,报错,匹配不唯一,使用名称注入方式
@Resource(name="beanid")
@Autowired:默认按类型匹配注入,如有多个符合要求类型,则使用名称注入方式
@Autowired
@Qualifier("beanid")
5)AOP注解
(1)开启AOP注解
<aop:aspect-autoproxy />
(2)使用@component将组件扫描到时Spring容器
(3)使用@Aspect将组件定义为Aspect组件
(4)定义方法,在方法前使用@Pointcut定义切入点表达式
(5)在target前使用@Around,@Before,@AfterReterning,@AfterThrowing,@After
例子
package com.bird.service;  
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;  
/**
 * 切面
 * @author Bird
 *
 */
@Aspect
public class MyInterceptor {
    @Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")
    private void anyMethod(){}//定义一个切入点  
    @Before("anyMethod() && args(name)")
    public void doAccessCheck(String name){
        System.out.println(name);
        System.out.println("前置通知");
    }  
    @AfterReturning("anyMethod()")
    public void doAfter(){
        System.out.println("后置通知");
    }  
    @After("anyMethod()")
    public void after(){
        System.out.println("最终通知");
    }  
    @AfterThrowing("anyMethod()")
    public void doAfterThrow(){
        System.out.println("例外通知");
    }  
    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("进入环绕通知");
        Object object = pjp.proceed();//执行该方法
        System.out.println("退出方法");
        return object;
    }
}  
6)常用注解汇总
@Configuration把一个类作为一个IoC容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean。
@Scope注解 作用域
@Lazy(true) 表示延迟初始化
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Scope用于指定scope作用域的(用在类上)
@PostConstruct用于指定初始化方法(用在方法上)
@PreDestory用于指定销毁方法(用在方法上)
@DependsOn:定义Bean初始化及销毁时的顺序
@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier("personDaoBean") 存在多个实例配合使用
@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@PostConstruct 初始化注解
@PreDestroy 摧毁注解 默认 单例  启动就加载
@Async异步方法调用
Spring 笔记总结的更多相关文章
- Spring笔记02_注解_IOC
		目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ... 
- Spring笔记01_下载_概述_监听器
		目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ... 
- Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)
		Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ... 
- Spring笔记:事务管理
		Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ... 
- Spring笔记:AOP基础
		Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ... 
- Spring:笔记整理(1)——HelloWorld
		Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ... 
- Spring笔记:IOC基础
		Spring笔记:IOC基础 引入IOC 在Java基础中,我们往往使用常见关键字来完成服务对象的创建.举个例子我们有很多U盘,有金士顿的(KingstonUSBDisk)的.闪迪的(SanUSBDi ... 
- Spring笔记(6) - Spring的BeanFactoryPostProcessor探究
		一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ... 
- spring笔记----看书笔记
		上周末看了一章以前javaee轻量级的书spring部分,简单做了一些笔记 // ApplicationContext ac=new ClassPathXmlApplicationContext(&q ... 
- Spring 笔记(三)Bean 装配
		前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ... 
随机推荐
- 201521123044 《Java程序设计》第9周学习总结
			1. 本章学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免 ... 
- 201521123055 《Java程序设计》第11周学习总结
			1. 本章学习总结 2. 书面作业 Q1.互斥访问与同步访问 ** 完成题集4-4(互斥访问)与4-5(同步访问) ** 1.1 除了使用synchronized修饰方法实现互斥同步访问,还有什么办法 ... 
- PTA分享码-Java
			主要用于Java语法练习,非竞赛类题目. 1. Java入门 959dbf0b7729daa61d379ec95fb8ddb0 2. Java基本语法 23bd8870e ... 
- 参加IMWebConf 2017 前端开发者大会是什么体验?
			周六作为特邀讲师之一参加了IMWebConf 2017 前端开发者大会的主题演讲,主题为<WebAssembly:面向未来的web开发技术>.本次大会质量非常高,来自国内外的技术专家带了很 ... 
- Java:静态内部类的使用目的、使用限制、与非静态内部类的对比
			Java之静态内部类(static class) 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static关键字修饰),也可以是非静态的. 一.静态内部类的使用目的. 在 ... 
- thymeleaf模板引擎调用java类中的方法(附源码)
			前言 <Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦> 由于开源了项目的缘故,很多使用了My Blog项目的朋友遇到问题也都会联系我去解决 ... 
- 用Beautifulsoup 来爬取贴吧图片
			import urllib.request import bs4 import re import os url="https://tieba.baidu.com/p/1988291937? ... 
- [04] Object类
			1.基本概念 Object类是所有类的父类,位于java.lang包中.任何类的对象,都可以调用Object类中的方法,包括数组对象. 2.常用方法 2.1 toString toString可以将任 ... 
- HDU-3032
			Problem Description Nim is a two-player mathematic game of strategy in which players take turns remo ... 
- Matlab入门学习(矩阵、函数、绘图的基本使用)
			一.矩阵 1.定义和简单使用(一般的编程语言,数组下标都是从0开始的,但是MATLAB是从1开始的) >> a=[ ; ; ] a = >> b=[ ; ; ]; >&g ... 
