7人参与 • 2025-03-08 • Java
package com.ruoyi.web.controller.employment.utils; import org.springframework.beans.beansexception; import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; import org.springframework.stereotype.component; @component public class springutil implements applicationcontextaware{ private static applicationcontext applicationcontext = null; @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { if(springutil.applicationcontext == null){ springutil.applicationcontext = applicationcontext; } } //获取applicationcontext public static applicationcontext getapplicationcontext() { return applicationcontext; } //通过name获取 bean. public static object getbean(string name){ return getapplicationcontext().getbean(name); } //通过class获取bean. public static <t> t getbean(class<t> clazz){ return getapplicationcontext().getbean(clazz); } //通过name,以及clazz返回指定的bean public static <t> t getbean(string name,class<t> clazz){ return getapplicationcontext().getbean(name, clazz); } }
package com.ruoyi; import com.ruoyi.web.controller.employment.provportcontroller1; import com.ruoyi.web.controller.employment.provportcontroller2; import com.ruoyi.web.controller.employment.utils.springutil; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration; import org.springframework.context.applicationcontext; /** * 启动程序 * * @author ruoyi */ @springbootapplication(exclude = { datasourceautoconfiguration.class }) public class ruoyiapplication { //注入service private static testservice testservice; //注入controller private static testcontroller testcontroller; public static void main(string[] args) throws interruptedexception { springapplication.run(ruoyiapplication.class, args); applicationcontext context = springutil.getapplicationcontext(); testservice= context.getbean(testservice.class); testcontroller= context.getbean(testcontroller.class); //调用service方法 testservice.getservice(); //调用controller方法 testcontroller.getcontroller(); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论