创建新项目
记得打勾
CTRL+ALT+SHIFT+S打开添加外部jar包spring-framework,文末提供链接
做完添加确保有以上5个包
测试程序
public interface UserService {
public void say();
}
public class UseServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void say() {
this.userDao.say();
System.out.println("say goodbye");
}
}
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-4.3.xsd">
<bean id="userDao" class="com.demo.ioc.UserDaoImpl"/>
<bean id="userService" class="com.demo.ioc.UseServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
运行
public static void main(String[] args) {
ApplicationContext applicationContext=
new ClassPathXmlApplicationContext("beans.xml");
UserService userService;
userService = (UserService) applicationContext.getBean("userService");
userService.say();
}
}
实现
链接:https://pan.baidu.com/s/1fDPM5KgYF2i0YG2k7crQ-A
提取码:ah84