spring的 @ConditionalOnClass 这种配置是可以提高效率吗?直接从字典表拿不好吗?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring的 @ConditionalOnClass 这种配置是可以提高效率吗?直接从字典表拿不好吗?相关的知识,希望对你有一定的参考价值。

spring的 @ConditionalOnClass 这种配置是可以提高效率吗?直接从字典表拿不好吗?

我发现spring在读取配置也有很多 注解方法,这种是可以显著提高效率吗?

下面这几个注解怎么灵活运用???

@Conditional(TestCondition.class)

这句代码可以标注在类上面,表示该类下面的所有@Bean都会启用配置,也可以标注在方法上面,只是对该方法启用配置。

@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)

参考技术A 这跟效率有什么关系呢。。这是表示按条件注入配置Bean
有了解springboot的autoconfigure包嘛? 为什么能开箱即用呢,springboot已经做好了这些配置类,你依赖相关的底层组件就能自动被Spring容器管理使用了,那你用redis时依赖个spring-boot-starter-data-redis就能直接使用redis的缓存了,为什么啊,因为springboot已经写好了RedisAutoConfiguraion,还有Properties,通过conditional看redis相关的类有,就会自动注入配置了,那你要不使用redis它还给你注入不就报错了,根本找不到redis的类怎么注入

这些条件控制就是提供了多个实现的可能,比如你们用了缓存功能,单机时使用本地缓存,集群时使用分布式缓存,那你就可以用这种控制来选择使用那套配置,是单机的配置还是集群的配置

Spring--Spring入门

Spring的概述-什么是Spring

  • ·Spring 是一个开源框架
  • ·Spring 为简化企业级应用开发而生.使用Spring可以使简单的
  • JavaBean 实现以前只有EJB才能实现的功能
  • ·Spring 是JavaSE/EE的一站式框架
  • ·方便解耦,简化开发
  • -Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
  • ·AOP编程的支持
  • -Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
  • ·声明式事务的支持
  • -只需要通过配置就可以完成对事务的管理,而无需手动编程

Spring的概述-Spring的优点

  • ·方便程序的测试
  • -Spring对Junit4支持,可以通过注解方便的测试Spring程序
  • ·方便集成各种优秀框架
  • -Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis等)的直接支持
  • ·降低JavaEEAPI的使用难度
  • -Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

Spring的概述-Spring的模块

 

Spring的Ioc的底层实现

 Spring的Ioc的入门案例

  • 使用idea创建新的maven项目
  • 在pom.xml中添加依赖
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.7.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.7.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
      <version>4.3.7.RELEASE</version>
  </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
  <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>
</dependencies>
  • 建包

 

UserService

public interface UserService {

    public void sayHello();
}

  UserServiceImpl

public class UserServiceImpl implements  UserService{
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //添加属性
    private String name;
    @Override
    public void sayHello() {
        System.out.print("hello spring" + name);
    }
}

  SpringDemo1

public class SpringDemo1 {
    @Test
    /**
     * 传统方式开发
     */
    public void demo1(){
        //UserService userService = new UserServiceImpl();
        UserServiceImpl userService = new UserServiceImpl();
        //设置属性 传统方法要改代码 就不好了
        userService.setName("张三");
        userService.sayHello();
    }

    @Test
    /**
     * 使用Spring 的方式
     */
    public void demo2(){
        //创建Spring的工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过工厂获得类
        UserService userService = (UserService) applicationContext.getBean("userService");

        userService.sayHello();
    }
}

   applicationContext.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- UserService的创建权交给了Spring-->
    <bean id="userService" class="com.windbag.ioc.demo1.UserServiceImpl">

        <!-- 设置属性-->
        <property name="name" value="李四"></property>
    </bean>

</beans>
  • 测试

Spring IOC的快速入门案例

  • ·IOC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spring框架管理
  • ·简单说,就是创建UserService对象控制权被反转到了Spring框架
  • ·DI Dependency Injection 依赖注入的概念,就是在Spring创建这个对象的过程中,将这个对象所依赖的属性注入进去。(name)

 

以上是关于spring的 @ConditionalOnClass 这种配置是可以提高效率吗?直接从字典表拿不好吗?的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记——Spring简介;Spring搭建步骤;Spring的特性;Spring中getBean三种方式;Spring中的标签

Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC

Spring--Spring入门

Spring:Spring介绍

Spring浅析Spring框架的搭建

Spring DAO vs Spring ORM vs Spring JDBC