我是靠谱客的博主 粗心夏天,最近开发中收集的这篇文章主要介绍Spring基于java注解方式注册javabean关键代码:AnnotationConfigApplicationContextpom文件程序入口:ApplicationMessagePrinter服务接口:MessageService服务实现类:MessageServiceImplspring注册bean关键位置,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

pom文件

<?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.burns.spring.framework.test</groupId>
    <artifactId>spring-framework-test-20220516</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.5.13</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
    </dependencies>

</project>

程序入口:Application

package com.burns.spring.framework.test;

import com.burns.spring.framework.test.component.MessagePrinter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;

@ComponentScan
public class Application {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        printer.pringMessage();
    }
}

MessagePrinter

package com.burns.spring.framework.test.component;

import com.burns.spring.framework.test.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {
    final private MessageService messageService;

    @Autowired
    public MessagePrinter(MessageService messageService) {
        this.messageService = messageService;
    }

    public void pringMessage() {
        System.out.println(this.messageService.getMessage());
    }


}

服务接口:MessageService

package com.burns.spring.framework.test.service;

public interface MessageService {
    String getMessage();

}

服务实现类:MessageServiceImpl

package com.burns.spring.framework.test.service.impl;

import com.burns.spring.framework.test.service.MessageService;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements MessageService {
    @Override
    public String getMessage() {
        return "Hello World!";
    }
}

spring注册bean关键位置

 

最后

以上就是粗心夏天为你收集整理的Spring基于java注解方式注册javabean关键代码:AnnotationConfigApplicationContextpom文件程序入口:ApplicationMessagePrinter服务接口:MessageService服务实现类:MessageServiceImplspring注册bean关键位置的全部内容,希望文章能够帮你解决Spring基于java注解方式注册javabean关键代码:AnnotationConfigApplicationContextpom文件程序入口:ApplicationMessagePrinter服务接口:MessageService服务实现类:MessageServiceImplspring注册bean关键位置所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部