我xml文件是放在src目录下的
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
测试代码:
package com.imooc.conform;
import static org.junit.Assert.*;
import java.util.Date;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private static ApplicationContext context = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test() {
Date date = (Date) context.getBean("date");
System.out.println(date);
}
}
报错:
七月 08, 2016 9:21:15 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b065c63: startup date [Fri Jul 08 21:21:15 CST 2016]; root of context hierarchy
七月 08, 2016 9:21:15 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
七月 08, 2016 9:21:16 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25bbe1b6: defining beans [sessionFactory,date]; root of factory hierarchy
七月 08, 2016 9:21:16 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25bbe1b6: defining beans [sessionFactory,date]; root of factory hierarchy
为什么啊?求大神。。。。。。。
11 个解决方案
#1
没看出来哪报错了,你打印个容易辨认的字符串先
#2
xml文件没有加载成功
我试了往这句context = new ClassPathXmlApplicationContext("applicationContext.xml")后加输出语句 根本打印不出来
#3
额,我觉得这不是找没找到的问题,而是这个方法执行了没有的问题,你放在之前打印看看
#4
public class SpringTest {
private static ApplicationContext context = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test() {
Date date = (Date) context.getBean("date");
System.out.println(date);
}
}
改一下 用main 方法 调用一下 别用@Test
感觉你的报错信息不是 因为找不到
private static ApplicationContext context = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test() {
Date date = (Date) context.getBean("date");
System.out.println(date);
}
}
改一下 用main 方法 调用一下 别用@Test
感觉你的报错信息不是 因为找不到
#5
我这有spring的学习视频 我觉得讲的好 你要的话私信我
#6
你的配置文件没有问题,导入的包完全的话也没问题,估计是你配置的bean=“sessionFactroy”这个里面的property属性的value="classpath:hibernate.cfg.xml"需要读取一个hibernate配置文件,不配置好了,就没问题了,因为我测试可以一下你的demo,配置好hibernate文件就ok了,要不你把这个bean=“sessionFactroy”屏蔽掉也没问题
#7
哪有错误信息
#8
test类前面加注解
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleSpringTest {
.....
}
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleSpringTest {
.....
}
#9
推荐你用JavaConfig的形式来使用spring
#10
如果真的要用xml的话,也可以这么写
package com.javarticles.spring.annotations;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Configuration
@ComponentScan(basePackages = {
"com.javarticles.spring.annotations.packageA",
"com.javarticles.spring.annotations.packageB" })
public class SpringComponentScanAnnotationViaXMLExample {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
try {
System.out.println("BeanA: "
+ ctx.getBean("beanA"));
System.out.println("BeanB: "
+ ctx.getBean("beanB"));
System.out.println("Contains BeanC1?: "
+ ctx.containsBean("beanC31"));
System.out.println("Contains BeanD?: "
+ ctx.containsBean("beanD"));
System.out.println("Contains BeanE1?: "
+ ctx.containsBean("beanE1"));
} finally {
ctx.close();
}
}
}
#11
web.xml 里面配了没?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<description>Spring</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
#1
没看出来哪报错了,你打印个容易辨认的字符串先
#2
没看出来哪报错了,你打印个容易辨认的字符串先
xml文件没有加载成功
我试了往这句context = new ClassPathXmlApplicationContext("applicationContext.xml")后加输出语句 根本打印不出来
#3
没看出来哪报错了,你打印个容易辨认的字符串先
xml文件没有加载成功
我试了往这句context = new ClassPathXmlApplicationContext("applicationContext.xml")后加输出语句 根本打印不出来
额,我觉得这不是找没找到的问题,而是这个方法执行了没有的问题,你放在之前打印看看
#4
public class SpringTest {
private static ApplicationContext context = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test() {
Date date = (Date) context.getBean("date");
System.out.println(date);
}
}
改一下 用main 方法 调用一下 别用@Test
感觉你的报错信息不是 因为找不到
private static ApplicationContext context = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test() {
Date date = (Date) context.getBean("date");
System.out.println(date);
}
}
改一下 用main 方法 调用一下 别用@Test
感觉你的报错信息不是 因为找不到
#5
我这有spring的学习视频 我觉得讲的好 你要的话私信我
#6
你的配置文件没有问题,导入的包完全的话也没问题,估计是你配置的bean=“sessionFactroy”这个里面的property属性的value="classpath:hibernate.cfg.xml"需要读取一个hibernate配置文件,不配置好了,就没问题了,因为我测试可以一下你的demo,配置好hibernate文件就ok了,要不你把这个bean=“sessionFactroy”屏蔽掉也没问题
#7
哪有错误信息
#8
test类前面加注解
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleSpringTest {
.....
}
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleSpringTest {
.....
}
#9
推荐你用JavaConfig的形式来使用spring
#10
如果真的要用xml的话,也可以这么写
package com.javarticles.spring.annotations;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Configuration
@ComponentScan(basePackages = {
"com.javarticles.spring.annotations.packageA",
"com.javarticles.spring.annotations.packageB" })
public class SpringComponentScanAnnotationViaXMLExample {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
try {
System.out.println("BeanA: "
+ ctx.getBean("beanA"));
System.out.println("BeanB: "
+ ctx.getBean("beanB"));
System.out.println("Contains BeanC1?: "
+ ctx.containsBean("beanC31"));
System.out.println("Contains BeanD?: "
+ ctx.containsBean("beanD"));
System.out.println("Contains BeanE1?: "
+ ctx.containsBean("beanE1"));
} finally {
ctx.close();
}
}
}
#11
web.xml 里面配了没?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<description>Spring</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>