`

spring

阅读更多
对于spring的看法觉得spring就相当于一个容器似的,可以装很多bean,然后对那些bean进行管理,首先是配置bean,我们可以这样写<bean id="xxx" class="xxx.xxx.xxx"/>其中id是唯一的。我们还可以给bean指定别名:<alias name="xxxx" alis="xx"/>,配置好了bean,我们就要先初始化他们,然后去调用它们,初始化呢,我们有三种方法,第一:构造器,第二:静态工厂,第三:实例工厂。然后我们调用bean,Resoruce resource = new FileSystemResource("bean.xml")  BeanFactory beanFactory=new XmlBeanFactory(resource);
这样我们就可以获取到在xml文件中配置的bean了。或者用applicationContent来获取:
ApplicationContent content = new ClassPathXmlApplicationContent("applicationContent.xml");
BeanFactory beanFactory = content;
得到了beanFactory以后就可以获取到你想到得到的bean。
在实例化bean的时候,如果要用到static工厂方法那么在<bean factory-method="createInstance"/>bean标签中中一定要使用factory-method属性。
在bean中要有一个static方法例如:
class ExampleBean(){
  private ExampleBean(Abean abean,Bbean bbean){
this.abean = abean;
this.bbean = bbean;

};
  public  static ExampleBean createInstance(Abean abean,Bbean bbean){
ExampleBean bean = new ExampleBean(abean,bbean);
return bean;

}


}

以上就是static工厂实例化bean方法。在配置文件中配置bean的时候我们可以在bean中加一个属性<idref = "beanId"/>这个属性可以验证在调用这个bean的时候这个bean是否存在。

关于注入有几种:set,get注入(同时配置文件要有bean注入配置),构造器注入(同上)。
<ref>标签是用来引用其他bean,内部bean则用<bean><property><bean/></property></bean>标签。注入list,map,set。后来出现自动注入@Autowired.

bean的作用域包括(singleton,prototype,request,session,global session)
如果你打算用一个作用域为request的baen注入到其他bean中,那么需要注入一个aop代理来替代被注入的作用域bean,也就是说,你需要注入一个代理对象,该对象具有与被代理对象一样的公共接口,而容器则可以足够智能的从相关作用域中(比如一个HTTP request)获取到真实的目标对象,并把方法调用委派给实际的对象。(这个理解不是很透彻)。

我们经常用ApplicationContext,但是ApplicationContext到底是什么?
beans包是对bean的管理,ApplicationContext则是以一种更加面向框架的方式增强了BeanFactory的功能。context包核心是ApplicatonContext接口由于ApplicationContext是由BeanFactory派生而来的所以ApplictionContext具有BeanFactory所有的功能。

spring国际化消息,可以自定义MessageSource或者是spring中的org.springframework.context.support.ResourceBundleMessageSource那么必须有属性文件在可以为list集合多个属性文件。获取国际化消息有三种方式:
String getMessage(String code, Object[] args, String default, Locale loc):用来从MessageSource获取消息的基本方法。如果在指定的locale中没有找到消息,则使用默认的消息。args中的参数将使用标准类库中的MessageFormat来作消息中替换值。

String getMessage(String code, Object[] args, Locale loc):本质上和上一个方法相同,其区别在:没有指定默认值,如果没找到消息,会抛出一个NoSuchMessageException异常。

String getMessage(MessageSourceResolvable resolvable, Locale locale):上面方法中所使用的属性都封装到一个MessageSourceResolvable实现中,而本方法可以指定MessageSourceResolvable实现。

注解:
@Autowired可以注解属性,方法,set,map(key为String类型,values为已知类型),构造器字段.
对于spring对classpath的扫描,我们可以用@component注解,@Repository,@service,@controller是对@component的细化,@Scope可以注解bean的作用域,如果要自定义检测bean名称策略,那么就要实现beanNameGenerator,自定义作用域那么要实现scopeMetadataResolver,注解元数据那么要用到@qualifiter("")等。

spring Validator :
实现validator接口,然后用rejectValue();来返回错误,然后用<spring:bind>来显示错误。
自定义转化,要继承PropertyEditorSupport或者实现PropertyEditorRegistrar ,然后在配置文件中注册这个转化<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  <property name="customEditors">
    <map>
      <entry key="example.ExoticType">
        <bean class="example.ExoticTypeEditor">
          <property name="format" value="upperCase"/>
        </bean>
      </entry>
    </map>
  </property>
</bean>


面向切面:
spring aop 两种代理机制,一个是jdk代理,一个是cglib代理
启动@AspectJ支持,<aop:aspectj-autoproxy/>然后就可以自定义aopbean
并且切入点表达式使用@Pointcut注解来表示(作为切入点签名的方法必须返回void 类型)。
关于切面注解匹配:@execution,@within,@target(目标bean),@annotation,@args,this
(代理bean)声明通知:@before,@afterReturnning(可以有返回值参数),@afterReturnningThrowing(可以有抛出什么异常的参数),@after,@around,@agrs("")(传递参数),






分享到:
评论

相关推荐

    spring.net中文手册在线版

    Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序。它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为...

    spring源码合集spring源码合集

    spring

    spring3.1 官方全部jar包

    spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....

    Spring MVC 入门实例

    这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...

    Getting started with Spring Framework: covers Spring 5(epub)

    Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...

    最新版本的Struts2+Spring4+Hibernate4框架整合

    项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...

    spring源码分析(1-10)

    Spring源代码解析(一):Spring中的事务处理 Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy ...

    spring3.0.0相关jar包

    spring3.0.0相关jar包 org.springframework.aop-3.0.0.RELEASE org.springframework.asm-3.0.0.RELEASE org.springframework.aspects-3.0.0.RELEASE org.springframework.beans-3.0.0.RELEASE org.springframework....

    SpringBoot+SpringCloud面试题.doc

    Spring boot 是 Spring 的一套快速配置脚手架,可以基于spring boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具;Spring boot专注于快速、方便集成的单个个体,Spring Cloud是关注...

    spring 3.2.4.RELEASE jar包

    spring 3.2.4 Realease 的所有jar包: spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-...

    spring-mock.jar

    Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring4.3.1官方全套jar包下载

    spring4.3.1全套jar下载。 4.3.1/spring-aop-4.3.1.RELEASE.jar 4.3.1/spring-aspects-4.3.1.RELEASE.jar 4.3.1/spring-beans-4.3.1.RELEASE.jar 4.3.1/spring-context-4.3.1.RELEASE.jar 4.3.1/spring-core-4.3.1....

    基于spring cloud 和vue全家桶的开源电商源码

    基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的开源电商源码基于spring cloud 和vue全家桶的...

    Spring技术内幕:深入解析Spring架构与设计原理(第2部分)

    Spring技术内幕:深入解析Spring架构与设计原理(第2部分) 《Spring技术内幕:深入解析Spring架构与设计原理》是Spring领域的问鼎之作,由业界拥有10余年开发经验的资深Java专家亲自执笔!Java开发者社区和Spring...

    Spring in Action 中文版 第五部分(Spring in Action CN.005)

    Spring in Action CN.001&lt;br&gt;Spring in Action CN.002&lt;br&gt;Spring in Action CN.003&lt;br&gt;Spring in Action CN.004&lt;br&gt;Spring in Action CN.005&lt;br&gt;Spring in Action CN.006&lt;br&gt;Spring in Action CN.007&lt;br&gt;Spring in ...

    org.spring-framework-3.0.4. 所有jar

    org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....

    尚硅谷SpringCloud第2季2020版.mmap

    一篇很好的springCloud学习的思维导读,详细的介绍了,springCloud的搭建步骤以及各组件的说明讲解 涵盖 Eureka服务注册与发现 Zookeeper服务注册与发现 Consul服务注册与发现 Ribbon负载均衡服务调用 OpenFeign...

    spring cloud 体系版本选型,涉及spring cloud alibaba spring boot spring cloud

    spring boot , spring cloud alibaba, spring cloub 版本选型

    Spring从入门到精通 源码

    本书由浅入深,循序渐进地介绍了Spring的体系结构和相关知识点,目的是帮助初学者快速掌握Spring,并能使用Spring进行应用程序的开发。本书最大的特色在于每章都是由浅入深,从一个简单的示例入手,让读者快速了解本...

Global site tag (gtag.js) - Google Analytics