当前位置:编程学习 > JAVA >>

有关struts+ibatis+spring整合的问题

各位高手 :我写了个struts+ibatis+spring 的小程序 但是spring不能正常实例化  不知道怎么回事,这是spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 启用注解 --><!--
    <context:annotation-config />
    <context:component-scan base-package="com.*" />
   <!--将Data Source配置到ApplicationContext文件中  配置数据源-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
    </property>
<property name="url">
<value>${jdbc.url}</value>
    </property>
<property name="username">
<value>${jdbc.username}</value>
    </property>
<property name="password">
<value>${jdbc.password}</value>
    </property>
<property name="minIdle">
<value>1</value>
    </property>
    <property name="maxActive">
<value>25</value>
    </property>
    <property name="maxIdle">
<value>5</value>
    </property>
</bean>

<!--在Spring配置中注入3中的datasource数据源-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
    </property>
</bean>

<!-- 引入具体的sql语句配置文件(注意:sqlMapConfig.xml文件的路径)-->



<bean id="springContext" class="com.util.SpringDAOUtil"></bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
    <property name="sqlMapClient" ref="sqlMapClient" />
</bean>
    <bean id="dao" class="com.dao.UserDao">
<property name="sqlMapClientTemplate">
<ref bean="sqlMapClientTemplate"/>
</property>
</bean>
<bean id="user" class="com.bean.User"></bean>

<bean id="service" class="com.service.UserDaoService">
<property name="dao">
<ref bean="dao"/>
</property>
</bean>
<bean id="action" class="com.action.UserAction">
<property name="service">
<ref bean="service"/>
</property>
<property name="user">
<ref bean="user"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/> 
 
</beans>

这是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/conf/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet>
    <servlet-name>action_tmp</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>register.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  麻烦各位帮个忙  谢谢!! --------------------编程问答-------------------- 用
public static void main(String[] args) {
ApplicationContext ctx = 
new ClassPathXmlApplicationContext("applicationContext.xml");
UserDaoService service = (UserDaoService)ctx.getBean("service");

System.out.println(service);


}
可以获得对象
但是整合后:用set方法注入时service就是null --------------------编程问答-------------------- 你不是用的注解嘛  干嘛还要用set在注入呢? --------------------编程问答-------------------- 注解不作用,后来才换的
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,