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个回答

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


修改:



[/color][/size]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐