spring 注解不生效的问题

pangyongmeng 2010-04-29 03:51:12
本菜鸟试着用spring的注解方式,但是注解不生效。
web.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">




<!-- Data Source-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/helloWorld?createDatabaseIfNotExist=true" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="maxWait" value="5000" />
</bean>
<!--sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean ">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

<context:annotation-config />
<!--bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/-->
<bean id="fruitService" class="com.megasoft.service.impl.FruitServiceImpl"/>
<bean id="fruitService_2" class="com.megasoft.service.impl.FruitService_2Impl"/>

</beans>


java代码如下:

@Resource
private IFruitService_2 fruitService_2;

public void test() {
fruitService_2.hello();
}


导入的包如下:

晕不懂发图;

最后在
public void test() {
fruitService_2.hello();
}
抛出了空指针。
...全文
398 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hoseli007 2011-05-15
  • 打赏
  • 举报
回复
不错啊
pangyongmeng 2010-05-19
  • 打赏
  • 举报
回复
呵呵,可能是因为用了Tapestry注入的原因,影响了SPRING注入。
我用同样的包,同样的代码,在SSH中可以正常运行。谢谢大家的回帖。
zs86374877 2010-04-29
  • 打赏
  • 举报
回复
spring 的依赖注入需要在类中给出你关联的类的set方法,程序运行的时候会去调用这个方法从而获得这个类的实例!
所以在类里面加上public void setFruitService_2(IFruitService_2 fruitService_2) {this.fruitService_2=fruitService_2}
易写科技 2010-04-29
  • 打赏
  • 举报
回复
是正常启动
易写科技 2010-04-29
  • 打赏
  • 举报
回复
<!-- -->
抛出了空指针不是你spring注释的问题,是的话项目根本就不能启动
zs86374877 2010-04-29
  • 打赏
  • 举报
回复
spring 的依赖注入需要在类中给出你关联的类的set方法,程序运行的时候会去调用这个方法从而获得这个类的实例!
所以在类里面加上public void fruitService_2(IFruitService_2 fruitService_2) {this.fruitService_2=fruitService_2}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧