Spring配置了标签但是却不能自动注入

Try2Rich4U 2018-06-15 02:03:34
我在我的配置文件里面配置了Bean标签却没有能够自动注入,求解答一下

下面是applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!--配置注解扫描 -->
<context:annotation-config/>
<bean class="com.sc.jf.is.service.SpringContextUtil" lazy-init="false"/>

<!--对bean中的属性值进行外在化管理 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:common.properties</value>
<value>classpath:mongodb-config.properties</value>
</list>
</property>
</bean>

<!-- 自动扫描 -->
<context:component-scan base-package="com" />

<import resource="classpath:mongodb-config.xml"/>

</beans>


下面是引入的mongodb-config.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd">

<!-- 加载mongodb的属性配置文件 -->
<!-- <context:property-placeholder location="mongodb-config.properties" ignore-unresolvable="true" /> -->

<mongo:mongo-client id="mongo" host="${mongo.host}" port="${mongo.port}">
<mongo:client-options
connections-per-host="${mongo.connectionsPerHost}"
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="database" mongo-ref="mongo" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="${mongo.dbname}" />
</bean>


<!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 -->
<!--<mongo:mapping-converter base-package="jaky.web.*.dao" />-->
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<!--<mongo:repositories base-package="jaky.web.*" />-->
</beans>


对应的mongodb-config.properties文件:
# MongoDB
mongo.host=127.0.0.1
mongo.port=27017
#MongoDB操作的数据库名
mongo.dbname=testDB
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#连接超时时间
mongo.connectTimeout=1000
#等待时间
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
#Socket超时时间
mongo.socketTimeout=1500
mongo.slaveOk=true


用作测试的类:
package com.jf.cloud.monitor.test;

import java.util.Date;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

@Component
public class LogUtil {

@Autowired
private MongoTemplate mongoTemplate;

@Before
public void beforeSave(){
if(null==mongoTemplate){
System.out.println("没有自动注入到这个鸡儿MongoTemplate");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

if(context.containsBean("mongoTemplate")){
mongoTemplate = context.getBean("mongoTemplate",MongoTemplate.class);
}
}
}


@Test
public void save(){

System.out.println("进入了这个方法");


LogInfo log = new LogInfo("A1", "INFO", "a1",
new Date(), "getXxxxxx()", 100L,
new Date().getTime(), new Date().getTime()+100,
true, "Idontknownwhatid");


mongoTemplate.save(log,"test");
}

}


然后我是在我的测试类里面获取这个mongoTemplate,但是没有获取到

大神们看下我是哪一块出错了

最后是异常信息: ...太长了放不下,放个精简版的
没有自动注入到这个鸡儿MongoTemplate
log4j:ERROR Could not find value for key log4j.appender.ERROR
log4j:ERROR Could not instantiate appender named "ERROR".
[2018-06-15 13:38:06 603]-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7cdbc5d3: startup date [Fri Jun 15 13:38:06 CST 2018]; root of context hierarchy
[2018-06-15 13:38:06 646]-Loading XML bean definitions from class path resource [applicationContext.xml]
[2018-06-15 13:38:07 455]-Overriding bean definition for bean 'myRealm' with a different definition: replacing [Generic bean: class [com.jf.cloud.security.MyRealm]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [E:\workspace\监控统计\build\classes\com\jf\cloud\security\MyRealm.class]] with [Generic bean: class [com.jf.cloud.security.MyRealm]; scope=; abstract=false; lazyInit=true; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [applicationContext.xml]]
[2018-06-15 13:38:07 463]-Loading XML bean definitions from class path resource [mongodb-config.xml]
[2018-06-15 13:38:07 705]-Loading properties file from class path resource [common.properties]
[2018-06-15 13:38:07 706]-Loading properties file from class path resource [mongodb-config.properties]
[2018-06-15 13:38:07 942]-Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.beans.factory.config.MethodInvokingFactoryBean#0' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'shiroFilter' while setting bean property 'targetObject'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanInitializationException: SecurityManager property must be set.
[2018-06-15 13:38:09 466]-Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=32}
[2018-06-15 13:38:09 512]-Opened connection [connectionId{localValue:1, serverValue:47}] to 127.0.0.1:27017
[2018-06-15 13:38:09 515]-Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 5]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2097995}
进入了这个方法
[2018-06-15 13:38:09 782]-Opened connection [connectionId{localValue:2, serverValue:48}] to 127.0.0.1:27017

...全文
1124 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Try2Rich4U 2018-06-15
  • 打赏
  • 举报
回复
解决了,原因是没有任何鸡儿问题 只是我傻夫夫的没有吓到controller或者service等等里面,而是用了junit做测试
masteryourself 2018-06-15
  • 打赏
  • 举报
回复

   @Autowired
    private MongoTemplate mongoTemplate;
     
    @Before
    public void beforeSave(){
        if(null==mongoTemplate){
            System.out.println("没有自动注入到这个鸡儿MongoTemplate");
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 
            if(context.containsBean("mongoTemplate")){
                mongoTemplate = context.getBean("mongoTemplate",MongoTemplate.class);
            }
        }
    }
楼主先把这个注解去掉Autowired,启动后再看下,看你的错误 是shiro报的错,你可以先把shiro的配置注释一下,然后去掉那个注解,再测一下
stacksoverflow 2018-06-15
  • 打赏
  • 举报
回复
SecurityManager property must be set. 参考(借用别人的回答) https://bbs.csdn.net/topics/392011980 https://blog.csdn.net/wlwlwlwl015/article/details/48518003

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧