spring学习4-spring管理对象之间的关联关系

简介: spring学习4-spring管理对象之间的关联关系

image.png

MesasageService 类
    package hello;
     import org.springframework.stereotype.Component;
     //注解的加入
     @Component
     public class MesasageService {
         public MesasageService() {
             super();
             System.out.println("MessageService...");
         }
         /**
          * 执行打印功能
          * @return 返回要打印的字符串
          */
         public String getMessage(){
             return "Hello World";
         }
     }MessagePrinter类
    package hello;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.stereotype.Component;
     //注解的加入
     @Component
     public class MessagePrinter {
         //无参构造方法的加入
         public MessagePrinter() {
             super();
             System.out.println("MessagePrinter...");
         }
         private MesasageService service;
         /*
         * 简历和MessageService的关系
         * */
         //设置service的值
         @Autowired
         //创建关联关系
         public void setService(MesasageService service){
             this.service =service;
         }
         public void printMessage(){
             System.out.println(this.service.getMessage());
         }
     }ApplicationSpring类
    package hello;
     import org.springframework.context.ApplicationContext;
     import org.springframework.context.annotation.AnnotationConfigApplicationContext;
     import org.springframework.context.annotation.ComponentScan;
     //加入扫描
     @ComponentScan
     public class ApplicationSpring {
         public static void main(String[] args){
             System.out.println("appliaction....");
             //初始化spring
             ApplicationContext context =new AnnotationConfigApplicationContext(ApplicationSpring.class);
             //获取messagePrinter对象
             MessagePrinter printer=context.getBean(MessagePrinter.class);
             //获取messafeService对象
             //MesasageService service=context.getBean(MesasageService.class);
             //System.out.println(printer);
            // System.out.println(service);
            // printer.setService(service);
             //打印消息
             printer.printMessage();
         }
     }运行结果
    appliaction....
     10月 27, 2019 6:45:12 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
     信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3d012ddd: startup date [Sun Oct 27 18:45:12 CST 2019]; root of context hierarchy
     MessageService...
     MessagePrinter...
     Hello World
相关文章
|
1月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
8月前
|
监控 Java 应用服务中间件
微服务——SpringBoot使用归纳——为什么学习Spring Boot
本文主要探讨为什么学习Spring Boot。从Spring官方定位来看,Spring Boot旨在快速启动和运行项目,简化配置与编码。其优点包括:1) 良好的基因,继承了Spring框架的优点;2) 简化编码,通过starter依赖减少手动配置;3) 简化配置,采用Java Config方式替代繁琐的XML配置;4) 简化部署,内嵌Tomcat支持一键式启动;5) 简化监控,提供运行期性能参数获取功能。此外,从未来发展趋势看,微服务架构逐渐成为主流,而Spring Boot作为官方推荐技术,与Spring Cloud配合使用,将成为未来发展的重要方向。
323 0
微服务——SpringBoot使用归纳——为什么学习Spring Boot
|
5月前
|
安全 Java 数据库
Spring Boot 框架深入学习示例教程详解
本教程深入讲解Spring Boot框架,先介绍其基础概念与优势,如自动配置、独立运行等。通过搭建项目、配置数据库等步骤展示技术方案,并结合RESTful API开发实例帮助学习。内容涵盖环境搭建、核心组件应用(Spring MVC、Spring Data JPA、Spring Security)及示例项目——在线书店系统,助你掌握Spring Boot开发全流程。代码资源可从[链接](https://pan.quark.cn/s/14fcf913bae6)获取。
836 2
|
7月前
|
Java Spring
Spring框架的学习与应用
总的来说,Spring框架是Java开发中的一把强大的工具。通过理解其核心概念,通过实践来学习和掌握,你可以充分利用Spring框架的强大功能,提高你的开发效率和代码质量。
192 20
|
11月前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
296 12
|
11月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
400 12
|
前端开发 Java 开发者
Spring生态学习路径与源码深度探讨
【11月更文挑战第13天】Spring框架作为Java企业级开发中的核心框架,其丰富的生态系统和强大的功能吸引了无数开发者的关注。学习Spring生态不仅仅是掌握Spring Framework本身,更需要深入理解其周边组件和工具,以及源码的底层实现逻辑。本文将从Spring生态的学习路径入手,详细探讨如何系统地学习Spring,并深入解析各个重点的底层实现逻辑。
286 9
|
前端开发 Java 数据库
SpringBoot学习
【10月更文挑战第7天】Spring学习
183 9
|
11月前
|
XML 安全 Java
Spring Boot中使用MapStruct进行对象映射
本文介绍如何在Spring Boot项目中使用MapStruct进行对象映射,探讨其性能高效、类型安全及易于集成等优势,并详细说明添加MapStruct依赖的步骤。
480 0
|
XML Java 数据格式
Spring学习
【10月更文挑战第6天】Spring学习
119 1

热门文章

最新文章