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. Objective-C:运行时runtime

    1.是否可以把比较耗时的操作放在通知中心中?   通知在哪一个线程发的,那么对通知事件的处理就在同一个线程中进行; 如果在异步线程发的通知,那么可以执行比较耗时的操作: 如果在主线程发的通知,那么就不 ...

  2. 【前端自动化构建 grunt、gulp、webpack】

    参考资料: 用自动化构建工具增强你的工作流程!:http://www.gulpjs.com.cn/ gulp详细入门教程:http://www.ydcss.com/ JavaScript构建(编绎)系 ...

  3. Scala 将BigDecimal转换为Long

    待转换.asInstanceOf[Number].longValue (Double转为Long也适用)

  4. Android项目总结

    功能: 1.图片载入 ImageLoader 參数配置要合理    cacheMemory 一次性的图片最好不要缓存在内存中   合理控制在内存中的内存大小 ,适当的释放   volley是googl ...

  5. HDU 3917 Road constructions(最小割---最大权闭合)

    题目地址:HDU 3917 这题简直神题意... 题目本身就非常难看懂不说..即使看懂了.也对这题意的逻辑感到无语...无论了.. 就依照那题意上说的做吧... 题意:给你n个城市,m个公司.若干条可 ...

  6. Oracle,跳出游标循环

    1,跳出游标的循环,不执行遍历了. 方法一:goto for c_row in 游标 loop if 条件 then dbms_output.put_line('测试跳出循环'); goto brea ...

  7. Spring 注入简介

    注入方式有三种,setter,构造方法,接口注入.   常用的是setter注入和构造方法注入.   setter注入: <?xml version="1.0" encodi ...

  8. iOS原生推送(APNS)进阶iOS10推送图片、视频、音乐

    代码地址如下:http://www.demodashi.com/demo/13208.html 前言 我们首先要在AppDelegate里面进行iOS的适配,可以参考这篇文章 iOS原生推送(APNS ...

  9. 用JS将json日期格式化成正常日期

       function ChangeDateFormat(cellval) {             var date = new Date(parseInt(cellval.replace(&qu ...

  10. javaweb项目开发错误代码

    HTTP状态码(HTTP Status Code) 一些常见的状态码为:200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释:点击查看 1xx(临时响应) ...