spring获取数据源dataSource为空null

为国读书 2014-05-05 08:23:45
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" >

<mvc:annotation-driven />

<context:annotation-config/>

<context:component-scan base-package="com.memo.controller" />
<context:component-scan base-package="com.memo.dao" />

<mvc:resources mapping="/img/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/javascript/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/memo</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>

<bean id="dataBean" class="com.memo.dao.ConnectionFactory">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>



<!-- 视图解析器,在请求时模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/html/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>


</beans>


一个测试类:

private static final Logger log = Logger.getLogger(ConnectionFactory.class);

private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public DataSource getDataSource(){
return this.dataSource;
}

public void query()throws Exception{
log.info("dataSource:"+dataSource);
Connection conn = dataSource.getConnection();
System.out.println("conn:"+conn);
}
public static void main(String[] args) throws Exception{
new ConnectionFactory().query();
}

这个Test类中一直不能获取dataSource,dataSource为空,但是如果不用配置文件,直接在Test类中新建dataSource设置参数如:

dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/memo");
dataSource.setUsername("root");
dataSource.setPassword("123456");
Connection conn = dataSource.getConnection();

就可以获得dataSource,请各位帮帮我,这个问题是什么引起的,该怎么解决,谢谢!
jar包截图:
...全文
1540 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
为国读书 2014-05-06
  • 打赏
  • 举报
回复
引用 8 楼 xuefeng0707 的回复:
@Autowired似乎是可以省略applicationContext.xml中的bean的property配置,不用配置各个bean的属性了。 web.xml中的东西似乎是SpringMVC的东西,和创建bean没有关系。
谢谢帮助,结贴。
S117 2014-05-05
  • 打赏
  • 举报
回复
你得从spring容器中取得bean,这样DataSource 才会被注入呀!
xuefeng0707 2014-05-05
  • 打赏
  • 举报
回复
@Autowired似乎是可以省略applicationContext.xml中的bean的property配置,不用配置各个bean的属性了。 web.xml中的东西似乎是SpringMVC的东西,和创建bean没有关系。
为国读书 2014-05-05
  • 打赏
  • 举报
回复
引用 6 楼 xuefeng0707 的回复:
你上面的代码有注入的方式吗?并不是有了set和get方法就叫注入的。你写的代码和Spring就一点联系都没有,Spring就是用BeanFactory来管理bean的。你可以用:
beanFactory..getBean("dataBean", ConnectionFactory.class);
来获取ConnectionFactory的对象,这样它的dataSource属性就自动注入到了。
一定使用beanfactory才能获得实例吗,
@Autowired
	DataSource dataSource;
作用呢?楼主小白,多谢指导。 在web.xml的中配置:(注:上面的测试类,应该是通过web请求之后才会请求数据库链接)
<servlet>
		<servlet-name>dispatch</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>  
	        <param-name>contextConfigLocation</param-name>  
	        <param-value>classpath*:/config/applicationContext-*.xml</param-value>  
    	</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatch</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
xuefeng0707 2014-05-05
  • 打赏
  • 举报
回复
你上面的代码有注入的方式吗?并不是有了set和get方法就叫注入的。你写的代码和Spring就一点联系都没有,Spring就是用BeanFactory来管理bean的。你可以用:
beanFactory..getBean("dataBean", ConnectionFactory.class);
来获取ConnectionFactory的对象,这样它的dataSource属性就自动注入到了。
为国读书 2014-05-05
  • 打赏
  • 举报
回复
引用 1 楼 xuefeng0707 的回复:
既然用了Spring了,怎么没用Spring的bean方式来获取对象?
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"applicationContext.xml"));
		DataSource dataSource = beanFactory.getBean("dataSource", DataSource.class);
		System.out.println(dataSource);
其实刚开始配置文件里面是没有下面这段代码的:

<bean id="dataBean" class="com.memo.dao.ConnectionFactory">  
        <property name="dataSource">  
            <ref bean="dataSource"/>  
        </property>  
    </bean>  
测试类中是这样子:

@Autowired
	DataSource dataSource;  
	
	public void query()throws Exception{
		log.info("dataSource:"+dataSource);
		Connection conn = dataSource.getConnection();
		System.out.println("conn:"+conn);
	}
	public static void main(String[] args) throws Exception{
		new ConnectionFactory().query();
	}
使用自动注入的方式,单获得的datasource为null。
为国读书 2014-05-05
  • 打赏
  • 举报
回复
引用 2 楼 niit_java 的回复:
你的测试类没有加载spring,怎么能拿到spring 管理的bean?
刚接触spring,你说的具体怎么弄?
为国读书 2014-05-05
  • 打赏
  • 举报
回复
引用 1 楼 xuefeng0707 的回复:
既然用了Spring了,怎么没用Spring的bean方式来获取对象?
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"applicationContext.xml"));
		DataSource dataSource = beanFactory.getBean("dataSource", DataSource.class);
		System.out.println(dataSource);
谢谢,刚测试了下,用这个方法的确可以获得dataSource,看来也不是配置文件的问题了。如果纠结再问下,为什么使用上面注入的方式就不能获取,问题出现在哪呢?
paker_ma 2014-05-05
  • 打赏
  • 举报
回复
你的测试类没有加载spring,怎么能拿到spring 管理的bean?
xuefeng0707 2014-05-05
  • 打赏
  • 举报
回复
既然用了Spring了,怎么没用Spring的bean方式来获取对象?
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"applicationContext.xml"));
		DataSource dataSource = beanFactory.getBean("dataSource", DataSource.class);
		System.out.println(dataSource);

67,513

社区成员

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

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