Spring读取配置文件实现自动注入
学习spring第二天:spring对应的配置文件Applicationcontext.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="userDao" ref="userDao" />
? </bean>
? <!-- 构造方法注入
? <bean id="useService" class="com.service.UserServiceImpl">
? ?<constructor-arg><ref bean="userDao"/></constructor-arg>
? </bean>
? -->
? <!-- 还有一种是接口注入 -->
</beans>
?
测试类:
package com.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.User;
import com.service.UserService;
public class TestSpringIoc {
?
?public static void main(String[] args) {
??ApplicationContext? context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
?? User user = new User();
????? user.setPassword("sss");
????? UserService service = (UserService)context.getBean("userService");
????? service.add(user);
?}
}
其他源文件见上篇文章!