spring--打印hello--注解component--自动创建对象

1.创建

GroupId----项目目录(com.javaspring)

Artifactid---项目名称(spring01qiuckstart)

Version--版本默认

2.默认打开的pom.xml文件

编辑---编写spring核心项目依赖

复制代码
<?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.springjava</groupId>
    <artifactId>spring01quickstart</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> 核心 <version>4.3.12.RELEASE</version> 版本 </dependency> </dependencies> </project>
复制代码

---创建一个

MessageService-类
复制代码
package hello;

/**
 * d打印服务
 * 执行打印功能
 * @Date: 2019/7/12 14:51
 * @Version 1.0
 */
public class MessageService {
    public String getMessage(){
        return "Hello World";

    }
}
复制代码

---创建一个类MessagePrinter-用来调用 MessageService这个类,实现打印功能

---建立类和类的关联关系--一个类作为另一个类的成员变量

按alt和insert键--调用getset方法--快捷键

复制代码
package hello;

/**
 * @Author: 建立和MessageService的关联关系
 * @Date: 2019/7/12 15:07
 * @Version 1.0
 */
public class MessagePrinter {
    private MessageService service;

    public void setService(MessageService service) {
        this.service = service;
    }
    public void printMessage(){
        System.out.println(this.service.getMessage());
    }
}
复制代码

--创建主类--调用打印机

-写一个main方法--打印一个调试信息--创建消息打印机对象--创建消息服务对象--设置打印机对象的service属性--打印消息

复制代码
package hello;

/**
 * @Author:
 * @Date: 2019/7/12 15:19
 * @Version 1.0
 * -写一个main方法--
 * 打印一个调试信息--
 * 创建消息打印机对象--
 * 创建消息服务对象--
 * 设置打印机对象的service属性
 * --打印消息
 */
public class Applicaton {
    public static void main(String[] args) {
        System.out.println("application");
        MessagePrinter messagePrinter = new MessagePrinter();
        MessageService messageService = new MessageService();
        messagePrinter.setService(messageService);
        messagePrinter.printMessage();
    }
}
复制代码

---运行程序---在main方法上右键--run--

 ----在类上添加注解不再new

--此处可省略--自动生成--都有spring 来管理

----在MessageService中创建一个无参构造方法-方便打印输出--ctrl+o---选择object进行创建(或选择alt+inset--选overid)

----在printer 也创建一个无参构造

---创建另一个主类--applicationapring--

加入扫描注解--

@ComponentScan---扫描有commpontent注解的类---自动创建到spring容器当中

---将容器初始化---包含spring 类路径
 
复制代码
package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * @Author:
 * @Date: 2019/7/12 15:19
 * @Version 1.0
 *
 */
@ComponentScan
public class Applicatonspring {
    public static void main(String[] args) {
        System.out.println("application");
      /*  MessagePrinter messagePrinter = new MessagePrinter();
        MessageService messageService = new MessageService();
        messagePrinter.setService(messageService);
        messagePrinter.printMessage();*/
        ApplicationContext context = new AnnotationConfigApplicationContext(Applicatonspring.class);
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        MessageService service = context.getBean(MessageService.class);
        System.out.println(printer);
        System.out.println(service);
    }
}
复制代码




 

posted @   小白咚  阅读(827)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示