spring 注入失败

ding82727 2015-09-23 11:46:09
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/profiles/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


<display-name>CloudServpage</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

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: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">
<!-- Definition of View Resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/web/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<!-- 定义映射 -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="login.do">loginAction</prop>
<prop key="userList.do">loginAction</prop>
<prop key="addUser.do">loginAction</prop>
<prop key="updateUser.do">loginAction</prop>

<prop key="roleList.do">roleAction</prop>
<prop key="addRole.do">roleAction</prop>

<prop key="houseList.do">houseAction</prop>
<prop key="addHouse.do">houseAction</prop>
<prop key="updateHouse.do">houseAction</prop>
</props>
</property>

</bean>

<!-- 定义控制器 -->
<bean id="loginAction" class="com.cloud.action.LoginAction"></bean>
<bean id="roleAction" class="com.cloud.action.RoleAction"></bean>
<bean id="houseAction" class="com.cloud.action.HouseAction"></bean>

</beans>

applicationContext-ibatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring 整和ibatis spring的配置 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
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.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

<!-- jndi数据源配置 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/CloudServpage</value>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="create*" rollback-for="Exception" />
<tx:method name="delete*" rollback-for="Exception" />
<tx:method name="save*" rollback-for="Exception" />
<tx:method name="insert*" rollback-for="Exception" />
<tx:method name="update*" rollback-for="Exception" />
<tx:method name="del*" rollback-for="Exception" />
<tx:method name="cost*" rollback-for="Exception" />
<tx:method name="add*" rollback-for="Exception"/>
<tx:method name="modify*" rollback-for="Exception"/>
<tx:method name="remove*" rollback-for="Exception"/>
<tx:method name="*" read-only="true" rollback-for="Exception" />
</tx:attributes>
</tx:advice>

<!-- 事物配置 -->
<aop:config>
<aop:pointcut id="operation" expression="execution(* com.cloud.business.*.*(..))" />
<aop:advisor pointcut-ref="operation" advice-ref="txAdvice" />
</aop:config>



<!-- 配置iBatis的sqlMapClient 下面的 springSqlMapConfig.xml 是指定用来与spring整合的ibatis的配置文件
用来与spring整合的ibatis的配置文件的配置内容与单独使用ibatis时的配置 有所不同。详见具体的ibatis配置文件 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:springSqlMapConfig.xml</value>
</property>
</bean>

<!-- 根据sqlMapClien获取一个SqlMapClient模版-->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>

<!-- 添加ibatis支持 抽象类 -->
<bean id="sqlMapClientDaoSupport"
class="org.springframework.orm.ibatis.support.SqlMapClientDaoSupport"
abstract="true">
<property name="dataSource" ref="dataSource"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property>
<property name="sqlMapClientTemplate" ref="sqlMapClientTemplate"></property>
</bean>

</beans>

applicationContext-beans.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:jee="http://www.springframework.org/schema/jee"
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.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- dao -->
<bean id="housedao" class="com.cloud.dao.SysHouseDAOImpl" parent="sqlMapClientDaoSupport">
</bean>

<!-- business -->
<bean id="housebusiness" class="com.cloud.business.HouseBusinessImpl">
<property name="housedao" ref="housedao" />
</bean>

<!-- Action Definition -->
<bean id="loginAction" class="com.cloud.action.LoginAction">
<property name="housebusiness" ref="housebusiness" />
</bean>


</beans>

以上是我的spring配置文件
下面是action 使用 对象时,发现对象没有注入,HouseBusiness为null。
public class LoginAction extends MultiActionController {

private HouseBusiness housebusiness;

public HouseBusiness getHousebusiness() {
return housebusiness;
}

public void setHousebusiness(HouseBusiness housebusiness) {
this.housebusiness = housebusiness;
}

ibatis也注入失败
严重: Servlet.service() for servlet dispatcherServlet threw exception
java.lang.IllegalArgumentException: No SqlMapClient specified
...全文
312 19 点赞 打赏 收藏 举报
写回复
19 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ujquan 2015-11-15
看啊看,不懂就学学吧。
  • 打赏
  • 举报
回复
wonderwall625 2015-11-15
<!-- 定义控制器 --> <bean id="loginAction" class="com.cloud.action.LoginAction"></bean> <!-- Action Definition --> <bean id="loginAction" class="com.cloud.action.LoginAction"> <property name="housebusiness" ref="housebusiness" /> </bean> 冲突了吧?
  • 打赏
  • 举报
回复
jumpheightway 2015-09-29
我很想知道你的文件之间有没有import
  • 打赏
  • 举报
回复
MCJ_King 2015-09-25
你可以直接new一个啊 试一试 就能看出哪里的问题来了
  • 打赏
  • 举报
回复
ding82727 2015-09-25
不是这个问题哦,问题是spring框架的依赖注入机制没起作用
  • 打赏
  • 举报
回复
011100 2015-09-24
我之前遇到一次,类似,用的注解。最后原因是因为我在其他地方手动new了一个该dao的实例,所以,@Autowired便不起作用了,具体原因我也不知道,但是我发现后,将另外那个类里手动new的删掉,注入不了的问题就解决了
  • 打赏
  • 举报
回复
ding82727 2015-09-24
54行的java代码就是上面30行的那个
SysHouse house = housebusiness.findHoseById(1);
使用的时候housebusiness是null
  • 打赏
  • 举报
回复
ding82727 2015-09-24
下面是54行的java代码
public class LoginAction extends MultiActionController {

	private HouseBusiness housebusiness;
		
	public HouseBusiness getHousebusiness() {
		return housebusiness;
	}

	public void setHousebusiness(HouseBusiness housebusiness) {
		this.housebusiness = housebusiness;
	}

	/**
	 * 登陆
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		ModelAndView mv = new ModelAndView("main");
		String userName = request.getParameter("username");
		String userPwd = request.getParameter("password");
		
		
//		housebusiness = new HouseBusinessImpl();
		SysHouse house = housebusiness.findHoseById(1);
		System.out.println(house.getOwnerName());
  • 打赏
  • 举报
回复
从小就很呆 2015-09-24
已经很明显了,你的dao集成了sqlMapClientDaoSupport,但是这个类,你没有给他定义bean,所以它找不到了
  • 打赏
  • 举报
回复
ding82727 2015-09-24
没有异常信息啊,亲。就是spring的依赖注入没有成功对java对象实例化,项目可以运行,但是使用对象的时候发现空指针 下面是调用时候的异常 2015-9-24 9:33:19 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet dispatcherServlet threw exception java.lang.NullPointerException at com.cloud.action.LoginAction.login(LoginAction.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:471) at org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:408) at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153) at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:563) at javax.servlet.http.HttpServlet.service(HttpServlet.java:643) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619)
  • 打赏
  • 举报
回复
Defonds 2015-09-23
肉眼挑错? 你具体描述下啥问题
  • 打赏
  • 举报
回复
ding82727 2015-09-23
自己顶一下,求人来帮忙看看我这是哪里配错了哇。。。。
  • 打赏
  • 举报
回复
dw_java08 2015-09-23
把后台报的信息都贴出来看看
  • 打赏
  • 举报
回复
风若飞 2015-09-23
你把抛出的异常发出来看下
  • 打赏
  • 举报
回复
ding82727 2015-09-23
配置在这里了 主要是spring注入失败,下面的dao对象没有实例化 <!-- dao --> <bean id="housedao" class="com.cloud.dao.SysHouseDAOImpl" parent="sqlMapClientDaoSupport"> </bean> <!-- business --> <bean id="housebusiness" class="com.cloud.business.HouseBusinessImpl"> <property name="housedao" ref="housedao" /> </bean> <!-- Action Definition --> <bean id="loginAction" class="com.cloud.action.LoginAction"> <property name="housebusiness" ref="housebusiness" /> </bean>
  • 打赏
  • 举报
回复
心随自在飞 2015-09-23
代码太多, 我貌似没看到 dao 的 bean 和 service 的 bean 配置在哪
  • 打赏
  • 举报
回复
ding82727 2015-09-23
spring注入ibatis 失败 java.lang.IllegalArgumentException: No SqlMapClient specified
  • 打赏
  • 举报
回复
ding82727 2015-09-23
private HouseBusiness housebusiness; public void setHousebusiness(HouseBusiness housebusiness) { this.housebusiness = housebusiness; } 这里,spring应该是给我吧这个HouseBusiness 对象实例化了,但是现在情况是没有,该对象还是null。
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-09-23 11:46
社区公告
暂无公告