9. Spring 的 新注解

大概意思是说  原始注解不能完全替代Spring配置文件中的内容,比如以下是不能被替代的:

·非自定义的Bean的配置:<bean>

·加载properties文件的配置:<context:property-placeholder>

·组件扫描的配置:<context:component-scan>

·引入其他文件:<import>

所以 Spring新注解就来了:

注解

说明

   @Configuration

用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解

   @ComponentScan

用于指定 Spring 在初始化容器时要扫描的包。

作用和在 Spring 的 xml 配置文件中的

<context:component-scan base-package="com.itheima"/>一样

   @Bean

使用在方法上,标注将该方法的返回值存储到 Spring 容器中

   @PropertySource

用于加载.properties 文件中的配置

   @Import

用于导入其他配置类

解析总体用法:

首先要有一个Spring的配置类 然后在类中写入@Configuration 代表这是一个主配置类,

然后用@ComponentScan注解 配置 扫描目录【扫描Spring注解】

然后用 @Bean 注解 可以将返回值存在注解指定的Bean上

然后用@PropertySource 可以在Spring配置中 载入 properties文件

然后用@Import 导入其他配置 到 主要配置类中。

例如下面有一个数据库获取连接【Connction】 的实例:

s1.properties :

jdbc.Driver="com.mysql.jdbc.Driver"
jdbc.url="mysql://localhost:3306/jdbc"                          此代码是错误代码!!别拷贝!! 仅此示范 下次不犯!!! 
jdbc.username="root"
jdbc.password="root"

正是因为上满的 properties 文件写错了 所以!我! 找了一个小时的Bug!!! 我是**!!!! 我既然将双引号也带进去!!!!

正确的代码:

s1.properties:

jdbc.Driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jdbc
jdbc.username=root
jdbc.password=root

SpringConfiguration.java 【Java配置类】:

package com.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/*Spring配置类*/

//注解配置类
@Configuration

//注解配置扫描目录
@ComponentScan("com")   //因为只有一个com目录

/*然后我们直接导入其他配置类[配置properties文件] 参数是 类.class 字节码文件  【class参数】
//@Import({aaa.class","bbb.class})    //多个用逗号即可。*/
@Import({DataSourceConfiguration.class})


public class SpringConfiguration {
}

DataSourceConfiguration.java :

package com.config;
//DataSouce c3p0的 配置类

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;


import javax.sql.DataSource;    //DataSouce类是最原生的那个连接池 之间有很多关系 所以可以直接接到c3p0的ComboPooledDataSource类!!!
import java.beans.PropertyVetoException;

//先用注解导入properties文件 参数是文件名
@PropertySource("classpath:s1.properties")
public class DataSourceConfiguration {
    //一系列通过读入properties文件的数据进行注入

    @Value("${jdbc.Driver}")
    private String Driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    //这个注解使用在方法上,标注将该方法的返回值存储到 Spring 容器中
    @Bean(name = "DataSource")
    public DataSource GetDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(Driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }

}

Test.java 【测试类】

import com.config.SpringConfiguration;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class Demo {
    @Test
    public void test() throws SQLException {
        //因为用的是 Spring的配置类配置 所以呢 这里我们需要用 Annotation这个来直接加载配置类
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        //因为我们在DataSource配置类中将DataSource放入Spring中了 相当于一个id吧
        DataSource dataSource = (DataSource) app.getBean("DataSource");
        Connection conn = dataSource.getConnection();
        System.out.println(conn);   //如果打印出来没错 证明就用新注解配置完毕了!
    }
}

所以呢 他这里没用到 手动配置XML

本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/14928801.html

原文地址:https://www.cnblogs.com/bi-hu/p/14928801.html