Test.java
package com.test.tianhang.junt_test;
import java.util.List;
import org.jtester.module.spring.annotations.SpringBeanByName;
import org.junit.Test;
import org.unitils.spring.annotation.SpringApplicationContext;
@SpringApplicationContext({"spring/spring-context.xml"})
public class mbtest {
@SpringBeanByName
private SwitchConfig swit;
@Test
public void test1(){
System.out.println(":1");
}
}
spring-context.xml 在src/test/resources/spring下
<?xml version="1.0" encoding="GBK"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<bean id="switchConfig" class="com.test.datastation.common.constants.SwitchConfig" init-method="init"/>
</beans>
我在输出那行设置了断点,但是swit没有注入成功,,为null。为什么呢?
------解决方案--------------------
你可以在你的SwitchConfi中增加一个空的构造,断点空构造方法,用于判断是否spring是否加载过,如果进入断点了,说明spring加载过你这个bean---》查看jtest中是否写的不正确;,如果没有进,那么说明spring没有加载--->查看资源文件是否写对,路径是否正确