spring学习10-创建项目(自动装配)

 

首先创建项目

pom.xml的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
     
        <groupId>com.geyao</groupId>
        <artifactId>spring01geyao</artifactId>
        <version>1.0-SNAPSHOT</version>
     
        <dependencies>
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    </project>

配置log4j的配置文件

    ### 设置###
    log4j.rootLogger = INFO,stdout
     
    ### 输出信息到控制抬 ###
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
    log4j.category.org.springframework.beans.factory=DEBUG

CompactDisc类

    package soundSystem;
     
     
    import org.springframework.stereotype.Component;
     
    @Component
    public class CompactDisc {
     
        public CompactDisc() {
            super();
            System.out.println("compactdisc无参构造方法");
        }
        public void play(){
            System.out.println("正在播放音乐....");
        }
    }

CDplay类

    package soundSystem;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
     
    @Component
    public class CDPlayer {
        private CompactDisc cd;
     
        public CDPlayer() {
            super();
            System.out.println("CDPlayer无参构造方法");
        }
    @Autowired
        public CDPlayer(CompactDisc cd) {
            this.cd = cd;
            System.out.println("CDPlayer有参构造方法");
        }
     
        public void play(){
            cd.play();
        }
    }

App类

    package soundSystem;
     
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.ComponentScan;
     
    @ComponentScan
    public class App {
        public static void main(String[] args){
            ApplicationContext context=new AnnotationConfigApplicationContext(App.class);
            CDPlayer player=context.getBean(CDPlayer.class);
            player.play();
        }
    }

运行结果

    [INFO ] 2019-10-29 18:56:54,850 method:org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:583)
    Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@880ec60: startup date [Tue Oct 29 18:56:54 CST 2019]; root of context hierarchy
    [DEBUG] 2019-10-29 18:56:54,874 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:54,875 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:54,900 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:54,904 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:54,995 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:54,996 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:54,997 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:55,030 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:55,030 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:55,031 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:55,032 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:55,040 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:55,046 method:org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:730)
    Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@166fa74d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,app,CDPlayer,compactDisc]; root of factory hierarchy
    [DEBUG] 2019-10-29 18:56:55,047 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:55,047 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:55,050 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
    [DEBUG] 2019-10-29 18:56:55,050 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
    [DEBUG] 2019-10-29 18:56:55,051 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
    [DEBUG] 2019-10-29 18:56:55,059 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'org.springframework.context.event.internalEventListenerProcessor' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:55,063 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'org.springframework.context.event.internalEventListenerProcessor'
    [DEBUG] 2019-10-29 18:56:55,064 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
    [DEBUG] 2019-10-29 18:56:55,064 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
    [DEBUG] 2019-10-29 18:56:55,065 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'org.springframework.context.event.internalEventListenerFactory' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:55,070 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'org.springframework.context.event.internalEventListenerFactory'
    [DEBUG] 2019-10-29 18:56:55,072 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'app'
    [DEBUG] 2019-10-29 18:56:55,072 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'app'
    [DEBUG] 2019-10-29 18:56:55,073 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'app' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:55,076 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'app'
    [DEBUG] 2019-10-29 18:56:55,076 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'CDPlayer'
    [DEBUG] 2019-10-29 18:56:55,077 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'CDPlayer'
    [DEBUG] 2019-10-29 18:56:55,120 method:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    Creating shared instance of singleton bean 'compactDisc'
    [DEBUG] 2019-10-29 18:56:55,121 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:449)
    Creating instance of bean 'compactDisc'
    compactdisc无参构造方法
    [DEBUG] 2019-10-29 18:56:55,123 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'compactDisc' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:55,126 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'compactDisc'
    [DEBUG] 2019-10-29 18:56:55,127 method:org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:757)
    Autowiring by type from bean name 'CDPlayer' via constructor to bean named 'compactDisc'
    CDPlayer有参构造方法
    [DEBUG] 2019-10-29 18:56:55,129 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    Eagerly caching bean 'CDPlayer' to allow for resolving potential circular references
    [DEBUG] 2019-10-29 18:56:55,131 method:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:485)
    Finished creating instance of bean 'CDPlayer'
    [DEBUG] 2019-10-29 18:56:55,131 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'compactDisc'
    [DEBUG] 2019-10-29 18:56:55,132 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
    [DEBUG] 2019-10-29 18:56:55,194 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'lifecycleProcessor'
    [DEBUG] 2019-10-29 18:56:55,199 method:org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:251)
    Returning cached instance of singleton bean 'CDPlayer'
    正在播放音乐....
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,spring-boot-starter-data-redis是Spring Boot中用于自动装配Redis的starter包。它包含了自动装配所需的类和注解等。当我们在项目的pom.xml文件中引入spring-boot-starter-data-redis包时,Spring Boot会自动根据配置文件中的相关配置信息来完成Redis的自动装配。 具体来说,spring-boot-starter-data-redis使用了RedisAutoConfiguration类来实现自动装配。该类通过读取配置文件中的相关配置信息,例如主机名、端口号、密码等,来创建Redis连接工厂和RedisTemplate等实例。这些实例可以在应用程序中直接使用,而无需手动配置和初始化。 下面是一个示例代码,展示了如何使用spring-boot-starter-data-redis进行自动装配: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.redis.core.RedisTemplate; @SpringBootApplication public class RedisApplication { private final RedisTemplate<String, String> redisTemplate; public RedisApplication(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); } // 在需要使用Redis的地方,可以直接注入RedisTemplate实例,并进行操作 // 例如: // redisTemplate.opsForValue().set("key", "value"); // String value = redisTemplate.opsForValue().get("key"); } ``` 通过上述代码,我们可以看到,在Spring Boot应用程序中,我们只需要在需要使用Redis的地方注入RedisTemplate实例,就可以直接使用Redis的相关操作方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值