<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="dao" class="com.atguigu.spring.ref.Dao" >
<property name="dataSource" value="C3P0"></property>
</bean>
<bean id="service" class="com.atguigu.spring.ref.Service" autowire="byName"></bean>
</beans>
Service 类
public class Service {
private Dao d;
public void setDao(Dao d) {
System.out.println("setDao......");
System.out.println("dao.toString---------"+d.toString());
this.d = d;
}
public Dao getDao() {
return d;
}
}
主方法
public class AutowireTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext cxl=new ClassPathXmlApplicationContext("beans-autowireTest.xml");
}
}
运行结果
Dao's Constructor...
setDao......
dao.toString---------Dao [dataSource=C3P0] 这是完成了service类中d属性的自动装配吧,可是bean的id和属性名不一致啊?
这是为什么,求大神指教
第一个字母会变成小写。所以Service-->service
和Service有关系吗?不是为Service中的Dao类型的属性注入吗?没懂