求助,Spring的autowire为什么不生效!

J2虾虾 2014-11-23 12:36:26
这几天在研究Spring,看Spring入门的书。

看到autowire(自动绑定)的时候,发现例子运行的结果与书上不一致。

Java Bean代码:



package com.tntxia.test.spring;

import java.util.Date;

public class HelloBean {

private String h;

private Date date;

public String getH() {
return h;
}

public void setH(String h) {
this.h = h;
}

public void sayHello(){
System.out.println(date+h);
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

}



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">

<bean id="date" class="java.util.Date"></bean>

<bean id="hello" class="com.tntxia.test.spring.HelloBean" autowire="byName">
<property name="h">
<value>Hello,World too</value>
</property>
</bean>

</beans>




测试类:



package com.tntxia.test.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

public static void main(String args[]){
Resource resource = new ClassPathResource("bean.xml");
BeanFactory bf = new XmlBeanFactory(resource);
HelloBean helloBean = (HelloBean)bf.getBean("hello");
helloBean.sayHello();
}

}

...全文
842 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xi629 2016-09-23
  • 打赏
  • 举报
回复
很明显private Date date;上面需加上@Autowired [ xxxxx]
技术控_ 2016-09-21
  • 打赏
  • 举报
回复
<context:component-scan base-package=" 包名"/> //自动扫描注解的包 @Autowired 要注入的实体或属性
jinguodongweida 2016-09-20
  • 打赏
  • 举报
回复
在需要注入的地方加入@Autowrired
  • 打赏
  • 举报
回复
楼主解决了么,我也遇到了这个问题
J2虾虾 2014-11-26
  • 打赏
  • 举报
回复
如果手动增加一个property的那么autowire这个属性就没什么作用了,我看书上说autowire可以简化配置文件。但实际操作却没有效果,也没有报错。
W441675 2014-11-26
  • 打赏
  • 举报
回复
必须在spring XML配置文件中开启注解 自动装配才行, <!-- enable autowire --> <context:annotation-config />
huangxiaohui1991 2014-11-26
  • 打赏
  • 举报
回复
你没有用@autowire注解啊
dokia123 2014-11-26
  • 打赏
  • 举报
回复
加了<property name="date" />后,autowire才会生效啊,这样也是为了避免一个对象的属性太多,而又设置成了autowire,就会无脑的去遍历给不需要注入的属性进行注入工作,影响性能。
dokia123 2014-11-26
  • 打赏
  • 举报
回复
你还要加一个property属性,即<property name="date" />,然后不要使用ref来引用。这样是让spring明确哪些字段要自动注入,哪些字段不需要。
dokia123 2014-11-23
  • 打赏
  • 举报
回复
把你的运行结果发出来撒
RunningShadow_007 2014-11-23
  • 打赏
  • 举报
回复
我搞错了 你的date只是一个属性你怎么把它写成 bean了? 它只是hello这个bean下的一个属性 在hello这个bean下添加一个 <property name="date"></property>就好了
RunningShadow_007 2014-11-23
  • 打赏
  • 举报
回复
缺少context命名空间 添加上context命名空间后 <!-- 扫描包中的注解 --> xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd <context:component-scan base-package="com"></context:component-scan>
dokia123 2014-11-23
  • 打赏
  • 举报
回复
需要你在配置文件里显示的注入date这个bean吧?
J2虾虾 2014-11-23
  • 打赏
  • 举报
回复
显示: nullHello,World 如果会自动绑定Date对象的话,应该可以输出日期才对

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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