`

{转}spring基础总结

阅读更多
IOC控制反转:
依赖注入:
需要的jar文件
dist\spring.jar
lib\jakarta-comnons\comnons-logging.jar
lib\aspectj\aspectjweaver.jar
aspectjrt.jar
lib/cglib/cglib nodep2.1_3.jar
lib\j2ee\common-annotation.jar
实例化Spring容器的方法
ApplicationContext ctx= new ClassPathXmlApplication(new String[]("beans.xml"));

三种实例化bean的方法
1、使用构造器实例化:(绝大部分都是用该方法)
ApplicationContext ctx= new ClassPathXmlApplication(new String[]("beans.xml"));
<bean id="orderService" class="bean的路径"></bean>
2、静态工厂方法实例化:
public class OrderFactory{
public static OrderServiceBean createOrder(){
   return new OrderServiceBean();
}
}
<bean id="orderService" class="工厂类路径" factory-method="工厂类方法名称createOrder"></bean>
3、实例工厂方法实例化
public class OrderFactory{
public OrderServiceBean createOrder(){
   return new OrderServiceBean();
}
}
<bean id="orderServiceFactory" class="工厂类路径"></bean>
<bean id="orderService" factory-bean="工厂类路径" factory-method="工厂类方法名称createOrder"></bean>

spring容器管理的bean对象的作用于
1、默认情况下是单实例(同一个bean);
2、<bean id="orderService" class="bean的路径" scope="prototye"></bean>添加了scope属性指定作用域时可以在每次getBean方法调用时生成一个新的实例;

spring管理的bean的生命周期
1、在默认情况下,在容器实例化的时候就对bean进行实例化了;
2、当scope="prototye"时,就在getBean方法时才实例化bean;
3、可以在beans.xml文件中设置lazy-init="true"来延迟初始化(在容器实例化的时候bean不实例化),实际中一般不用这个属性;
4、可以在beans.xml文件中设置init-method="方法名称"来在实例化bean完成后立刻调用该方法;
5、可以在beans.xml文件中设置destroy-method="方法名称"来在bean销毁的时候调用该方法;(默认情况下只有在应用关闭的时候才会关闭spring容器实例)正常的关闭方法是:
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");(注意是抽象类)
ctx.close();


依赖注入
在程序运行期间,由外部容器动态的将以来对象注入到组件中;
############就是让容器来实例化类(被注入的类),一定要添加属性的set方法#########################
注入方法:
1、通过属性的setter方法注入:(手动)
<bean id="personDao" class="business.dao.impl.PersonDaoBean"></bean>
<bean id="personServiceBean" class="businessbeans.impl.PersonServiceBean" init-method="open" destroy-method="destroy">
<property name="pd" ref="personDao"></property>
</bean>
使用bean的构造器注入依赖对象或基本类型
被注入的类中需要有构造函数,例如
public PersonServiceBean(PersonDao pd, String name) {
   super();
   this.pd = pd;
   this.name = name;
}
2、使用内部bean的方式,但该bean不能被其他bean使用:(不常用,手动)
<bean id="personServiceBean" class="businessbeans.impl.PersonServiceBean" init-method="open" destroy-method="destroy">
   <property name="pd">
   <bean id="personDao" class="business.dao.impl.PersonDaoBean"></bean>
</property>
</bean>
3、最优雅的依赖属性注入方法:使用Field注入(一般用于注解方式,手动)
添加lib\j2ee\commons-annotation.jar
添加context容器,并打开
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xmd
@Autowired默认按类型匹配 @Resource默认按名称匹配,无匹配名称则按类型匹配(推荐Resource方法)
@Resource(可缺省name="")根据属性的名称去xml文件中寻找对应的id或name,如果找到直接注入/如果没找到匹配属性的类型与xml中配置的类型,相同则注入;
可以不写get set方法

自动装配(不推荐使用)
不需要注解,只需要在<bean></bean>标签中添加autowire属性;必须要有set方法

不同类型数据的注入
1、类类型(如上)
2、注入一个普通属性(如String name="yanzhexian");
<bean id="personDao" class="business.dao.impl.PersonDaoBean"></bean>
<bean id="personServiceBean" class="businessbeans.impl.PersonServiceBean" init-method="open" destroy-method="destroy">
<property name="pd" ref="personDao"></property>
<property name="name" value="yanzhexian"></property>
</bean>
PS:spring会自动转换注入的普通类型到类中定义的类型
3、注入集合类型(set map list等)
set类型
<property name="sets">
   <set>
    <value>第一个</value>
    <value>第二个</value>
    <value>第三个</value>
   </set>
</property>


list类型
<property name="lists">
   <list>
    <value>一</value>
    <value>二</value>
    <value>三</value>
   </list>
</property>

property类型
<property name="properties">
   <props>
    <prop key="key1">value1</prop>
    <prop key="key2">value2</prop>
    <prop key="key3">value3</prop>
   </props>
</property>

Map类型
<property name="maps">
   <map>
    <entry key="key1" value="value1"></entry>
    <entry key="key2" value="value2"></entry>
    <entry key="key3" value="value3"></entry>
   </map>
</property>

PS:集合类型的定义与遍历
private Set<String> sets=new HashSet<String>();
private List<String> lists= new ArrayList<String>();
private Properties properties =new Properties();
private Map<String,String> maps=new HashMap<String,String>();
 

//尝试输出以来注入的集合属性sets
   System.out.println("=======Set======");
   for(String value:this.getSets()){
    System.out.println(value);
   }
//尝试输出依赖注入的list集合属性lists
   System.out.println("=======List======");
   for(String value:this.getLists()){
    System.out.println(value);
   }
//尝试输出依赖注入的Properties集合属性maps
   System.out.println("=======Properties======");
   for(String key:this.getProperties().keySet()){
    System.out.println(key+"="+this.getProperties().get(key));
   }
 
//尝试输出依赖注入的Map集合属性maps
   System.out.println("=======Map======");
   for(String key:this.getMaps().keySet()){
    System.out.println(key+"="+this.getMaps().get(key));
   }

给spring配置文件减肥(在类路径下用自动扫描的方式,注,没有添加依赖注入是不会有依赖属性注入的)
1、引入context这个命名空间和xmd文件
2、<context:component-scan base-package=""></context:component-scan>
3、标注@Service业务层 @Controller控制层 @Repository DAO层 @Component其他层
@Scope指定作用域范围 @PostConstruct指定bean初始化方法 @PreDestroy指定容器容器关闭的方法
4、使用注解时当不指定bean的名称,bean的名称就是类的简单名称(第一个字母小写)
5、可以在注释后面直接添加名称来定义bean的名称@Service("NAME");


AOP代理对象(主要是对权限管理模块应用)
代理对象--------目标对象
1、拦截所有业务方法,判断用户是否有权限,有权限就允许它执行业务方法,没有权限就不允许它执行业务方法(是否有权限根据user是否为null模拟)
2、用JDK提供的Proxy代理类来创建代理对象(目标对象必须实现接口);

如果目标对象没有实现接口
1、引入cglib.jar文件
2、用cglib来创建代理对象:
Enhancer enhancer = new Enhance();
enhancer.setSuperclass(this.targetObject.getClass);
enhancer.setCallback(this);
return enhancer.create();

PS:代理类一定要实现MethodInterceptor接口,并通过该接口来调用目标类中的业务方法;
AOP概念:前置通知,后置通知,例外通知,最终通知,环绕通知
Aspect切面 joinpoint连接点(拦截到的每一个业务方法) pointcut切入点(拦截所有的业务方法)
Advice通知() Target目标对象 Weave织入 Introduction引入


用spring进行AOP编程:
1、引入jar文件
2、配置配置文件:引入AOP的命名空间
分享到:
评论

相关推荐

    Spring框架基础总结

    Spring的基础总结,是一片很好的适合初学者查看的资源

    Spring总结(四)

    Spring个人总结,基于Annotation注解的方式开发,配置

    Spring学习总结笔记

    Spring学习笔记,包含了sping学习所有知识点,和例子一个综合的学习总结

    spring ioc aop基础理论实践笔记

    1,spring是一个开源的免费的框架(容器)。 2,spring是一个轻量级的,非入侵式的框架。 ​ 非入侵式:就是项目引入了这个框架之后,...总结:spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架。

    spring笔记.pdf

    spring笔记spring基础笔记

    SpringCloud视频教程全套百度网盘

    第1章 SpringCloud简介 第2章 SpringCache缓存详细讲解及应用 第3章 Redis高级缓存讲解及应用 第4章 SpringSecurity...学习SpringCloud前面要对SpringBoot有一定的了解及应用,在没SpringBoot 基础的建议先学习一下。

    Spring&Mybatis&SpringMVC总结笔记-最全最基础.pdf

    这是我搜集的Spring、Mybatis、SpringMVC笔记,结合自己的理解,又进行加工,是一个很不错的SSM入门学习、面试的好资料。建议收藏哦

    spring杂谈 作者zhang KaiTao

    1.9 Spring对事务管理的支持的发展历程(基础篇) 1.10 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。 1.11 在spring中获取代理对象代理的目标对象工具类 1.12 如何为...

    Spring开发指南

    构建Spring基础代码 Spring 基础语义 Dependency Injection 依赖注入的几种实现类型 Type1 接口注入 Type2 设值注入 Type3 构造子注入 几种依赖注入模式的对比总结 Spring Bean封装机制 Bean Wrapper ...

    spring知识点梳理

    spring从搭建配置到简单开发的一个整体流程,比较适合打基础的朋友!

    spring3的总结

    对一些spring用法的总结,有点基础的课可以看看,这样会省事很多啊。

    想学习的看过来了spring4.0、springboot、springcloud详细视频课程(硅谷)

    Spring boot 基础包 10 6. spring boot 分解 11 6.1 提供 Spring MVC自动配置 11 6.2 对静态资源的支持 11 6.3 模板引擎 12 6.3.1 Thymeleaf模板引擎 12 ....... Springcloud: 通过整合SpringMVC+...

    spring常用模块介绍

    Spring框架基础学习,比较适合初学者,总结了Spring相关概念

    spring boot+spring cloud视频教学下载全套

    ├12 4.8 Feign-1 Feign的简介及基础使用.avi ├13 4.9 Feign-2覆写Feign的默认配置.avi ├14 4.10 Fegion-3覆写Fegion的默认配置及Fegion的日志.avi ├15 4.11 Fegion-4解决Fegion第一次请求timeout的问题.avi ├16...

    Spring特性.xmind

    Spring框架基础学习,比较适合初学者,总结了Spring相关概念

    spring.net中文手册在线版

    Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。 第一章 序言 第二章 简介 2.1.概述 2.2.背景 2.3.模块 2.4.许可证信息 2.5.支持 第三章 背景 3.1.控制反转 第...

    Spring3.X编程技术与应用,完整扫描版

    全书分3篇共21章,具体内容包括:Spring环境的安装与使用、JSP与JSTL简介、 Spring基础概念与工具、用SpringJdbcTemplate访问数据库、使用Mayen工程、Spring MVC编程、基于 MVC的资源共享网站设计、Spring的AOP编程...

    SpringSecurity3.1实际摸索总结

    文档包含各种重要命令参数的解析、具体的原理、具体的代码分析、实现资源也从数据库进行验证。在别人资料基础上做了的优化和总结。绝对物有所值。

    Spring 2.0 开发参考手册

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@...

Global site tag (gtag.js) - Google Analytics