spring为什么注入不了对象

J2EE 码拜 10年前 (2015-04-04) 922次浏览 0个评论

 Spring配置
<!– sessionfactory配置 –>
     <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
      <property name=”configLocation”>
      <value>classpath:hibernate.cfg.xml</value>
      </property>
     </bean>
     
    <!– txManager配置 –>
     <bean id=”txManage” class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
      <property name=”sessionFactory” ref=”sessionFactory”></property>
     </bean>
     
     <!– 定义事务通知 –>
     <tx:advice id=”txAdvice” transaction-manager=”txManage”>
      <tx:attributes>
      <tx:method name=”*” propagation=”REQUIRED”/>
      </tx:attributes> 
     </tx:advice>
     
     <aop:config>
      <aop:pointcut expression=”execution(* dao..*.*(..))” id=”serverMethod”/>
      <aop:advisor advice-ref=”txAdvice” pointcut-ref=”serverMethod”/>
     </aop:config>
     
     
     
     <!– dao层 –>
     <bean id=”ClassesDaoImpl” class=”dao.impl.ClassesDaoImpl”>
      <property name=”sessionFactory” ref=”sessionFactory”></property>
     </bean>
     
     <bean id=”StudentDaoImpl” class=”dao.impl.StudentDaoImpl”>
      <property name=”sessionFactory” ref=”sessionFactory”></property>
     </bean>

action代码

package web.action;

import java.util.List;

import bean.Student;

import com.opensymphony.xwork2.ActionSupport;

import dao.StudentDao;

public class StuAction extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private StudentDao studentDao;
private List<Student> stulist;

public List<Student> getStulist() {
return stulist;
}

public void setStulist(List<Student> stulist) {
this.stulist = stulist;
}

public StudentDao getStudentDao() {
return studentDao;
}

public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}

public String getStus(){

try {
stulist = studentDao.getStu();
return SUCCESS;
} catch (Exception e) {
// TODO Auto-generated catch block

e.printStackTrace();
return INPUT;
}

}

}

package dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bean.Classes;
import dao.ClassesDao;

public class ClassesDaoImpl extends HibernateDaoSupport implements ClassesDao {

public boolean save(Classes classes) {
boolean flg = true;
try {
Session session =this.getSessionFactory().openSession();
session.save(classes);
} catch (HibernateException e) {

flg = false;
e.printStackTrace();
}
return flg;
}

@SuppressWarnings(“unchecked”)
public List<Classes> getClassAll() {
try {
Session session = this.getSessionFactory().openSession();
Query query = session.createQuery(“from Classes”);
return query.list();
} catch (HibernateException e) {

e.printStackTrace();
}
return null;
}

}

package dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bean.Student;
import dao.StudentDao;

public class StudentDaoImpl extends HibernateDaoSupport implements StudentDao{

public boolean addStu(Student student) {
boolean flg = true;
try {
Session session = this.getSessionFactory().openSession();
session.save(student);
} catch (HibernateException e) {
// TODO Auto-generated catch block
flg = false;
e.printStackTrace();
}
return flg;
}

public List<Student> getStu() {

try {
Session session = this.getSessionFactory().openSession();
Query query = session.createQuery(“from Student”);
return query.list();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

}

package dao;

import java.util.List;

import bean.Classes;

public interface ClassesDao {
/**
 * 添加班级
 * @param classes
 * @return
 */
public boolean save(Classes classes);
/**
 * 得到所有班级
 * @return
 */
public List<Classes> getClassAll();

}

package dao;

import java.util.List;

import bean.Student;

public interface StudentDao {
/**
 * 添加学生
 * @param student
 * @return
 */
public boolean addStu(Student student);

/**
 * 得到学生
 * @return
 */
public List<Student> getStu();
}

spring为什么注入不了对象
20分
 <bean id=”ClassesDaoImpl” class=”dao.impl.ClassesDaoImpl”>
      <property name=”sessionFactory” ref=”sessionFactory”></property>
     </bean>
     
     <bean id=”StudentDaoImpl” class=”dao.impl.StudentDaoImpl”>
      <property name=”sessionFactory” ref=”sessionFactory”></property>
     </bean>

id的值首字母都小写。

spring为什么注入不了对象
20分
引用 1 楼 fangmingshijie 的回复:

<bean id=”ClassesDaoImpl” class=”dao.impl.ClassesDaoImpl”>
      <property name=”sessionFactory” ref=”sessionFactory”></property>
     </bean>
     
     <bean id=”StudentDaoImpl” cl……

+++

spring为什么注入不了对象
2013-3-20 12:35:07 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: F:\MyEclipse\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin;D:\TomCat\bin
2013-3-20 12:35:08 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2013-3-20 12:35:08 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1131 ms
2013-3-20 12:35:08 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2013-3-20 12:35:08 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.35
2013-3-20 12:35:08 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor manager.xml
2013-3-20 12:35:08 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory docs
2013-3-20 12:35:09 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory ROOT
2013-3-20 12:35:09 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory StuManageMent
2013-3-20 12:35:10 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
2013-3-20 12:35:26 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2013-3-20 12:35:26 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2013-3-20 12:35:26 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/110  config=null
2013-3-20 12:35:27 org.apache.catalina.startup.Catalina start
信息: Server startup in 18854 ms
12:35:37,546  WARN OgnlValueStack:45 – Could not find property [struts.actionMapping]
12:35:37,625  WARN OgnlValueStack:45 – Could not find property [struts.valueStack]
java.lang.NullPointerException
at web.action.StuAction.getStus(StuAction.java:43)
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 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
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:102)
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:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
12:35:38,390  WARN OgnlValueStack:45 – Could not find property [org.apache.catalina.jsp_file]
小写也不行的、还是注入不了对象
spring为什么注入不了对象
你好,请问问题解决了吗,我也遇到注入不了对象的问题
spring为什么注入不了对象
我出现这样的情况,是因为少了spring和struts的插件包,可以试试是不是这个原因

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring为什么注入不了对象
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!