spring整合mongodb MongoRepository注入null

壹贰叁 发布于 2017/03/03 17:02
阅读 859
收藏 1

application.xml配置

<context:annotation-config />
    <!-- 扫描service 层方法 -->
    <context:component-scan base-package="com.data.service"/>

    <!-- 设置连接 数据库 用户 密码  -->
    <mongo:mongo-client id="mongoClient"  
host="127.0.0.1" port="27017" credentials="test:test@test">
        <mongo:client-options
                connections-per-host="10"
                min-connections-per-host="10"
                threads-allowed-to-block-for-connection-multiplier="5"
                connect-timeout="10000"
                max-wait-time="120000"
                socket-keep-alive="true"
                socket-timeout="10000"
                description="route mongodb database"
                max-connection-idle-time="1500"
                max-connection-life-time="0"
                heartbeat-socket-timeout="1000"
                heartbeat-connect-timeout="1500"
                min-heartbeat-frequency="5"
                heartbeat-frequency="10"

        />
    </mongo:mongo-client>

    <mongo:db-factory id="mongoDbFactory"
                      dbname="test"
                      mongo-ref="mongoClient"
                      />
    <mongo:repositories base-package="com.data.repository" />
    <mongo:mapping-converter base-package="com.data.bean" />

    <!-- 创建模板 -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>

repository  User是实体bean。。

public interface UserDao extends MongoRepository<User, String>{}

service

public class UserServiceImpl implements UserService{
@Autowired
private  UserDao dao;

public List<User> getPager() {
    System.out.println(dao);
    return dao.findAll();
}
}

 

 

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:application.xml"})
public class test {
    @Test
    public void ha(){
        UserService service = new UserServiceImpl();
        List<User> list = service.getPager();
    }
}

然后输出dao是null,大神求指教,谢谢了

 

加载中
0
一两代码
一两代码
测试用例的service没有从bean容器中获取
壹贰叁
壹贰叁
我配置了监听器,也做了扫描,目的就是为了在启动tomcat的时候application自动实列化,但是如果还需要手动的话,哪就不太好了。。。
返回顶部
顶部