美文网首页
@Configuration与@Configurable的比较

@Configuration与@Configurable的比较

作者: dancer4code | 来源:发表于2020-09-23 18:32 被阅读0次

    @Configuration

    @Configuration就是以前spring的xml配置文件中的<beans> 的java实现,而@Bean就相当于<bean>
    @Configuration+@Bean就实现了spring以前配置文件中的类的配置加载。

    @Configurable

    主要是这个注解以前了解的不多。百度了一下,才知道其主要作用

    主要是适用于,某些自己new出来的对象,而new这个对象又必须依赖spring容器里的对象,才能完成一些工作。

    具体怎么运用。

    1.添加依赖

     <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.9.RELEASE</version>
      </dependency>
      <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>5.1.9.RELEASE</version>
     </dependency>
    

    2.具体的类

    Student

    package com.d4c.custombean.config;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Student {
    
        public void study(){
            System.out.println("I am a student,I must to study!");
        }
    }
    
    

    People

    package com.d4c.custombean.config;
    
    import org.springframework.beans.factory.annotation.Configurable;
    
    import javax.annotation.Resource;
    
    @Configurable
    public class People {
    
        @Resource
        private Student student;
    
        public void doSomething(){
            student.study();
        }
    
    }
    
    

    启动类加注解@SpringBootApplication+@EnableSpringConfigured

    package com.d4c.custombean;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableLoadTimeWeaving;
    import org.springframework.context.annotation.aspectj.EnableSpringConfigured;
    
    @ServletComponentScan
    @SpringBootApplication
    @EnableSpringConfigured
    @EnableLoadTimeWeaving
    public class CustomBeanApplication {
        public static void main(String[] args) {
            SpringApplication.run(CustomBeanApplication.class, args);
        }
    }
    
    

    测试类

    package com.d4c.custombean.config;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class PeopleTest {
    
        @Test
        public void doSomething() {
            People people = new People();
            people.doSomething();
        }
    }
    

    配置jvm参数

    不配置是会报错的

    -javaagent:E:\maven_repository\org\springframework\spring-instrument\5.1.9.RELEASE\spring-instrument-5.1.9.RELEASE.jar
    //直接指定spring-instrument.jar的maven本地仓库的地址就行
    

    如图:


    image.png

    OK ,可以了

    相关文章

      网友评论

          本文标题:@Configuration与@Configurable的比较

          本文链接:https://www.haomeiwen.com/subject/xxvoyktx.html