Spring XML配置和注解配置

XML 文件的配置

<bean id="" class="" init-method="" destroy-method="" scope="">
    <property name="" value=""></property>
    <property name="" ref=""></property>
</bean>
  • id:被创建的对象的 id

  • class:要被创建的类对象的类全名

  • init-method:初始化方法

  • destroy-method:销毁方法

  • scope:对象的作用域

  • name:要被注入的数据的名称

  • value:注入数据的值

  • ref:当注入的数据是其他 bean 类型时,其他 bean 类型的名称

常用注解配置

用于创建对象的注解

注解的作用和 xml 配置文件中编写 <bean></bean> 标签的功能是一样的,即将当前类对象存入 Spring 容器中

  1. @Componcnt 注解:该注解用于把当前类对象存入 Spring 容器中

    该注解的属性:

    value: 指定 bean 的 id。假如我们没有指定 value 的值,默认为当前类名的首字母改小写,其余不变的名称。

  2. 其中 Spring 还为我们提供了三个作用和 @Component 一样的注解(使得我们的三层对象更加清晰):

    @Controller 一般用在表现层

    @Service 一般用在业务层

    @Repository 一般用在持久层

用于改变创建的对象的注入数据的注解

  1. @Autowritred 注解:该注解可以自动按照类型注入数据到 bean 中

    注入条件:

    • 如果 ioc 容器中用唯一的一个 bean 对象类型和要被注入的变量类型匹配
    • 如果 ioc 容器中没有对应的 bean 对象类型和要被注入的变量类型匹配,那么会抛出异常。
    • 如果 ioc 容器中有多个 bean 对象类型和要被注入的变量类型匹配,首先会根据 id 来匹配,如果 id 都一样,则会根据要被注入的变量的名称匹配,如果变量的名称都一样,那么就会抛出异常。
  2. @Qualifier 注解:该注解,在 @Autowritred 的基础之上,可以添加 value 属性值,指定注入的 bean id(需要和 @Autowritred 一起使用)

  3. @Resource 该注解和 @Qualifier 类似,可以指定注入的 bean id,但不需要和 @Autowritred 一起使用。@Resource 单独使用。

  4. @Value 注解:该注解用于注入基本数据类型和 String 数据类型

    该注解的属性:

    value:用于指定数据的值。可以是 Spring 中的 el 表达式(SpEL)

    SpEL 书写格式:${EL 表达式}

说明:

  1. @Autowritred @Qualifier @Resource 只能注入其他 bean 类型的数据,不能注入基本数据类型和 String 类型的数据。
  2. 集合类型的数据只能通过 XML 文件进行注入。

用于改变创建的对象的作用范围的注解

  • @Scope 注解:该注解用于指定 Bean 的作用范围

    该注解的属性:

    value:指定范围的取值。吃常用的取值有:singleton(单例的)、prototype(多例的)

    默认为单例的(对象只创建一次)

生命周期相关的注解

  1. @PreDestroy 销毁方法的注解
  2. @PostConstruct 初始化方法的注解

Spring 中的新注解

@Configuration 注解

用于指定当前类是一个 Spring 配置类,当创建容器时 会从该类上加载注解。 获取容器时需要使用AnnotationApplicationContext(有@Configuration 注解的类.class)。

/**
 * @Description: Spring 的配置类,相当于 bean.xml 文件
 */
@Configuration
public class SpringConfiguration { }

@ComponentScan 注解

用于指定 spring 在初始化容器时要扫描的包。作用和在 spring 的 xml 配置文件中的:

<context:component-scan base-package="pers.stringbug"/> 是一样的

  • @ComponentScan 注解的属性:

    1. basePackages:用于指定要扫描的包。
    2. value:用于指定要扫描的包。功能和 basePackages 属性功能一样。
@Configuration
@ComponentScan(basePackages="pers.stringbug")
public class SpringConfiguration { }

@Bean 注解

该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器。

属性:

name:给当前 @Bean 注解方法创建的对象指定一个名称 (即 bean 的 id)。

@Configuration
@ComponentScan(basePackages="pers.stringbug")
public class SpringConfiguration {

    /** 创建 QueryRunner 对象,并存入 Spring 容器中 */
    @Bean(name="runner")
    public QueryRunner createQueryRunner(DataSource dataSource) {
        return new QueryRunner(dataSource);
    }

    /** 创建数据源对象,并存入 spring 容器中 */
    @Bean(name="dataSource")
    public DataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/Study");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        return dataSource;
    }
}

@PropertySource 注解

用于加载 .properties 文件中的配置 。 例如我们配置数据源时, 可以把连接数据库的信息写到properties 配置文件中,就可以使用此注解指定 properties 配置文件的位置。

属性:

value[]:用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath:

@PropertySource(value={"classpath:jdbc.properties"})
public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean(name="dataSource")
    public DataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

@Import 注解

此时我们已经有了两个配置类(SpringConfigurationJdbcConfig),但是他们还没有关系。下面我们可以使用 @Import 注解,为这两个配置类建立关系链接。

用于导入其他配置类,在引入其他配置类时,可以不用再写@Configuration 注解。当然,写上也没问题。

属性:

value[]:用于指定其他配置类的字节码。

@Configuration
@ComponentScan(basePackages="pers.stringbug")
@PropertySource(value={"classpath:jdbc.properties"})
@Import(value={JdbcConfig.class})
public class SpringConfiguration {
    /**
     * 创建 QueryRunner 对象,并存入 Spring 容器中
     * 其中 @Qualifier(value="dataSource") 指定 bean 参数 id
     */
    @Bean(name="runner")
    public QueryRunner createQueryRunner(@Qualifier(value="dataSource") DataSource dataSource) {
        return new QueryRunner(dataSource);
    }
}

该例子类似于 @Bean 注解 中的 SpringConfiguration.java

参考文献

Good Good Write Bug, Day Day Up
原文地址:https://www.cnblogs.com/liyihua/p/14482488.html