idea 纯spring项目打jar包 @Resource抛 NullPointerException 手动getBean又报 XML schema 和 Offending resource:

 本人文章链接:

                idea 导出jar包 及 解决缺少jar包依赖问题

                idea中因spring配置文件找不到引发的 FileNotFoundException 和 NoSuchBeanDefinitionException

参考文章:

                 Maven生成可以直接运行的jar包的多种方式

                spring.handlers、spring.schemas、spring.tooling被覆盖的三种解决方式


我先把解决方法贴出来吧

    1、报错原因是:

                   由于在依赖中调用了spring的许多包,每个包都有自己的spring.schemas文件,会存在文件覆盖的情况,所以会有 xml schema错误

    2、解决方法:

            在pom.xml文件中加入

<build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <!--这里是运行的主类-->
                                    <mainClass>com.**.main.**</mainClass>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

3、OK


---------------------------------------------------------------------------------------------------------------------------------

过程记录下吧

1、环境:

    1-1、搭好的spring项目,可以用springTest跑通。

    1-2、打成jar包后,在Linux上运行报 nullPointerException , 当时感觉是spring的jar包或者spring的配置文件没有加入,就在 pom.xml 通过 <resource> 导入文件 

    1-3、导入文件后,通过 ClassPathXmlApplicationContext("/spring.xml").getBean("memMonitorService")去获取,又报出如下异常


    

2、个人分析

    2-1、nullPointerException

                未证实,欢迎指教,空指针感觉是 spring.xml 没有被容器加载进来,一般会在 web.xml 中会配置 <context-param> , 但我这里是纯spring的项目,没有用到 webmvc ,以至于spring.xml无人加载,所以注解失效

    2-2、xml schema 和   offending resource

                    这个可以参照上面的友情链接( spring.handlers、spring.schemas、spring.tooling被覆盖的三种解决方式


3、贴下 spring.xml 和 pom.xml 留作保存吧

加载配置文件

ApplicationContext context = new ClassPathXmlApplicationContext("/spring.xml");
monitorRun.memMonitorService = context.getBean("memMonitorService", IMemMonitorService.class)

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/data/mongo
       http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.**.monitor" />

    <mvc:annotation-driven >
        <mvc:message-converters register-defaults="false">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" index="0"/>
                <property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- 获取配置资源 -->
    <context:property-placeholder location="classpath:config.properties" />

    <mongo:mongo-client id="mongo" host="${mongo.host}" port="${mongo.port}" credentials="${mongo.user}:${mongo.pass}@${mongo.db}">
        <mongo:client-options connections-per-host="8"
                              threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
                              connect-timeout="${mongo.connectTimeout}"
                              max-wait-time="${mongo.maxWaitTime}"
                              socket-keep-alive="${mongo.socketKeepAlive}"
                              socket-timeout="${mongo.socketTimeout}"/>
    </mongo:mongo-client>

    <mongo:db-factory dbname="${mongo.db}" mongo-ref="mongo" />


    <!-- mongodb的模板 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
        <!--<constructor-arg type="org.springframework.data.mongodb.core.convert.MongoConverter" ref="mongoMoxydomainConverter"/>-->
    </bean>
    <!-- 构造key中.$符号的转换类 -->
    <!--<bean id="mongoMoxydomainConverter" class="com.**.utils.MappingMongoConverterUtil">
        <constructor-arg index="0" ref="mongoDbFactory" />
        <constructor-arg index="1">
            <bean class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
        </constructor-arg>
        <property name="mapKeyDotReplacement" value="DOT_PARSER"/>
        <property name="mapKeyDollarReplacement" value="DOLLAR_PARSER" />
    </bean>-->

    <bean id="envParameters" class="java.util.HashMap">
        <constructor-arg>
            <map>
                <entry key="mongo.host" value="${mongo.host}"></entry>
                <entry key="mongo.port" value="${mongo.port}"></entry>
                <entry key="mongo.db" value="${mongo.db}"></entry>
                <entry key="mongo.user" value="${mongo.user}"></entry>
                <entry key="mongo.pass" value="${mongo.pass}"></entry>
            </map>
        </constructor-arg>
    </bean>
</beans>

pom.xml
<?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">
    <parent>
        <artifactId>**</artifactId>
        <groupId>com.**</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>event-monitor</artifactId>
    <packaging>jar</packaging>

    <name>event-monitor Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <!--测试spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>xom</groupId>
            <artifactId>xom</artifactId>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>

        <!-- lombo注解 https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>


        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.codec</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

    </dependencies>

    <build>
        <finalName>event-monitor</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <!--这里是运行的主类-->
                                    <mainClass>com.**.main.**</mainClass>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值