可能有点抽象,先看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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ZuyingSoft"></property>
<property name="username" value="root"></property>
<property name="password" value="***"></property>
</bean>
<!--在使用Struts2时,由于在struts.xml中有配置userAction,所以在UserAction中,可以通过setDataSource取到dataSource-->
<bean id = "userAction" class = "com.action.UserAction">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!--由于Test类没有配置struts.xml,虽然我也把dataSource注入到了Test类中去了,setDataSource也有,但依然取不到dataSource的值,怎么办?-->
<bean id = "test" class = "com.test.Test">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
</beans>重点看注释部分,UserAction类继成了ActionSupport,当通过XXX.action访问UserAction中方法时,此时dataSource不为空,而Test类与struts没任何关系,就得不到dataSource的值了。说了这么多,我的问题是,有没办法,在Test类中,也能用到数据源dataSource呢?
并且,要求在applicationContext.xml设置,最好不要用hibernate。附:我现在用的方法是这样的:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource= (DataSource) context.getBean("dataSource");只是感觉这样很不好,我想直接在applicationContext.xml设置,在这里用get,set得到dataSource,怎么设置呢?水平有限,对spring也没什么研究,有高人懂不?欢迎指点。
<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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ZuyingSoft"></property>
<property name="username" value="root"></property>
<property name="password" value="***"></property>
</bean>
<!--在使用Struts2时,由于在struts.xml中有配置userAction,所以在UserAction中,可以通过setDataSource取到dataSource-->
<bean id = "userAction" class = "com.action.UserAction">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!--由于Test类没有配置struts.xml,虽然我也把dataSource注入到了Test类中去了,setDataSource也有,但依然取不到dataSource的值,怎么办?-->
<bean id = "test" class = "com.test.Test">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
</beans>重点看注释部分,UserAction类继成了ActionSupport,当通过XXX.action访问UserAction中方法时,此时dataSource不为空,而Test类与struts没任何关系,就得不到dataSource的值了。说了这么多,我的问题是,有没办法,在Test类中,也能用到数据源dataSource呢?
并且,要求在applicationContext.xml设置,最好不要用hibernate。附:我现在用的方法是这样的:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource= (DataSource) context.getBean("dataSource");只是感觉这样很不好,我想直接在applicationContext.xml设置,在这里用get,set得到dataSource,怎么设置呢?水平有限,对spring也没什么研究,有高人懂不?欢迎指点。
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
改成 <bean id = "test" class = "com.test.Test">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
呢?
是这样子的,UserAction与Test类中,都有如下的方法: private DataSource dataSource = null;
/**
* @return the dataSource
*/
public DataSource getDataSource()
{
return dataSource;
} /**
* @param dataSource the dataSource to set
*/
public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
}
这样说吧,此时UserAction中,可以得到dataSource值,即不为空。
而Test类中,dataSource值此为空,即setDataSource根本没把dataSource设置进来。也就是说,我也想在Test类中,也想要到dataSource值,怎么弄呢?(我那种方法除外)
并非很明显,此时在applicationContext.xml设置的如下内容显然无效:
<bean id = "test" class = "com.test.Test">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
ref 是全局的 local 是本文件的
如果注入的不是当前文件的bean 那就用<ref bean="dataSource"/>吧
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
不知道这样说你明白了没
不好意思,其实我本地就是用bean的了,写贴子时,拷错了。
这样还是得不到值的
那就不应该出问题的……不懂了,求高手
同时学习
你说的有理,确实是这样的。我就是想知道,除了直接在类Test中加载applicationContext.xml,有没其它什么方法的了?能不能不用加Test中加载,通过写什么配置文件之类的解决呢?回10楼是的,你可以理解为我new 一个Test类,然后,想在里面处理一些数据库相关的业务。
代码写是写了。
再次更正下贴子中的代码:local改为bean
所有通过spring ioc的对象都是由容器管理的
你只有从容器中获取对象,属性的属性才是注入过的
你直接new出来的,那肯定是null,这个对象不由spring容器管理,所以也不会注入
我的意思是,Test是一个独立的类,怎样才能跟spring扯上关系呢?失之成为spring容器中的一个对象。
更详细地我也帮不了你什么了,我不大会 Spring
可以用 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext((ServletContext) ActionContext
.getContext().get(ServletActionContext.SERVLET_CONTEXT));
DataSource d= (DataSource ) ctx.getBean("dataSource ");
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
Test test = (Test)context.getBean("test");
test.function();//这时就可以在test的function中直接使用dataSource了。
......
火龙果的思路很好,他想到了,用某一特殊的类接口ApplicationContextAware,实现ApplicationContextAware对象的注入,进而间接的来获取数据源。但本人水平确实有限,测试了几次,还是没能得到ApplicationContext对象,进而求出数据源。ApplicationContextAware接口有一个方法是这样的:
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.applicationContext=applicationContext;
}
所以,应该是这样方式可行的,只是可能我还有那里搞错了,先放着了,以后有空再研究了。25楼的那个方法似乎也不错,不用new对象,直接使用系统的静态类方法,但不知道为何,我本地好像有空指针现象,求不出的。具体那个静态类是空指针,还没去查,先不管了。其它的原因,9楼也说得很清楚的了,我不重复了。想来想去,最终还是先用自己的那个方法吧,先这样用的了,贴子明天再结吧。
试了下,你这种方法是可行的,其实也是跟我那个同类型的,跟25楼也同类型,只是25楼的出错了,呵
但你这个不用new 对象,感觉还是比用new 的要好点吧。