spring3学习笔记文档格式.docx
《spring3学习笔记文档格式.docx》由会员分享,可在线阅读,更多相关《spring3学习笔记文档格式.docx(18页珍藏版)》请在冰豆网上搜索。
23.beanFactory是一个很根的接口。
继承它的另一个接口是Applicationtext。
所以ApplicationContextfactory=newClassPathXmlApplicationContext("
beans.xml"
);
和BeanFactoryfactory=newClassPathXmlApplicationContext("
都是可以的。
建议使用ApplicationContext
24.当factory.getBeans(“name”)的时候,返回的是object类型,可以进行强制类型转换,也可以用factory.getBeans(“name”,UserDAO.class)来指定返回的类型,。
25.面向接口编程但实际上真正注入的是它的实现;
在xml文件里面配置
26.IOC容器
a)实例化具体bean
b)动态装配
27.AOP支持
a)安全检查
b)管理transaction
28.
29.Springioc配置与应用
30.FAQ不给提示:
a)window–preferences–myeclipse–xml–xmlcatalog
b)UserSpecifiedEntries–add
i.Location:
D:
\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd
ii.URI:
file:
///D:
/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd
iii.KeyType:
SchemaLocation
iv.Key:
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
31.Xsd文件放在org.springframework.benas.factory.xml包下面
32.注入类型
33.三种注入方式,spring的IOC有三种注入方式第一是根据属性注入也叫set方法注入;
第二种是根据构造方法进行注入;
第三种是根据注解进行注入,这种方式我认为比较好,方便,要是bean多的话,使用前两种方式会使得配置文件过于臃肿。
a)
b)Spring_0300_IOC_Injection_Type
c)setter(重要)
d)构造方法(可以忘记)
i.参数的传递可以用index来设置索引:
publicUserService(UserDAOuserDAO){
super();
this.userDAO=userDAO;
}
在xml文件里面这样写;
<
beanid="
u"
class="
com.bjxst.impl.UserDAOImpl"
>
<
/bean>
<
userService"
com.bjxst.service.UserService"
constructor-arg>
<
refbean="
/>
//把上面名字为u的对象注入进去.
/constructor-arg>
e)接口注入(可以忘记)
34.写了bean标签相当于new了一个这个类,因为都会注入。
35.<
或者<
beanname="
都可以,唯一区别就是name里面可以含有特殊字符。
36.idvs.name
a)Spring_0400_IOC_Id_Name
b)name可以用特殊字符
37.可以把简单属性配在xml文件里面;
简单属性的注入
a)Spring_0500_IOC_SimpleProperty
b)<
propertyname=…value=….>
38.Bean的生存范围:
a)<
bean中的scope属性
b)singleton单例(不论拿多少次都只拿到一个对象。
这是默认的)
c)proptotype每次创建新的对象。
39.集合注入
a)Spring_0700_IOC_Collections
b)很少用,不重要!
参考程序
40.<
propertyname="
adminEmails"
41.<
props>
42.<
propkey="
administrator"
administrator@example.org<
/prop>
43.<
support"
support@example.org<
44.<
development"
development@example.org<
45.<
/props>
46.<
/property>
将三个prop组装成为一个resultsinasetAdminEmails(java.util.Properties)call
做为一个参数传递进去。
someSet"
set>
value>
justsomestring<
/value>
myDataSource"
/>
/set>
将其组装成为一个set集合,传递;
47.自动装配(两种byName,byType)
a)Spring_0800_IOC_AutoWire
b)byName(按照名字自动匹配,默认先匹配它)
userDAO1"
<
daoId"
value="
1"
beanname="
userDAO"
2"
scope="
prototype"
autowire="
byName"
>
//当属性名字与bean的name相等时候,就自动匹配;
如果没有找到就会出错。
/beans>
c)byType(按照类型自动匹配)
byType"
//它会按照类型来匹配,但是如果在找到了两个同样的类型,那么就会出错。
d)其余的很少用
e)如果所有的bean都用同一种,可以使用beans的属性:
default-autowire
48.生命周期
a)Spring_0900_IOC_Life_Cycle
b)lazy-init(不重要)//当BeanFactoryfactory=newClassPathXmlApplicationContext("
xml里面的class全部初始化,只有加上lazy-init的类不初始化;
什么时候用到了什么时候初始化;
大部分不用,只有当应用每次启动的时候特别慢的时候可以考虑用。
Destory()方法在ClassPathXmlAplicationContext类里面的方法,不是ApplicateionContext接口不能调用。
c)init-methoddestroy-methd两个属性表示要调用的方法,不要和prototype一起用(了解)
49.在ioc方面用annotation比xml好,但是在aop方面用xml比annotation好;
50.Annotation第一步:
a)修改xml文件,参考文档<
context:
annotation-config/>
xmlns:
context=http:
//www.springframework.org/schema/context//当前xml文件namespace中的context开头的对应的内容去哪里找。
如果引号写在下面也不会有提示。
用http开头,是最自然的不会冲突的用法,就像oracle与google发布的标准用各自的网站进行标识,就不会冲突了。
51.Dtd文件是xml文件的规范,现在规范xml文件的是xsd文件。
Xml可以引入多个xsd文件。
Xsd一般称为xml文件的schema.
52.理解xsl的概念。
指扩展样式表语言(EXtensible
Stylesheet
Language)。
53.需要有一个参数为空的构造方法。
54.@Autowired(不建议使用)
a)@Autowired(required=false)表示本来应该注入,但是可以没有。
不是必须的
b)@Autowired也可以放在非set方法上,spring自动帮您注入。
c)默认按类型bytype
d)如果想用byName,使用@Qualifier(name),后面加上名字
e)写在privatefield(第三种注入形式)(不建议,破坏封装)
f)如果写在set上,@qualifier需要写在参数上,
g)在xml文件里配置
beanclass="
example.SimpleMovieCatalog"
qualifiervalue="
main"
!
--injectanydependenciesrequiredbythisbean-->
action"
55.@Resource(重要)
c)加入:
j2ee/common-annotations.jar
d)默认按名称,名称找不到,按类型
e)可以指定特定名称
f)推荐使用
g)不足:
如果没有源码,就无法运用annotation,只能使用xml
56.JSR是JavaSpecificationRequests的缩写,意思是Java规范请求。
是指向JCP(JavaCommunityProcess)提出新增一个标准化技术规范的正式请求。
任何人都可以提交JSR,以向Java平台增添新的API和服务。
JSR已成为Java界的一个重要标准。
57.@Resource(重要)
a)默认的注入方式是byType;
b)加入:
c)默认按名称,名称找不到,按类型
d)可以指定特定名称
e)推荐使用
f)不足:
58.
?
xmlversion="
1.0"
encoding="
UTF-8"
beansxmlns="
http:
//www.springframework.org/schema/beans"
xsi="
//www.w3.org/2001/XMLSchema-instance"
context="
//www.springframework.org/schema/context"
xsi:
schemaLocation="
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context-3.0.xsd"
component-scanbase-package="
org.example"
在xml文件里加上这句,所有的bean配置都不需要了。
自动去org.example里去找。
59.@Component@Service@Controller@Repository,默认情况下这些类是我们的组合之一。
a)@Component当成是一个组件来使用,这样@Resource放在set方法上,就可以直接注入进去。
@Component指定名字,用@Component(“name”),推荐写上名字。
b)初始化的名字默认为类名首字母小写
c)可以指定初始化bean的名字
60.@Scope用注解默认是:
singleton
61.@PostConstruct=init-method;
@PreDestroy=destroy-method;
用注解
62.相对来说aop要比aoc复杂多了。
63.什么是AOP
64.面向切面编程Aspect-Oriented-Programming
a)是对面向对象的思维方式的有力补充
b)加逻辑的方式
i.直接写死的
ii.继承(耦合性不强),没有源码的时候,我们可以用extends的方法来扩展自己需要的程序。
iii.组合代替了继承。
(在xml的bean文件里加一个逻辑)
65.动态的把一部分逻辑加到bean上面,叫aop
66.动态代理必须实现一个接口:
给我的接口里有哪些方法,我生成的代理里面就有哪些方法。
我们产生的一个代理对象里面实际上包含了一个被代理的对象。
publicclassLogInterceptorimplementsInvocationHandler{
privateObjecttarget;
publicObjectgetTarget(){
returntarget;
publicvoidsetTarget(Objecttarget){
this.target=target;
publicvoidbeforeMethod(){
System.out.println("
methodstart"
@Override
publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)
throwsThrowable{
beforeMethod();
method.invoke(target,args);
}
@Test
publicvoidtestProxy(){
UserDAOuserDAO=newUserDAOImpl();
//首先产生一个被代理的对象。
LogInterceptorli=newLogInterceptor();
li.setTarget(userDAO);
//将被代理的类加入到代理类去。
UserDAOuserDAOProxy=(UserDAO)Proxy.newProxyInstance(UserDAO.class.getClassLoader(),newClass[]{UserDAO.class},li);
//产生代理类。
System.out.println(userDAOProxy.getClass());
userDAOProxy.save(newUser());
67.好处:
可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码
68.Spring两种实现代理的方式
a)Annotation
b)加上对应的xsd文件spring-aop.xsd
c)beans.xml<
aop:
aspectj-autoproxy/>
d)此时就可以解析对应的Annotation了
e)建立我们的拦截类
f)用@Aspect注解这个类
g)建立处理方法
h)用@Before来注解方法
i)写明白切入点(execution…….)
j)让spring对我们的拦截器类进行管理@Component
k)常见的Annotation:
i.@Pointcut(命名切面,下面的可以用它的名字,加在空方法上面。
)
ii.@Before
iii.@AfterReturning(正常执行返回后,拦截)
iv.@AfterThrowing(拦截异常)
v.@After
vi.@Around(包围方法,用pjp.proceed()来调用方法,在这语句之前后加逻辑。
@Aspectj回在类上来实现的;
需要导入aspectj包,在xml里面用<
aspectj-autoproxy/>
,需要加新的命名空间xsd文件。
Aspectj是专门用来实现代理的一个框架。
AspectJ是一个面向切面的框架,它扩展了Java语言。
AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件.。
@Aspectj说明它是一个切面逻辑,可以把它切到其它方法上面去。
@Aspect
@Component
publicclassLogInterceptor{
@Before("
execution(publicvoidcom.bjxst.impl.UserDAOImpl.save(com.bjxst.model.User))"
还有@After,@AfterReturning
如果没有加@Component那么程序就会出错。
execution不能写错,写错了会报错,要有返回值都要写上
1.织入点语法
a)void!
void非void返回值。
b)参考文档(*..)
给切入点的集命名,是用一个方法命名的。
一定要写在一个方法上面.
上面的service下是没有实现接口的类,所以要加入cglib包。
如果类没有实现接口那么spring会直接用操作二进制码的方式,去生成代理的代码。
这样需要加一个包(cglib-nodep-2.2.2.jar)
如果实现接口就会用jdk自带的proxy来帮你产生代理,但是如果没有实现接口,就会用cglib产生接口,直接更改二进制码。
69.xml配置AOP,重点
a)把interceptor对象初始化
config
i.<
aspect…..
1.<
pointcut
2.<
before
70.好处:
a)Filter
b)Struts2的interceptor,也是这样用的。
71.概念:
a)JoinPoint
b)PointCut(切入点集合)
任何类的任何方法的返回任何值的。
支入点语法,需要的时候看文档学习,”..”代表无限多个层次。
..*.*(..)不管子包里有多少层,里面的任何类任何方法。
c)Aspect(切面)
d)Advice(建议,加在切入点上的建议跟加在点上的业务逻辑差不多。
e)Target
f)Weave(编织)
72.AroundAdvice
a)责任链模式
73.声明式的事务管理
74.Spring整合Hibernate
75.Spring里经常用的链接池叫dbcp
76.在beans.xml文件里面配置。
需要加上命名空间xmlns:
p=http:
//www.springframework.org/s