读书笔记之Spring实战

版权声明:本文为博主原创文章,未经博主允许不得转载。【huawangxin】 https://blog.csdn.net/huawangxin/article/details/76768817

《Spring实战(第3版)》

第1章

(个人觉得可以忽略)

第2章 装配Bean
1、声明Bean
1)使用一个或多个XML文件作为配置文件;
普通spring配置文件模板:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

</beans>
除了beanss命名空间外,spring的核心框架自带了10个命名空间配置(aop/bean/context/jee/jms/lang/mvc/oxm/tx/util),和Spinrg Portfolio的其他命名空间。
<bean id="duke" class="com.springinaction.springidol.Juhhler"/>
所有spring bean默认都是单利,当容器分配一个bean时(无论是通过装配或调用容器的getBean()方法),他总返回同一个实例。但是当我们需要每次请求都新创建一个实例时,需要配置bean的scope属性为prototype。
初始化和销毁bean:方法1)使用init-method和destory-method参数配置bean元素;方法2)让bean实现InitializingBean,DisposableBean接口。
2)提供基于java注解的配置方式;
2、构造器注入和Setter方法注入
3、装配Bean
4、控制Bean的创建和销毁
使用<property>元素配置bean属性,调用属性的setter方法注入值。(注入简单值<property name="" value=""/> 引入其他bean<property name="" ref=""/> 注入内部bean<property name="" ><bean class="..."/></property>)
使用p标签装配属性(引入xmlns:p="http://www.springframework.org/schema/p"),例如:
<bean id="northMan" class="com.wangxinxin.entity.NorthMan" p:a="testA" p:sonnet29-ref="sonnet29" />
-ref后缀作为一个标志来告知装配一个引用而不是字面量。
<!-- 装配List、Set和Array Map -->
 <bean id="colloection1" class="com.wangxinxin.entity.Colloection1">
  <property name="northMans">
   <list>
    <ref bean="northMan"/>
    <ref bean="northMan2"/>
   </list>
  </property>
  <property name="set1">
   <set>
    <ref bean="northMan"/>
    <ref bean="northMan2"/>
   </set>
  </property>
  <property name="map1">
   <map>
    <entry key="no1" value-ref="northMan"/>
    <entry key="no2" value-ref="northMan2"/>
   </map>
  </property>
  <!-- map的key和value都是string时优先使用properties -->
  <property name="map2">
   <props>
    <prop key="no1">111111111</prop>
    <prop key="no2">222222222</prop>
   </props>
  </property>
 </bean>
<property>元素用于把值或Bean引用注入到Bean的属性中;
<props>元素用于定义一个java.util.Properties类型的集合值;
<prop>元素用于定义<props>集合的一个成员。
通常默认属性值为null,但是有些属性默认不是null,这时我们需要显式设置null值
<property name="test"><null/></property>
使用SpEL表达式装配:#{}
1)可以存放基本数据类型;2)存放其他bean的id,或者bean的属性或方法。(<property name="test1" value="#{class1.getA()?.toUpperCase()}"/>);3)操作类T{}(<property name="test2" value="#T(java.lang.Math).PI"/>);4)表达式;5)访问集合成员;6)查询集合成员(.?[] .^[] .$[]);
EQ 就是 EQUAL等于
NQ 就是 NOT EQUAL不等于 
GT 就是 GREATER THAN大于  
LT 就是 LESS THAN小于 
GE 就是 GREATER THAN OR EQUAL 大于等于 
LE 就是 LESS THAN OR EQUAL 小于等于

第3章 最小化spring xml配置
1、bean的自动装配;
2、bean的自动检测;
1)4种类型的自动装配(byName/byType/constructor/autodetect)
eg:
<bean id="test" class="..." autowire="byName"></bean>
为属性自动装配ID与该属性的名字相同的Bean。
<bean id="test" class="..." autowire="byType"></bean>
为属性自动装配ID与该属性的类型相同的Bean。存在多个则报异常。可以通过标志一个首选bean或取消某个bean的候选资格(排除首选primary的true/false,默认true;排除bean autowire-candidate="fasle")。
<bean id="test" class="..." autowire="constructor"></bean>
constructor自动装配
<bean id="test" class="..." autowire="autodetect"></bean>
最佳自动装配
<beans ..... default-autowire="byType"></beans>
全局默认自动装配
可以混合使用自动装配和显式装配。
3、面向注解的bean装配;
容器默认禁用注解装配。
<context:annotation-config />
支持3种不同的注解(spring自带的@Autowired注解;JSR-330的@Inject注解;JSR-250的@Resource注解)
@Autowired可以标注set方法、构造器、属性变量,其中属性不一定要装配,null值也是可以接受的。(@Autowire(required=fasle),默认true);存在多个相同bean时,@Autowire @Qualifier("class1")
/**
 * Spring自定义限定器
 * @author huawangxin
 * 2017年8月6日 上午11:11:30
 */
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface StringedInstrument {
}
借助@Inject实现基于标准的自动装配。和@Autowire用法一样,但没有required属性。@Named类似于@Qualifier用法。
JSR-330的Provider接口可以实现Bean引用的延迟注入以及注入Bean的多个实例功能。JSR-330不建议使用javax.inject的@Qualifier,建议创建自定义的限定器注解。
/**
 * JSR-330自定义限定器
 * @author huawangxin
 * 2017年8月6日 下午1:09:38
 */
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface StringedInstrument2 {
}
在注解注入中使用表达式(@Value("112"),也可以是SpEL表达式)
自动检测Bean:
<context:component-scan base-package="com.wangxinxin.entity">
     <context:include-filter type="assignable" expression="com.wangxinxin.entity.Jugger"/>
     <context:exclude-filter type="annotation" expression="com.wangxinxin.util.StringedInstrument2"/>
    </context:component-scan>
为自动检测标注Bean:@Component-通用构造型注解;@Controller-标记该类定义为SpringMVC controller;@Repository标记该类为数据仓库;@Service-标记该类为服务。
过滤器类型(annotation/assignable/aspectj/custom/regex)
4、基于java的spring配置;
除了加载自动注解的bean,还回自动加载使用@Configuration注解所标注的类。
@Configuration注解的类等价于<beans>元素
/**
 * 配置类
 * @author huawangxin
 * 2017年8月6日 下午1:51:06
 */
@Configuration
public class SpringIdolConfig {
 @Bean
 public NorthMan northMan() {
  return new NorthMan();
 }
}

第4章
待续。。。。。。



















猜你喜欢

转载自blog.csdn.net/huawangxin/article/details/76768817