Spring注解

Spring

- 生命周期回调函数

@PostConstruct
@PreDestroy

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 测试
package test.service;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Service
public class LifeCycleService implements InitializingBean, DisposableBean {

    public LifeCycleService() {
        System.out.println("do constructor");
    }

    public void init() {
        System.out.println("default init");
    }

    /**
     * 构造方法执行完之后 执行,和afterPropertiesSet()起同等作用
     */
    @PostConstruct
    public void postConstructMethod() {
        System.out.println("do postConstructMethod");
    }

    @PreDestroy
    public void preDestroyMethod() {
        System.out.println("do preDestroyMethod");
    }

    /**
     * -- 执行完之后 执行
     * @throws Exception
     */
    public void afterPropertiesSet() throws Exception {
        System.out.println("do afterPropertiesSet");
    }

    public void destroy() throws Exception {
        System.out.println("do destroy");
    }
}

  • 如果 beanOne 初始化前需要用到 manager,那么可用 @Dependson 注解,那么manager就会在 beanOne前完成初始化
    在这里插入图片描述
@Lazy
@ComponentScan
  • * 对于这个注解的一个值,excludeFilters,可以传入一个注解,表示不扫描所有包含此注解得类
    
  • * 可以传入一个类,表示不扫描哪个类
    
  • * 可以传入一个包,表示不扫描哪个包
    

在这里插入图片描述
在这里插入图片描述

  • 不扫描注解
package test.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

@Configuration
@ComponentScan(basePackages = {"test"},
excludeFilters = @ComponentScan.Filter(classes = {Service.class}))
public class MyAppConfig {
}

package test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import test.config.MyAppConfig;

public class SpringTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(MyAppConfig.class);

    }
}

  • 不扫描类
package test.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;

@Configuration
@ComponentScan(basePackages = {"test"},
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, 
        pattern = "test.service.LifeCycleService"))
public class MyAppConfig {
}

  • 不扫描包
package test.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;

@Configuration
@ComponentScan(basePackages = {"test"},
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, 
        pattern = "test.service.*"))
public class MyAppConfig {
}

@Qualifier
  • @Autowired 默认按类型注入,当类型存在多个时使用 @Qualifier按名称指定注入
@Primary
  • 当类型存在多个时,给类指定@Primary作为首选注入即可
对于大型项目的扫描优化
  • 当类非常多时,可以添加下面的依赖,在编写程序时,spring会自动建立扫描索引,使得启动spring项目速度增快
    在这里插入图片描述
spring 注解的替代

在这里插入图片描述
在这里插入图片描述

profile
  • 适应不同环境
  • 示例:配置生产环境和开发环境的配置文件
    在这里插入图片描述
    在这里插入图片描述
  • 示例:配置生产环境和开发环境的Bean,灵活切换开发/生产环境
    在这里插入图片描述
  • 使profile生效
  • * 先不传入 配置类,不进行初始化容器(扫描),先配置  环境
    
  • * 再传入配置类
    
  • * 最后使用 refresh() 初始化容器,扫描
    

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值