Spring框架学习记录

配置SpringApplication

设置spring配置选项

配置Spring有两种方法:1,Xml;2,注解

基本配置概述

对于xml配置,需要声明Spring提供的命名空间基础信息;

要想使用Spring的注解支持需要配置<context:component-scan>

<context:component-scan> 标记告诉Spring扫描代码,从而找到使用@Component,@Controller,@Repository和@Service注解的注入bean,以及使用@Autowired,@Inject,@Resource注解的bean。在该注解中,可以使用逗号,分号或者空格作为分割符来定义多个包,此外,该标记支持组件扫面的包含和排除,从而实现更细粒度的控制。例如:

<context:component-scan
     base-package="com.apress.prospring5.ch3.annotation">
    <context:exclude-filter type="assignable"
        expression="com.example.NotAService">
</context:component-scan>

上面所示的表示Spring扫描制定的包,同时忽略那些在表达式中制定的类。除了排除过滤器以外,还可以使用包含过滤器,对于类型,可以使用注解,正则表达式,赋值,AspectJ或自定义类(使用实现了TypeFilter的过滤器类)作为过滤条件,表达式取决于所指定的类型;

type的类型有:

annotation:注解类型(较常使用)使用时expression应写注解的类路径,例如:org.springframework.stereotype.Controller

assignable:指定的类型
aspectj:按照Aspectj的表达式,基本上不会用到
regex:按照正则表达式
custom:自定义规则

更详细的用法详细用法

声明Spring组件

xml声明:在xml中使用?<bean>进行声明;

注解声明,必须使用适当的构造型注解(@Service,@Controller...)来注解bean,并且必须使用@Autowired注解方式或构造函数,以便告诉SpringIoc

使用java配置,配置类使用@configuration,并含有@Bean注解的方法,bean的名称与用于创建它的方法名称相同

可以使用配置类读取带注解的bean定义。这种情况下,因为bean的定义配置是bean类的一部分,所以类将不再需要任何@Bean注解的方法。但是为了能够在java类中查找bean定义,必须启用组件扫描。例如:

@ComponentScan(basePackage=“com.apress.prospring5.ch3.annotation”)
@Configuration
public class HelloWorldConfigutation{
...}

如果之前的代码是xml形式的,则可以使用@ImportResource从一个或者多个Xml文件中导入bean定义

@ImportResource(locations = {"classPath:spring/app-context-xml.xml"})
@Configuration
public class HelloWorldConfigutation{
...}

使用setter注入

要使用XML配置来配置setter注入,需要在<bean>标签下制定<property>标记(为每一个<property>标记注入一个依赖项)

例如:

<beans>
    <bean id="renderer"
     class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer">
        <property name="messageProvider" ref="provider">
    </bean>
    
    <bean id="provider"
        class="com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider"/>

</beans>

ref为属性引用一个bean

spring2.5以后,可以在xml配置文件中声明p名称空间,则可以声明注入,如以下代码:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="provider" class="com.apress.prospring5.ch2.decoupled.HelloWorldMessageProvider"/>

    <bean id="renderer" class="com.apress.prospring5.ch2.decoupled.StandardOutMessageRenderer"
        p:messageProvider-ref="provider"/>
</beans>

使用注解,则更加简单,只需要向setter方法添加一个@Autowired注解即可

由于在xml配置文件中声明了<context-component-scan>标记,因此在Spring的applicationContext初始化期间,Spring会发现这些@Autowired注解并根据需要注入依赖。

除了使用@Autowired,还可以使用@Resource(name="messageProvider")取得相同的结果。@Resource是Jsr-
250标准的注解之一,它定义了JSE和JEE平台使用的一组通用的JAVA注解。与@Autowired不同,@Resource注解
支持name参数,以获取更精细的DI要求。此外,Spring支持使用作为JSR-299(用于JAVA EE平台的上下文和依
赖注入)一部分的@inject注解。@Inject在行为上与Spring的@Autowired注解等价

使用构造函数注入

public ConfigurableMessageProvider(String message){...}

<bean id="provider"
    class="com.apress.prospring5.ch3.xml.ConfigurableMessageProvider">
    <constructor-arg value="I hope that someone gets my message in a bottle">
    另外一种写法:
    <constructor-arg type="int">
        <value>123</value>
    </constructor-arg>
</bean>

type用于参数个数相同时,区分参数类型

在Spring3.1后和可以使用c命名空间如下所示

<bean id="provider"
    class="com.apress.prospring5.ch3.xml.ConfigurableMessageProvider"
    c:message="I hope that someone gets my message in a bottle">
    
</bean>
也可以使用c:_0等表示参数索引
c命名空间没有在XSD文件中定义,并且只存在于Spring Core中;因此没有在shemaLocation属性中声明XSD。同p命名空间

要为构造函数注入使用注解,还需要在目标bean的构造函数方法中使用@Autowired注解,这是setter方法的替代方法;

对于xml注入,有多个参数个数相同的构造函数,需要在<constructor-arg type="int">是用type区别类型;对与注解注入,可直接在相应的构造函数上加上@Autowired注解,明确使用哪个构造函数;@Autowired注解只能应用于其中一个构造函数,如果用于多个,则会报错;

使用字段注入

。。不在阐述

通过静态工厂方法创建bean

调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单地调用静态方法,而不用关心创建对象的细节。

要声明通过静态方法创建Bean,需要在<bean>的calss属性里指定拥有该工厂方法的类,同时在factory-method属性中制定工厂方法的名称。最后,使用<constrctor-arg>元素为该方法传递方法参数。

通过调用实例工厂方法创建bean

实例工厂方法:将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时,只需要简单的调用该实例方法,而不用关心对象的创建细节。

要声明通过实例工厂方法创建bean

-在bean的factory-bean属性里制定拥有该工厂方法的bean

-在factory-method属性里制定该工厂方法的名称

-使用construtor-arg元素为工厂方法传递方法参数

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值