1. ioc 容器主要有 :beanfactory和applictioncontext,两者都是接口,一般使用后者,功能更强大
2. beanfactory 的子类有:xmlbeanfactory,该类是从xml文件中读取与数据,getbean()方法可以获取bean
3. applicationcontext 的实现类有:FileSystemXmlApplicationContext、FileSystemXmlApplicationContext、WebXmlApplicationContext、AnnotationConfigApplicationContext
4. bean是一个被管理的实例化的对象,这些bean有容器的提供的元数据创建
5. 在xml文件中创建和初始化bean: 用bean标签包裹,属性有:init-method、destroy-method、id 、name、class、scope、constructor、parent等
字标签有:property来定义变量和它的值 例子:
<bean id="bean1" class="xxxx.xxx">
<property name="city" value="foshan"> 定义了一个city变量,值为 foshan
</bean>
6. spring的作用域:单例(默认)、多例、request(每一次http请求都创建一个bean)、session(同一个session共享一个bean)、global-session(适用于webapplicationcontext)
7.bean 生命周期:Bean的定义——Bean的初始化——Bean的使用——Bean的销毁
8.bean 后置处理器,BeanPostProcessor接口主要有两个方法postProcessBeforeInitialization(Object, String)和postProcessAfterInitialization(Object, String)来在实例化bean之前和之后进行一些操作,需要自己定义一个类来实现beanpostprocessor接口,然后容器会自动扫描这些实现类(可以有多个实现类)
9.bean 的继承性,parent属性定义了该bean的父bean,子类的属性值会覆盖父类的值
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message1" value="Hello World!"/>
<property name="message2" value="Hello Second World!"/>
</bean>
<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">
<property name="message1" value="Hello India!"/>
<property name="message3" value="Namaste India!"/>
</bean>
输出:
World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India! #India的message1的值覆盖了world的message1的值
India Message2 : Hello Second World!
India Message3 : Namaste India!
一般来说,定义一个父类bean模板时,只需将abstract属性设为true就行,不用定义其他的属性值
10. 依赖注入 :基于构造函数的依赖注入
要依赖其他类的类要在构造函数的参数里指定要依赖的类
public class TextEditor {
private SpellChecker spellChecker;
public TextEditor(SpellChecker spellChecker) {#要在括号里指定你要依赖的类对象
this.spellChecker = spellChecker;
}
10.1 定义一个bean id="a"
10.2 定义一个依赖a的bean id="b" 使用<constructor-arg ref="a"/>指定依赖的对象的引用
10.3 如果b依赖多个bean,则可以使用多个<constructor-arg ref="...">,可以使用index value指定参数
如:
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="2001"/>
<constructor-arg index="1" value="Zara"/>
</bean>
11. 依赖注入 :基于设值函数的依赖注入
要依赖其他类的类要在构造函数的参数里指定要依赖的类
public class TextEditor {
private SpellChecker spellChecker;
public setSpellChecker(SpellChecker spellChecker) {#要在括号里指定你要依赖的类对象
this.spellChecker = spellChecker;
}
11.1 定义一个bean id="a"
11.2 定义一个依赖a的bean id="b" 使用<property name="要与setxxx,set后面一样,和属性名无关,要与方法名有关" value=“a”/>指定依赖的对象的引用
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
12. 注入内部bean : 在<property />或<constructor-arg />标签内使用bean标签,作用是当做value的值
<bean id="outerBean" class="...">
<property name="target">
<bean id="innerBean" class="..."/> target的值就是里面定义的bean
</property>
</bean>
13. 注入集合: 如果被依赖的bean是一个集合类型,那么需要在xml文件中配置好相关的bean
13.1 list类型:
<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">
<!-- results in a setAddressList(java.util.List) call -->
<property name="addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
13.2 set类型 :
<!-- results in a setAddressSet(java.util.Set) call -->
<property name="addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
13.3 map类型 :
<property name="addressMap">
<map>
<entry key="1" value="INDIA"/>
<entry key="2" value="Pakistan"/>
<entry key="3" value="USA"/>
<entry key="4" value="USA"/>
</map>
</property>
13.4 propertys类型:(java有这个类)
<property name="addressProp">
<props>
<prop key="one">INDIA</prop>
<prop key="two">Pakistan</prop>
<prop key="three">USA</prop>
<prop key="four">USA</prop>
</props>
</property>
14. bean自动装配 :上面使用constructor和property来指定依赖的bean叫做显示装配,下面可以根据autowire属性来自动装配
14.1 byName : 根据bean的名称来自动装配 ,具体做法
14.1.1 定义一个被依赖的bean
14.1.2 定义 bean,设置autowire属性的值为“byName”
14.1.3 那么在类中定义的类对象就会被容器扫描配置文件,然后找到变量名和id一样的bean来注入
14.2 byType :根据bean的类型和定义变量的类型来注入,具体做法
14.2.1 定义一个被依赖的bean
14.2.2 定义 bean,设置autowire属性的值为“byType”
14.2.3 那么在类中定义的类对象就会被容器扫描配置文件,然后找到变量的类型和配置文件中类型一样的bean来注入
14.3 byConstructor :可以根据依赖类的构造函数的参数来自动装配,具体做法
14.3.1 定义一个依赖的类,构造方法中指定被依赖的对象
14.3.2 定义一个被依赖的bean ,将id的值设为本类的类名
14.3.3 定义 bean,设置autowire属性的值为“byConstructor”
14.3.4 那么在类中定义的类对象就会被容器扫描配置文件,然后找到构造函数中的参数和配置文件中id和定义的对象的类型一样的bean来注入
与byType有点类似
15. spring基于注解的配置 :从sprig2.5开始,如果开启xml配置和注解,那么优先级是xml大于注解
注解是默认关闭的,因此需要使用以下配置文件头
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>
16. @required注解 :作用于依赖类的setter方法上,表明他要依赖的对象必须是在xml配置文件中定义的bean,否则抛出BeanInitializationException 异常
17. @autowired注解 :作用于属性、构造方法、set设值方法上,注入方式是byType,即自动扫描配置文件中或者用注解注册到容器中类型用于的bean
18. @Qualifier注解 :当使用autowire的时候,可能有当个同样类型的bean,因此使用qualifier来限定使用某个具体的bean来注入
qualifier("id")来限定由id名匹配的bean来注入
19. @PostConstruct 和 @PreDestroy 注释:放在方法上面,指定方法为bean初始化和销毁调用方法,说明init-method和destroy-method
@PostConstruct
public void init(){
System.out.println("Bean is going through init.");
}
@PreDestroy
public void destroy(){
System.out.println("Bean will destroy now.");
}
<bean id="helloWorld"
class="com.tutorialspoint.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!"/>
</bean>
20.@Resource 注解:放在属性或者setter方法上,作用是根据bean的name来注入,该注解有一个name属性,name="beanname"
21.基于java的注解 :@Configuration和@Bean
@Configuration :放在类定义上面,作用是表明该类定义的bean是注册到sprig ioc容器中的
@Bean :放在放回一个对象的方法上面的,表明将一个从方法中返回的对象注册为bean
属性有:initMethod和destroyMethod
@Configuration
public class AppConfig {
@Bean
public Foo foo() {
return new Foo(bar());
}}
22.@Import 注解:放在类定义上面,参数是一个类,表示将参数中定义的bean导入到本类中
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}}
ConfigA中定义的bean,在bean中也存在,相当于继承了
23.@Scope注解 :指定bean作用域,一般来说都是放在@Bean后面,这样比较好理解他的作用
24.spring容器applicationcontext的事件 :ApplicationEvent 类和 ApplicationListener来完成事件监听和处理,了解一下就好
public class CStopEventHandler
implements ApplicationListener<ContextStoppedEvent>{
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
25.spring AOP:面向切面编程,将核心业务代码和一些通用的代码,比如日志、声明式事务等
25.1 切面 :就是自定义的类,他是一些通用业务代码。
25.2 切入点 :可以添加切面的点
25.3 连接点 : 使用了切面的点
25.3 通知 :有几个方法,在实际行动之前、之后、抛出异常、返回值时、环绕这五个方面
25.4 目标对象 :被切入的类对象
26. 基于xml配置文件的AOP :
26.1 xml文件头内容 :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- bean definition & AOP specific configuration -->
</beans>
26.2 将需要的jar包导入
26.3 声明一个切面aspect :
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
...
</aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>
26.4 声明一个切入点 :在<aop : aspect>标签里面
<aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..))"/>
26.4 声明一个通知 :
<!-- a before advice definition -->
<aop:before pointcut-ref="businessService"
method="doRequiredTask"/>
27. 基于@AspectJ的AOP :
27.1 声明一个切面 :在一个类定义上面添加@Aspect注解
27.2 在xml文件中注册该bean
27.3 声明一个切入点 :使用@Pointcut("execution(*com.xxxx.xxx.*.*(..))")放在方法上面,该方法可以没有具体代码,后续的通知要附着到这里来
第一个* :不明
第二个* :类名
第三个* : 方法名
括号中.. : 表示任意多个参数
27.4 定义通知 : 在方法前添加@Before("切入点处的方法")注解
27.4.1 @Before("selectAll()") 前置通知
27.4.2 @After("selectAll()") 后置通知
27.4.3 @AfterReturning(pointcut = "selectAll()", returning="retVal") 返回通知 returning的值为切入点返回的对象
27.4.4 @AfterThrowing(pointcut = "selectAll()", throwing = "ex") 抛出异常的通知 throwing的值为切入点抛出的异常类型
28. spring的jdbc框架 :JdbcTemplate 类执行 SQL 查询、更新语句和存储过程调用,执行迭代结果集和提取返回参数值
28.1 在xml文件中配置连接数据库相关数据
28.2 注册一个数据源bean,类型为class="org.springframework.jdbc.datasource.DriverManagerDataSource"
28.3 声明四个变量:driverClassName、url、 username、password,并赋值
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/TEST"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
28.4 JdbcTemplate类的构造函数参数为 :dataSource
所以在使用时在注册一个bean,JdbcTemplate,依赖dataSource
28.5 JdbcTemplate 对象有许多方法可以执行:增删改查
29. spring中存储过程 :
29.1 在数据库中定义一个存储过程 :
DELIMITER $$
DROP PROCEDURE IF EXISTS `TEST`.`getRecord` $$
CREATE PROCEDURE `TEST`.`getRecord` (
IN in_id INTEGER,
OUT out_name VARCHAR(20),
OUT out_age INTEGER)
BEGIN
SELECT name, age
INTO out_name, out_age
FROM Student where id = in_id;
END $$
DELIMITER ;
29.2 spring中的SimpleJdbcCall类可以获得该存储过程的对象,调用execute(参数)即可使用存储过程;
30. spring事务 :四个特性(原子性、一致性、持久性、隔离性)
有两种事务管理:编程式和声明式
31. spring事务抽象 :事务管理的五大属性:隔离级别、传播行为、是否只读、事务超时、回滚规则
31.1 编程式事务:使用TransactionTemplate对象的execute()方法
31.2 声明式事务(切面方式):
31.3 注解方式 :@Transactional() 在方法上面加
常用属性:
value:事务管理器 在xml中配置dataSourceTransactionManager类的bean
propagation 传播行为,默认不存在事务则新建,否则添加到已有事务
isolation 隔离级别
readOnly 是否读写
timeout 超时时间设置
32.springMVC :请求经过分发器找到controller,返回数据和视图,经过分发器发送给浏览器
32.1 在WEB-INF中新建xml配置文件,名字一般为:servletname-servlet.xml,如:HelloWeb-servlet.xml
32.2 在xml文件中定义一个分发器
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
定义一个servlet,因为可能有多个请求路径分开,如:/user 、/admin
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/admin</url-pattern> 将admin开头的请求路径全部交给HelloWeb这个分发器
</servlet-mapping>
32.3 在xml中定义一个视图解析的bean
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
32.4 开启注解方式: <context:component-scan base-package="com.xxx" />
33.访问静态页面
33.1 /pages下面的所有请求都定位到 /WEB-INF/pages/下面去。如:/pages/hello.html就到location下面找这个html文件
<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
<mvc:annotation-driven/>
34. spring异常处理:
34.1 @ExecptionHandler : 在方法上面加入该注解,参数值为某个异常类 例子:@Execption(MyExecption.class),在可能的地方throw new MyExecption("...");
34.2 配置文件 ,指定异常的页面
<bean class="org.springframework.web.servlet.handler. SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.tutorialspoint.SpringException">
ExceptionPage
</prop>
</props>
</property>
<property name="defaultErrorView" value="error"/>
</bean>
35. Log4j日志:主要分为三部分,优先级、输出地方、格式
Logger log = Logger.getLogger(MainApp.class.getName());
log.info(".....");
35.1 log4j.properties配置文件:log4j.appender.FILE.File=路径
log4j.appender.FILE.ImmediateFlush=true 等选项
36. p:namespace 和c:namespace
p:namespace是给属性值直接在bean标签里面赋值,例子:<bean id="id1" class="xxxx" p:name="nsl"> 就是将name属性赋值为“nsl”
c:namespace是给构造函数的参数赋值,例子:<bean id="id2" class="xxxx" c:name="nsl" c:password="123456">就是将name,password参数赋值
37. depend-on属性:明确告知ioc容器该bean依赖于某个bean,可以在实例化当前bean之前要先实例化依赖的bean
38. @Qualifier 注解:指定bean的name,相当于byName ,例子:
@Qualifier("user1")
@Autowired()
39. @Autowired不足:1.是无法注入string 和java的基本数据类型的;2.因为是基于byTYpe的,所以当父类有多个子类时,使用该注解会不知道该注入哪个子类,因此会报错。
40. 依赖与被依赖bean声明周期不一致问题:A依赖B,当A是单例模式,B是非单例,每次调用A都是使用同一个B,但是我们要每次调用A都是不同的B
40.1 解决办法1:抛弃使用@Autowired,使用context.getbean();
40.2 解决办法2:在bean类中重写lookup方法,例子:
@component
interface A{
@Lookup
public B lookB(); #此方法返回B对象
}
待续