Spring(二)SpringIOC相关的注解

1. 用于创建对象的注解

1. @Component   	应用: 工具类。
2. @Controller@Component , 主要用于修饰表现层的组件(组件 = 类)
3. @Service@Component ,主要用于修饰业务层的组件
4. @Repository@Component ,主要用于修饰数据访问层的组件

2. 用于依赖注入的注解

@Autowired
从容器中找对象,给属性赋值。根据类型、名称去容器中找。

@Autowired 实现依赖注入	(修饰字段)
1. 根据修饰的字段的类型(String),去容器中找该类型的对象注入;
2. 如果该类型的对象有多个,会根据字段的名称作为bean的id,去容器中查找注入。
3. 如果该类型的对象有多个,且根据字段名称没有找到,报错:NoUniqueBeanDefinitionException


@Autowired	(修饰方法)
1. 首先,根据参数类型,去容器中找对象注入;
2. 如果该类型对应的对象有多个,会根据参数名称去容器中查找注入。

@Qualifier
通常结合Autowired使用,可以指定按照名称去容器找对象注入。

@Qualifier("str1")
只会根据指定的名称id(str1), 去容器中查找对象注入。

@Value
给简单类型属性直接赋值/获取配置文件值@Value(“${key}”)

* @Value注解说明		(位置:字段)
* 1. 直接给简单类型的字段赋值
* 2. 获取配置文件值(纯注解使用)

@Resource
从容器中找对象,给属性赋值。 根据名称、类型去容器中查找

* @Resource		(位置:方法)
* 1. 根据名称或者类型注入
* 2. 属性
*    name  根据指定的名称注入。
*          一旦指定name,只会根据名称注入。

3. 对象范围与生命周期相关注解


 * @Scope	(位置:类上)
 * 指定对象单例、多例: singleton/prototype
 * @Lazy	(位置:类)
 * 1.延迟初始化;
 * 2.只对的单例有效。
 * 3.在第一次从容器中获取对象时候创建对象
 * @PostConstruct	(位置:方法)
 * 1.修饰的方法会在创建对象之后执行
 * 2.用于初始化资源
 * @PreDestroy	(位置:方法)
 * 1.在调用容器的close()方法时候执行
 * 2.释放资源

4. 取代配置文件的直接

@Configuration

	@Configuration
 * 1. 修饰的类就是注解配置类;
 * 2. 此注解取代:applicationContext.xml
 * 3. 此时创建容器,需要加载此注解修饰的类

@ComponentScan(basePackages = “com.it”)

@ComponentScan
 * 1. 开启注解扫描
 * 2. 取代:<context:component-scan base-package="com.it"/>
 * 3. basePackages 指定扫描的包

@Import(JdbcConfig.class)

@Import
 * 加载其他的配置管理类。

@PropertySource(“jdbc.properties”)

	@PropertySource
 * 1. 加载类路径下的配置文件
 * 2. 相当于:<context:property-placeholder location=""/>

@Bean(name = “dataSource”)

* 1. 创建连接池,加入ioc容器
* 2. @Bean
*    修饰在方法上,会自动把方法返回的结果加入ioc容器
*    也可以指定加入ioc容器中对象的名称:@Bean(name = "dataSource")



* @Bean 修饰带参数方法
*   1. 自动根据方法参数类型,去容器中找该类型的对象注入;
*   2. 如果类型有多个,会根据参数名称去ioc容器中找对应的对象注入。
*   3. @Qualifier 可以指定名称,按照指定的名称去容器中查找。

案例了解@Bean注解:JdbcConfig.java

package config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @PropertySource
 * 1. 加载类路径下的配置文件
 * 2. 相当于:<context:property-placeholder location=""/>
 */
@PropertySource("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;

    /**
     * 1. 创建连接池,加入ioc容器
     * 2. @Bean
     *    修饰在方法上,会自动把方法返回的结果加入ioc容器
     *    也可以指定加入ioc容器中对象的名称:@Bean(name = "dataSource")
     */
    @Bean(name = "dataSource1")
    public DataSource createDataSource(){
    
    
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(this.driver);
        ds.setUrl(this.url);
        ds.setUsername(this.username);
        ds.setPassword(this.password);
        return ds;
    }
    @Bean(name = "dataSource2")
    public DataSource createDataSource2(){
    
    
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(this.driver);
        ds.setUrl(this.url);
        ds.setUsername(this.username);
        ds.setPassword(this.password);
        return ds;
    }

    /**
     * 创建JdbcTemplate,加入ioc容器
     * @Bean 修饰带参数方法
     *   1. 自动根据方法参数类型,去容器中找该类型的对象注入;
     *   2. 如果类型有多个,会根据参数名称去ioc容器中找对应的对象注入。
     *   3. @Qualifier 可以指定名称,按照指定的名称去容器中查找。
     */
    @Bean
    public JdbcTemplate createJdbcTemplate(@Qualifier("dataSource1") DataSource dataSource){
    
    
        return new JdbcTemplate(dataSource);
    }
}










猜你喜欢

转载自blog.csdn.net/RookiexiaoMu_a/article/details/90208530