无法访问带注释的弹簧服务

时间:2009-10-08 22:29:12

标签: java spring annotations

我正在尝试访问@Service定义的类

定义了接口,这是一个实现

    @Service
    public class TypeServiceImpl implements TypeService {

        @Autowired
        TypeDAO dao;

        public List<DocType> findAll() {
    System.out.println("accessing findAll"); //have no appearence
    return dao.findAll();
}

public TypeDAOImpl()     {         System.out.println(“Constructing TypeDAOImpl”);     }

...

}

DAO的代码:

@Repository
public class TypeDAOImpl extends BaseDAO<Type, Long> implements TypeDAO{

}

BaseDAO:

public class BaseDAO<T, ID extends Serializable> extends GenericDAOImpl<T, ID> {

    @Autowired
    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }
}

使用generic dao 应用程序的配置加载正常(没有警告或错误),但当我尝试使用服务时:

@Autowired
private TypeService TypeService;
public void init() {

        try {
            for (Type d : TypeService.findAll()) {
                System.out.println(d.getType());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}
appContext中的

<!-- Use annotations (@Autowired) for property injection -->
    <context:annotation-config />
日志中的

: 信息:在org.springframework.beans.factory.support.DefaultListableBeanFactory@14db0e3中预先实例化单例:... typeDAOImpl,appMain,typeServiceImpl

我的web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/config/application-context.xml,classpath:/config/datasource-context.xml</param-value>
    </context-param>
    <context-param>
        <description>
    Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>false</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener> 
     <servlet>
        <servlet-name>My Application</servlet-name>
        <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
        <init-param>
            <description>
        Vaadin application class to start</description>
            <param-name>application</param-name>
            <param-value>myapp.main.MainApp</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>My Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
...
堆栈跟踪是“非常信息化的”(我使用vaadin作为Web框架)

显示java.lang.NullPointerException     在main.AppMain.init(AppMain.java:26)     在com.vaadin.Application.start(Application.java:497)     在com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1001)     在com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:411)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:717)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)     在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)     在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:583)     在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:454)     在java.lang.Thread.run(未知来源)

有什么建议吗?

我为GenericDAOHibernate和TypeDAOImpl添加了构造函数,据我所知,这些bean被初始化了 ... 12.10.2009 12:57:48 org.hibernate.impl.SessionFactoryImpl 信息:建立会话工厂 12.10.2009 12:57:48 org.hibernate.impl.SessionFactoryObjectFactory addInstance 信息:未将工厂绑定到JNDI,未配置JNDI名称 构建通用DAO 构建TypeDAOImpl 12.10.2009 12:57:48 org.springframework.web.context.ContextLoader initWebApplicationContext ...

我不明白因为这个配置适用于ZK ..

1 个答案:

答案 0 :(得分:0)

您发布的代码相当不一致。

findAll()方法未在任何地方声明;加上服务实现实现了Service,但是你试图将它自动装入TypeService,这是行不通的。

如果第一个代码片段改写为:

@Service
public class TypeServiceImpl implements TypeService {

并且它是您的应用上下文中唯一的TypeService实现,自动布线可行。