junjun16818 2010-09-29 09:49
浏览 273
已采纳

Spring Annotation 入门时的问题

springConfig.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-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:annotation-config/>

<bean id = "user" class="com.spring.demo.User"></bean>

<bean id = "helloBean" class="com.spring.demo.Test"  >

</bean>
</beans>

 

 

Test.java
--------------------------------------------------------------------------------------------------------------------

package com.spring.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Test {

private User user;


public User getUser() {
return user;
}


@Autowired
public void setUser(User user) {
this.user = user;
}

@Override
public String toString() {


String str = user+"";
return str;


}

}

 

 

main函数
----------------------------------------------------------------------------------------------------------------------
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {


Resource res = new ClassPathResource("com/spring/demo/springConfig.xml");
BeanFactory factory = new XmlBeanFactory(res);


Test hello = (Test) factory.getBean("helloBean");

System.out.println(hello.toString());
}

------------------------------------------------------------------------------------------------------------------''

就这样  为什么 user 这个对象是个 null 啊 
刚学习 spring  遇到这个问题  怎么也想不通 求大哥大姐 们帮忙 解答一下

 

  • 写回答

1条回答

  • weixin_42527311 2010-09-29 09:58
    关注

    [size=xx-large][color=blue]原文:


    修改:



    [/color][/size]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划