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);
    }
}




原文地址:https://www.cnblogs.com/fdxjava/p/11176374.html