day38 10-Spring的Bean的属性的注入
后处理bean,如果是返回bean,那么什么都不做直接把这个类原封不动地给你返回回去。
在它执行一些逻辑方法的时候对它进行逻辑增强,比如说进行时间监控,权限管理,日志的记录等等。
要做肯定是对正常的类增强才可以,因为正常你就调用这个类的add或者find()方法了。
增强的方法:继承、装饰者模式、动态代理。
装饰者的缺点是:如果你的接口中的方法太多,而你只需要增强其中的某一个方法,那你这个类里面就得写特别特别多的方法。而且其他的方法都是原封不动地给人家返回的。
动态代理:最灵活。对这个类生成一个代理类,Proxy.newProxyInstance()对bean这个类生成代理。
public Object postProcessAfterInitialization(final Object bean, String beanName){
//不是对所有的类都增强,如果想对所有的类都增强,直接在这里生成代理类即可。
//如果只想对某个类增强,判断这个类的名字是不是我们在Spring中注册的那个customerService.
//Spring可能配置了非常多的类,但是我只增强customerService.如果你配置了b1、b2、b3,那么b1、b2、b3正常也会执行我们的后处理bean.
//调用业务的任何一个方法(调用add()或者是find())都相当于执行InvocationHandler的invoke()方法.那就可以在它执行之前或者是之后做一些事情.
//不管怎么做都会生成一个customerService的代理类.代理类才有增强的代码,不是代理类就没有增强的代码.所以判断,如果这个类是customerService,返回代理类;如果不是customerService,把没有增强的类返回.
//通过后处理bean对某些类进行增强.
}
其实Spring AOP的思想的有一部分是基于后处理bean。
package cn.itcast.spring3.demo5; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest5 { @Test
public void demo5(){//Spring注入属性的两种方式:一种是构造器的注入
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car); }
@Test
public void demo6(){//还有一种是setter方法的注入
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2); } }
package cn.itcast.spring3.demo5;
public class Car2 {
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "Car2 [name=" + name + ", price=" + price + "]";
}
}
package cn.itcast.spring3.demo5;
public class Car {
private String name;
private Double price;
public Car() {
super();
}
public Car(String name, Double price) {
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 别去schema,schema是文件,本地的文件,你得引那个头 --> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- demo1快速入门================================================= -->
<!-- 把接口和实现类在这个配置文件中配置,有了实现类的全路径之后到时候才能用工厂反射 --> <!-- 通过一个<bean>标签来设置类的信息,通过id属性为类起个标识. -->
<!-- 接口,实现类,配置文件也都有了 -->
<!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
<!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
也是这么做的,只不过工厂由Spring提供好了
-->
<bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
<!-- 使用<property>标签注入属性
value指的是普通值
ref指的是对象
-->
<property name="info" value="传智播客"></property>
</bean>
<!-- demo1快速入门 -->
<!-- demo2Bean的实例化 -->
<!-- 默认情况下使用的就是无参数的构造方法. -->
<!--
<bean id="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
-->
<!--
<bean name="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
-->
<!-- 第二种使用静态工厂实例化 不能写class了,因为现在不是由Spring直接帮你创建对象了-->
<!--
<bean id="bean2" class="cn.itcast.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>
-->
<!-- 第三种使用实例工厂实例化 -->
<!--
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
-->
<!-- 要先把Bean3Factory实例化 -->
<!--
<bean id="bean3Factory" class="cn.itcast.spring3.demo2.Bean3Factory"></bean>
-->
<!-- demo2Bean的实例化====================== end--> <!-- demo3Bean的作用范围======================= -->
<!--
<bean id="customer" class="cn.itcast.spring3.demo3.Customer" scope="prototype"></bean>
-->
<!--
<bean id="product" class="cn.itcast.spring3.demo3.Product" init-method="setup" destroy-method="teardown" scope="singleton">
<property name="name" value="空调">-->
<!-- 把Product类的属性name注入进来 -->
<!--
</property>
</bean>
--> <!-- demo4Bean的生命周期======================= -->
<bean id="customerService" class="cn.itcast.spring3.demo4.CustomerServiceImpl" init-method="setup" destroy-method="teardown"> <property name="name" value="itcast"></property>
</bean>
<!-- 后处理Bean是由Spring容器自动调用不用你管,我们起个id是为了我们在程序中去获得它。但是这个类不用由我们获得, 由Spring自动调用。cn.itcast.spring3.demo4.MyBeanPostProcessor是后处理Bean-->
<bean class="cn.itcast.spring3.demo4.MyBeanPostProcessor"></bean>
<!-- demo5Bean的属性注入=====================================================================================================================================-->
<!-- 构造方法的注入 -->
<bean id="car" class="cn.itcast.spring3.demo5.Car">
<!--
<constructor-arg name="name" value="宝马"></constructor-arg>
-->
<!-- 通过这个标签为类注入属性 -->
<!--
<constructor-arg name="price" value="1000000"></constructor-arg>
-->
<!-- 通过这个标签为类注入属性 -->
<constructor-arg index="0" type="java.lang.String" value="奔驰"></constructor-arg><!-- 通过这个标签为类注入属性 -->
<constructor-arg index="1" type="java.lang.Double" value="2000000"></constructor-arg><!-- 通过这个标签为类注入属性 --> </bean> <bean id="car2" class="cn.itcast.spring3.demo5.Car2">
<!-- <property>标签中name就是属性名称,value是普通属性的值,ref:引用其他的对象 -->
<property name="name" value="保时捷"></property>
<property name="price" value="5000000"></property>
</bean> </beans>



day38 10-Spring的Bean的属性的注入的更多相关文章
- Spring中bean的四种注入方式
一.前言 最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...
- 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值
7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...
- day38 11-Spring的Bean的属性的注入:对象属性
package cn.itcast.spring3.demo5; public class Person { private String name;//人的名字 private Car2 car2; ...
- day38 14-Spring的Bean的属性的注入:集合属性的注入
集合:List.Set.Map. package cn.itcast.spring3.demo6; import java.util.List; import java.util.Map; impor ...
- day38 13-Spring的Bean的属性的注入:SpEL注入
Spring2.5提供了名称空间p注入属性的方式,Spring3.几提供了SpEL属性注入的方式. <?xml version="1.0" encoding="UT ...
- spring 引用Bean的属性值
引用Bean的属性值 从Spring3.0开始,可以通过#{beanName.beanProp}的方式方便地引用另一个bean的属性值1.不需要使用PropertyPlaceholderConfigu ...
- Spring中对象和属性的注入方式
一:Spring的bean管理 1.xml方式 bean实例化三种xml方式实现 第一种 使用类的无参数构造创建,首先类中得有无参构造器(重点) 第二种 使用静态工厂创建 (1)创建静态的方法,返回类 ...
- Spring - IoC(5): 集合属性的注入
如果 Bean 的属性是个集合,则可以使用 <list/>.<set/>.<map/> 和 <props/> 元素向 List.Set.Map 和 Pr ...
- Spring中bean作用域属性scope
关键字: spring中属性scope的prototype是什么意思 默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: Spri ...
随机推荐
- C++编程规范和编译过程详解
前言:因为c++基础打得不牢,所以准备花点时间再学一下c++的基础知识,主要是看网易云课堂里面的免费课程,把一些知识点做个笔记记下来. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- 安卓手机端微信网页浏览记录清理debugx5.qq.com
最近我们环境从复)星(云切换到阿里云.早上地铁路上就有小伙伴@,一阵搜索.找的如下的方法. 记录一下: 目前只支持安卓手机的微信内置浏览器清理. 由腾讯提供的网址http://debugx5.qq.c ...
- TableView之表头、表尾,区头、区尾!
一.UITableView的UITableViewStyle 样式分为UITableViewStylePlain和UITableViewStyleGrouped两种: plain样式下区头和区尾是悬浮 ...
- 微信开发SDK支持小程序 ,Jeewx-Api 1.3.1 版本发布
JEEWX-API 是一款JAVA版的微信开发SDK,支持微信公众号.小程序.微信企业号.支付宝生活号SDK和微博SDK.你可以基于她 快速的傻瓜化的进行微信开发.支付窗开发和微博开发. 基于jeew ...
- 封装了一个HOOKAPI的类。。。
我惊奇地发现,我手竟然这么生了..... 很久很久没有写这方面的东西,现在写着竟然这么手生....哎.... 这玩艺,还得天天练....
- 使用springboot上传文件至nginx代理服务器
nginx配置图片服务器 server { listen 8001; server_name image.xxx.com; proxy_set_header X-Forwarded-Host $hos ...
- tyvj 1266 费解的开关
传送门 解题思路 枚举第一行的状态,判断后面可不可行. 代码 #include<iostream> #include<cstdio> #include<cstring&g ...
- 进一步封装poco下的mysql操作
为方便程序对mysql操作,我对poco的mysql进行了再次封装,主要是针对自己应用需要的部分. 开发工具:netbean 系统环境:centos7 poco版本: poco-1.9.0-all 主 ...
- 爬虫(三)通过Selenium + Headless Chrome爬取动态网页
一.Selenium Selenium是一个用于Web应用程序测试的工具,它可以在各种浏览器中运行,包括Chrome,Safari,Firefox 等主流界面式浏览器. 我们可以直接用pip inst ...
- Bootstrap.之模态框 显示在遮罩层后面
Bootstrap.之模态框 显示在遮罩层后面 问题描述: 在使用bootstrap模态框,弹出的窗口在遮罩层后面,见图: 解决方案: 保证模态框的代码,所在的上一级(父元素)是body标签,即可.例 ...