会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132384个问题

package com.bjsxt;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyGameFrame extends Frame {
    //将背景图片与飞机图片定义为成员变量
    Image bg = GameUtil.getImage("images/bg.jpg");
    Image plane = GameUtil.getImage("image/plane.png");
    public void launchFrame(){ //初始化窗口
        this.setTitle("百战程序员_学生作品");//在游戏窗口打印标题
        this.setVisible(true);//窗口默认不可见,设为可见
        this.setSize(500,500);//窗口大小:宽度500,高度500
        this.setLocation(300,300);//窗口左上角顶点的坐标位置
        //增加关闭窗口监听,这样用户点击右上角关闭图标,可以关闭游戏程序
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//
            }
        });

    }

    @Override
    //paint方法作用是:画出整个窗口及内部内容。被系统自动调用。
    public void paint(Graphics g) {
        g.drawImage(bg,0,0,500,500,null);
        g.drawImage(plane,200,200,30,30,null);
       /* Color old = g.getColor();
        g.setColor(Color.blue);
        //g.getColor(new Color(0,0,255))
        //g就是一支画笔
       g.drawLine(100,50,400,400); //从坐标点(100,50)到(400,400)画出直线
       g.drawRect(100,50,400,400);  //画出矩形。矩形左上角顶点坐标(100,50),宽度300,高度300
        g.drawOval(100,50,400,400);  //画出椭圆。椭圆外切矩形为:左上角顶点(100,50),宽度300,高度300
       g.drawString("SXT",100,100);//字符串
        g.setColor(old);*/
    }

    public static void main(String[] args) {
        MyGameFrame frame = new MyGameFrame();
        frame.launchFrame();
    }
}

image.png

image.png老师我在out里面以及开始调用了images的照片为啥还是显示错误啊


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 7621楼

在运行“Solr新增功能”时,出现如下错误提示

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-17 21:56:11.983 ERROR 14004 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'searchController': Unsatisfied dependency expressed through field 'searchService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchServiceImpl': Injection of @Reference dependencies is failed; nested exception is java.lang.IllegalStateException: zookeeper not connected
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at com.ego.SearchApp.main(SearchApp.java:9) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchServiceImpl': Injection of @Reference dependencies is failed; nested exception is java.lang.IllegalStateException: zookeeper not connected
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:145) ~[spring-context-support-1.0.5.jar:na]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1427) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	... 19 common frames omitted
Caused by: java.lang.IllegalStateException: zookeeper not connected
	at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:83) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.createZookeeperClient(CuratorZookeeperTransporter.java:26) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter.connect(AbstractZookeeperTransporter.java:68) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfiguration.<init>(ZookeeperDynamicConfiguration.java:70) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfigurationFactory.createDynamicConfiguration(ZookeeperDynamicConfigurationFactory.java:37) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.common.config.configcenter.AbstractDynamicConfigurationFactory.lambda$getDynamicConfiguration$0(AbstractDynamicConfigurationFactory.java:39) ~[dubbo-2.7.5.jar:2.7.5]
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) ~[na:1.8.0_202]
	at org.apache.dubbo.common.config.configcenter.AbstractDynamicConfigurationFactory.getDynamicConfiguration(AbstractDynamicConfigurationFactory.java:39) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.common.config.configcenter.DynamicConfiguration.getDynamicConfiguration(DynamicConfiguration.java:223) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.bootstrap.DubboBootstrap.prepareEnvironment(DubboBootstrap.java:815) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.bootstrap.DubboBootstrap.startConfigCenter(DubboBootstrap.java:560) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.bootstrap.DubboBootstrap.useRegistryAsConfigCenterIfNecessary(DubboBootstrap.java:624) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.bootstrap.DubboBootstrap.initialize(DubboBootstrap.java:508) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.bootstrap.DubboBootstrap.init(DubboBootstrap.java:493) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:183) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:151) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.getOrCreateProxy(ReferenceAnnotationBeanPostProcessor.java:249) ~[dubbo-2.7.5.jar:2.7.5]
	at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:142) ~[dubbo-2.7.5.jar:2.7.5]
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.getInjectedObject(AbstractAnnotationBeanPostProcessor.java:358) ~[spring-context-support-1.0.5.jar:na]
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor$AnnotatedFieldElement.inject(AbstractAnnotationBeanPostProcessor.java:538) ~[spring-context-support-1.0.5.jar:na]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:141) ~[spring-context-support-1.0.5.jar:na]
	... 30 common frames omitted
Caused by: java.lang.IllegalStateException: zookeeper not connected
	at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:80) ~[dubbo-2.7.5.jar:2.7.5]
	... 52 common frames omitted

2021-01-17 21:56:11.985  INFO 14004 --- [bboShutdownHook] o.apache.dubbo.config.DubboShutdownHook  :  [DUBBO] Run shutdown hook now., dubbo version: 2.7.5, current host: 169.254.20.197
2021-01-17 21:56:11.986  INFO 14004 --- [bboShutdownHook] o.a.d.c.e.listener.LoggingEventListener  :  [DUBBO] Dubbo Service has been destroyed., dubbo version: 2.7.5, current host: 169.254.20.197

Process finished with exit code 1


请问老师:

出现该问题可能是什么原因,该如何解决?


我的SearchServiceImpl.java的代码为

package com.ego.search.service.impl;

import com.ego.dubbo.service.TbItemCatDubboService;
import com.ego.dubbo.service.TbItemDescDubboService;
import com.ego.dubbo.service.TbItemDubboService;
import com.ego.entity.TbItem;
import com.ego.entity.TbItemCat;
import com.ego.entity.TbItemDesc;
import com.ego.search.entity.SearchEntity;
import com.ego.search.service.SearchService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.HighlightOptions;
import org.springframework.data.solr.core.query.HighlightQuery;
import org.springframework.data.solr.core.query.SimpleHighlightQuery;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private SolrTemplate solrTemplate;

    @Reference
    private TbItemDubboService tbItemDubboService;

    @Reference
    private TbItemCatDubboService tbItemCatDubboService;

    @Reference
    private TbItemDescDubboService tbItemDescDubboService;

    @Override
    public Map<String, Object> search(String q, int page, int size) {
        //设置查询条件
        Criteria criteria = new Criteria("item_keywords");
        criteria.is(q);
        HighlightQuery query = new SimpleHighlightQuery(criteria);

        //设置排序规则
        query.addSort(Sort.by(Sort.Direction.DESC, "_version_")); //_version_相当于一个时间戳字段

        //分页设置
        query.setOffset((long) size * (page - 1));
        query.setRows(size);

        //设置查询结果高亮
        HighlightOptions options = new HighlightOptions();
        options.setSimplePrefix("<span style='color:red'>");
        options.setSimplePostfix("</span>");
        options.addField("item_title item_sell_point"); //指定查询的字段
        query.setHighlightOptions(options);

        //执行查询
        HighlightPage<SearchEntity> results = solrTemplate.queryForHighlightPage("ego", query, SearchEntity.class);//ego是Solr的Core,相当于库。

        /**
         * 查询结果主要包括:
         * responseHeader
         * response
         *     numFound
         *     start
         *     docs ...
         *     highlighting ...
         */

        //取出高亮结果数据
        List<HighlightEntry<SearchEntity>> highlighted = results.getHighlighted();

        //通过一个集合存放所有查询到的数据,对应页面中的${itemList}。
        ArrayList<SearchEntity> list = new ArrayList<>();

        for (HighlightEntry<SearchEntity> he:highlighted){
            //非高亮数据:docs
            SearchEntity entity = he.getEntity();

            //将solr中的image转换为images
            String image = entity.getImage();
            entity.setImages(image!=null&&!image.equals("")?image.split(","):new String[1]);

            //高亮数据:highlighting
            List<HighlightEntry.Highlight> highlights = he.getHighlights();
            for (HighlightEntry.Highlight highlight:highlights){
                //包含查询字段名的数据就是高亮数据
                if ("item_title".equals(highlight.getField().getName())){
                    //高亮数据替换非高亮数据
                    entity.setTitle(highlight.getSnipplets().get(0));
                }
            }

            //收集所有查询到的高亮数据
            list.add(entity);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("itemList",list);
        map.put("query",q);
        map.put("totalPages",results.getTotalPages());
        return map;
    }

    /**
     * Solr批量新增
     * @param ids 新增商品的若干id
     * @return
     */
    @Override
    public int insert(long[] ids) {
        List<SearchEntity> list=new ArrayList<>();
        for (long id: ids){
            SearchEntity searchEntity = new SearchEntity();
            TbItem tbItem = tbItemDubboService.selectById(id);
            searchEntity.setImage(tbItem.getImage());
            searchEntity.setTitle(tbItem.getTitle());
            searchEntity.setId(id);
            searchEntity.setPrice(tbItem.getPrice());
            searchEntity.setSellPoint(tbItem.getSellPoint());
            TbItemCat tbItemCat = tbItemCatDubboService.selectById(tbItem.getCid());
            searchEntity.setCatName(tbItemCat.getName());
            TbItemDesc tbItemDesc = tbItemDescDubboService.selectById(id);
            searchEntity.setDesc(tbItemDesc.getItemDesc());
            list.add(searchEntity);
        }
        UpdateResponse response = solrTemplate.saveBeans("ego", list);
        solrTemplate.commit("ego"); //须提交事务
        if (response.getStatus()==0){ //Solr新增成功
            return 1;
        }
        return 0;
    }
}


根据错误提示已经做过的排查工作:

  1.  发现“private SolrTemplate solrTemplate;”语句在IDEA中有编译错误。

image.png

将SolrTemplate改为父接口SolrOperations,启动SearchApp启动类仍出现前述错误提示。


2. 检查zookeeper发现相应的consumer和provider注册正常

image.png


3. 先启动其他服务最后再启动SearchApp出现前述错误提示。

image.png


4. 经分析,觉得报错是由于SearchServiceImpl类中@Reference注解注入失败所致。于是注释所有@Reference注解,并启动SearchApp主类发现可以正常启动。

image.png


现在,不知道为什么@Reference注入会失败,烦请老师解答。

JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:使用Solr实现数据搜索 7622楼
WEB前端全系列/第二十阶段:Git版本控制器/版本控制器Git与SVN 7626楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 7627楼
JAVA 全系列/第八阶段:Linux入门到实战/Maven 7628楼
Python 全系列/第一阶段:Python入门/面向对象 7630楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 7632楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 7633楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 7634楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 7635楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637