Spring注解驱动开发--自动装配

一、使用@Autowired

  • 默认优先按照类型去容器中找对应的组件,需要使用时自动注入(applicationContext.getBean(BookService.class);)
    package org.hao.service;
    
    import org.hao.dao.BookDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class BookService {
          
          
        @Autowired
        private BookDao bookDao;
    
        public void print(){
          
          
            System.out.println(bookDao);
        }
    
        @Override
        public String toString() {
          
          
            return "BookService{" +
                    "bookDao=" + bookDao +
                    '}';
        }
    }
    
    在这里插入图片描述

二、@Resource和@Inject

  • 功能与@Autowired基本一样,都能实现自动注入
  • @Autowired是Spring定义的,@Resource和@Inject都是Java规范

三、Aware注入Spring底层组件

  • 自定义组件实现xxxAware:在创建对象时,会调用接口规定的方法注入相关组件
    package org.hao.bean;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.EmbeddedValueResolverAware;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringValueResolver;
    
    @Component
    public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
          
          
        private ApplicationContext applicationContext;
    
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          
          
            System.out.println("传入的IOC:" + applicationContext);
            this.applicationContext = applicationContext;
        }
    
        public void setBeanName(String s) {
          
          
            System.out.println("当前Bean的名字:" + s);
        }
    
        public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
          
          
            String resolveStringValue = stringValueResolver.resolveStringValue("你好${os.name}");
            System.out.println("解析的字符串:" + resolveStringValue);
        }
    }
    
    在这里插入图片描述

四、@Profile

  • Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能
  • @Profile环境搭建
    /**
    *	有三个数据源:测试、开发、生产
    */
    package org.hao.config;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.EmbeddedValueResolverAware;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.util.StringValueResolver;
    
    import javax.sql.DataSource;
    import java.beans.PropertyVetoException;
    
    @PropertySource("classpath:/dbconfig.properties")
    @Configuration
    public class MainConfigOfProfile implements EmbeddedValueResolverAware {
          
          
    
        @Value("${db.user}")
        private String user;
    
        private StringValueResolver valueResolver;
    
        private String driverClass;
    
        @Bean("testDataSource")
        public DataSource dataSourceTest(@Value("${db.password}") String pwd) throws PropertyVetoException {
          
          
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/chapter05");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
        @Bean("devDataSource")
        public DataSource dataSourceDev(@Value("${db.password}") String pwd) throws PropertyVetoException {
          
          
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/hao");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
        @Bean("prodDataSource")
        public DataSource dataSourceProd(@Value("${db.password}") String pwd) throws PropertyVetoException {
          
          
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/crashcourse");
            driverClass = valueResolver.resolveStringValue("${db.driverClass}");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
        public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
          
          
            this.valueResolver = stringValueResolver;
        }
    }
    
  • @Profile:指定组件在哪个环境下能被注册到容器中;若不指定,任何环境下都能注册这个组件
  • 加了环境标识的Bean,只有在激活时才能被注册。默认是default环境。
    在这里插入图片描述
  • 激活环境的方法:
  1. 使用命令行动态参数:在Edit Configurations...处加载-Dspring.profiles.active = test
  2. 代码方式激活
    在这里插入图片描述
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44863537/article/details/108783962