?????????????bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--        ????????? -->
<bean id="user" class="com.bjsxt.model.User" >
<constructor-arg index="0" type="String" value="zhou"></constructor-arg>
<constructor-arg index="1" type="String" value="password"></constructor-arg>
</bean>
<!-- ??????? -->
<bean id="user1" class="com.bjsxt.model.User" >
<property name="username" value="zhang"></property>
<property name="password" value="oumyye"></property>
</bean>
<!-- ??????? -->
<bean  id= "userFactory" class="com.bjsxt.factory.UserFactory" > </bean>
<bean id="user2" factory-bean="userFactory" factory-method="CreatUser"></bean>
<!-- ?????????? -->
<bean  id= "user3" class="com.bjsxt.factory.UserFactory2" factory-method="CreatUser"> </bean>
<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean>
<bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">
<!-- <property name="userDAO" ref="u" /> -->
<!--        ????????? -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>
??????????
package com.bjsxt.service;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.model.User;
public class UserServiceTest {
@Test
public void test() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService) applicationContext.getBean("userService");
//?????????
User user=(User) applicationContext.getBean("user");
//???????
User user1=(User) applicationContext.getBean("user1");
//???????
User user2=(User) applicationContext.getBean("user2");
//???????
User user3=(User) applicationContext.getBean("user3");
System.out.println(user);
System.out.println(user1);
System.out.println(user2);
System.out.println(user3);
userService.save(user);
userService.destroy();
}
}
?????????
????###########init
????User [username=zhou?? password=password]
????User [username=zhang?? password=oumyye]
????User [username=??1?? password=oumyye]
????User [username=?my?1?? password=oumyye]
????user saved!
????destroy
????????????????????
???????????—??????
????Spring??????????????????????????????????????в????????????????????????????δ?????????????????????????????
???????????????????????????????£?
????<bean id="***" class="***" autowire="byType">
???????????????autowire???????????????????????????????д<property>???????????л?????????????????setter??????
????Autowire??????????????????
????· byType ?????????  ????????????????????????????????????bean?????ж?????????????????????????????????null??
????· byName ?????????  ???????????????????????в??????????????????bean?????????????????????null??
????· constructor ??byType?????????????????????????????????????????????????????????????????????μ?bean??????????????
????· autodetect ???bean??????????(introspection)???????????constructor????byType????????????????????????????????????????byType??????