1、pojo

2、为了降低java开发的复杂性,spring采用了4中策略

(1)、基于POJO的轻量级和最小侵入性编程

(2)、通过依赖注入和接口编程实现松耦合

(3)、基于切面和惯例进行声明式编程

(4)、通过切面和模板减少样板式代码

3、依赖注入(DI):让相互协作的软件组件保持松耦合

4、面向切面编程(AOP):允许把遍布各处的应用功能分离出来形成可重复的组件。AOP确保POJO保持简单。

5、创建组件之间协作的行为通常称为装配,spring通过应用上下文(application context)装载bean的定义并把它们组装起来

  • Spring在配置xml文件的过程中,如果有singleton作用域依赖prototype作用域的bean时,那么singleton作用域的Bean就只有一次的更新机会,它的依赖关系也只是在初始化阶段被设置,导致singleton作用域的Bean的依赖得不到及时更新。
  • 解决办法:在bean的文件中配置此<lookup-method/>节点解决
  • PropertyPathFactoryBean,

(1)、用来获取目标的属性值,实际上就是目标Bean的getter方法的返回值,获得的值可以 注入给其他的Bean,也可以直接定义为新的bean;

<!-- 将指定Bean实例的属性值定义成指定Bean实例 -->
<bean id="son1"
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- 确定目标Bean,表明son1 Bean来自哪个Bean的属性 -->
<property name="targetBeanName" value="person"/>
<!-- 确定属性表达式,表明son1 Bean来自目标bean的哪个属性 -->
<property name="propertyPath" value="son"/>
</bean> <!-- 如下定义son2的 Bean,该Bean的age属性不是直接注入
,而是依赖于其他Bean的属性值 -->
<bean id="son2" class="org.app.service.Son">
<property name="age">
<!-- 以下是访问Bean属性的简单方式,这样可以将person Bean的son属性的、
age属性赋值给son2这个bean的age属性-->
<bean id="person.son.age" class=
"org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
</property>
</bean>

(2)、使用PropertyPathFactoryBean时,

targetBeanName:确定指定目标Bean,确定获取那个Bean的属性值

propertyPath:用于指定属性,确定获取目标Bean的哪个属性值,此处的属性可以直接使用复合属性的形式。

<!-- 将基本数据类型的属性值定义成Bean实例 -->
<bean id="theAge2" class=
"org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- 确定目标Bean,表明theAge2 Bean来自哪个Bean的属性。
此处采用嵌套Bean定义目标Bean -->
<property name="targetObject">
<!-- 目标Bean不是容器中已经存在的Bean, 而是如下的嵌套Bean-->
<bean class="org.crazyit.app.service.Person">
<property name="age" value="30"/>
</bean>
</property>
<!-- 确定属性表达式,表明theAge2 Bean来自目标bean的哪个属性 -->
<property name="propertyPath" value="age"/>
</bean>

  

  • FieldRetrievingFactoryBean类,通过FieldRetrievingFactoryBean获取目标的Field值之后,得到的值可以注入给其他的Bean,也可以直接定义新的Bean
<!-- 将java.sql.Connection的TRANSACTION_SERIALIZABLE
的值赋给son的age属性-->
<bean id="son" class="org.app.service.Son">
<property name="age">
<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
</property>
</bean>

  在使用FieldRetrievingFactoryBean获取field值时,必须指定如下的属性值:

targetClass:所在的目标类或者目标对象,如果需要获得Field是静态字段,则使用targetClass指定目标类。

targetField:用于指定目标Field的Field值

<!-- 将Field 值定义成Bean 实例-->
<bean id="theAge1" class=
"org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<!-- targetClass指定Field所在的目标类 -->
<property name="targetClass" value="java.sql.Connection"/>
<!-- targetField指定Field名 -->
<property name="targetField" value="TRANSACTION_SERIALIZABLE"/>
</bean> <!-- 将Field 值定义成Bean实例 -->
<bean id="theAge2" class=
"org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<!-- value指定采用哪个类的哪个静态域值 -->
<property name="staticField"
value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
</bean>
  • MethodInvokingFactoryBean:通过此工厂,可以讲指定方法的返回值注入到目标的属性值,MethodInvokingFactoryBean用来获取指定方法的返回值;获得的返回值既可以注入到指定的Bean实例的指定属性,也可以直接定义为Bean的实例,代码实例如下:
<bean id="son2" class="org.app.service.Son">
<property name="age">
<bean class=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<!-- targetClass确定目标类,指定调用哪个类 -->
<property name="targetClass" value="org.app.util.ValueGenerator"/>
<!-- targetMethod确定目标方法,指定调用目标class的哪个方法。
该方法必须是静态方法-->
<property name="targetMethod" value="getStaticValue"/>
</bean>
</property>
</bean>

  

spring事件有下面两个成员:

1、ApplicationEvent,容器事件,由容器发布

2、ApplicationListener 监听器,可以由容器中的任何监听器Bean担任

(1)先顶一个spring的容器事件:

package cn.study.basic;

import org.springframework.context.ApplicationEvent;

public class EmailEvent extends ApplicationEvent {
private String address;
private String text; public EmailEvent(Object source) {
super(source);
} public EmailEvent(Object source, String address, String text) {
super(source);
this.address = address;
this.text = text;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} }

(2)编写容器监听器代码:

package cn.study.basic;

import org.springframework.context.ApplicationListener;

public class EmailListener implements ApplicationListener<EmailEvent> {

	@Override
public void onApplicationEvent(EmailEvent arg0) {
System.out.println(arg0 instanceof EmailEvent);
if (arg0 instanceof EmailEvent) {
EmailEvent ee = (EmailEvent) arg0;
System.out.println("address:" + ee.getAddress());
} else {
System.out.println("container:" + arg0);
}
} }

(3)、bean.xml文件中加入如下配置:

<bean class="cn.study.basic.EmailListener"></bean>

(4)、测试方法

package cn.study.basic.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.study.basic.EmailEvent; public class TestAMain {
@Test
public void testApp() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
EmailEvent emailEvent = new EmailEvent("object", "address", "test");
context.publishEvent(emailEvent);
}
}

运行代码,执行结果如下所示:

true
address:address

涉及spring的相关概念的更多相关文章

  1. Java中sleep()与wait()区别(涉及类锁相关概念)

    在区别之前,我们首先先了解一下关于对象锁,类锁的相关概念(当时查阅的详细地址:http://www.importnew.com/20444.html,该作者对类锁和对象锁进行了详细的举例分析) 对象锁 ...

  2. spring框架相关概念

    软件行业的二八法则?技术中只有20%是最常用和最关键的,决定你的基础,后面的80%决定你的潜能! 概念: 1,轻量级框架,用户需要什么功能就自己添加相应的功能模块,不像重量级框架,一旦用,所有功能都添 ...

  3. 关于Unity中的涉及到Attribute的相关概念整理(@WhiteTaken)

    这两天事情比较多,没有来得及更新,现在把我这两天看的attributes相关内容进行整理. 涉及到的相关概念包括: C#中的特性概念及用法 创建自己的特性以及通过反射访问特性 C#中的特性概念以及用法 ...

  4. spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务

    文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...

  5. Spring家族主流成员介绍

    摘 要:Spring 就像一个大家族,有众多衍生产品例如 Boot,Security,JPA等等.但他们的基础都是Spring 的 IOC 和 AOP,IOC提供了依赖注入的容器,而AOP解决了面向切 ...

  6. 图书-技术-SpringBoot:《Spring Boot2 + Thymeleaf 企业应用实战》

    ylbtech-图书-技术-SpringBoot:<Spring Boot2 + Thymeleaf 企业应用实战> <Spring Boot 2+Thymeleaf企业应用实战&g ...

  7. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  8. 7天学会spring cloud教程

    按照官方的话说:Spring Cloud 为开发者提供了在分布式系统(如配置管理.服务发现.断路器.智能路由.微代理.控制总线.一次性 Token.全局锁.决策竞选.分布式会话和集群状态)操作的开发工 ...

  9. Spring实战5:基于Spring构建Web应用

    主要内容 将web请求映射到Spring控制器 绑定form参数 验证表单提交的参数 对于很多Java程序员来说,他们的主要工作就是开发Web应用,如果你也在做这样的工作,那么你一定会了解到构建这类系 ...

随机推荐

  1. ubuntu查看系统版本

    1.查看文件信息,包含32-bit就是32位,包含64-bit就是64位 root@HDController:/home/nulige/tools# uname -a Linux HDControll ...

  2. oracle 10g函数大全--聚合函数

    AVG([distinct|all]x) [功能]统计数据表选中行x列的平均值. [参数]all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all 如果有参数distinct或 ...

  3. Snapdragon profiler连android手机

    oppo11 晓龙660 找一根好用的usb数据线 去设置->开发者选项->usb调试 打开(十分钟会自动关,注意再开开) 去windows cmd ===adb devices 会列出这 ...

  4. 播放器设置 Player Settings

    原地址:http://game.ceeger.com/Manual/class-PlayerSettings.html#Android Player Settings is where you def ...

  5. 实用且免费API接口2

    之前已经整理过一些免费API,现在在知乎专栏上看到别人整理的一些实用免费API,有一些是没有重复的,因此也搬过来. 今天的内容,很适合你去做一些好玩.实用的东西出来. 先来科普个概念,开放应用程序的A ...

  6. sqlite3 sqlite3_prepare、sqlite3_step使用

    void select_by_prepare (sqlite3* pDB){ 51     int i; 52     int ret = 0; 53     int time; 54     cha ...

  7. dubbo笔记

    使用Maven打包依赖项,启动时从本地jar中读取dubbo.xsd 最近项目用到dubbo,打包启动时报错 Failed to read schema document from http://co ...

  8. 获取request中的查询参数

    //获取request中的查询参数 public static Map<String, Object> getRequestParamsByMap(HttpServletRequest r ...

  9. 改变Fragment的默认动画

    FragmentTransaction ft = getFragmentManager().beginTransaction(); //设置进入退出动画 ft.setCustomAnimations( ...

  10. SVN Client API的.net 接口 SharpSvn介紹 Checkout操作实例

    Subversion是一個文件版本管理工具, 廣泛的被大家採用來作為源代碼版本管理. 已有的工具不管是其自帶的命令行工具還是Windows UI的tortoiseSVN等還是很方便實用的, 但是如果想 ...