1 spring配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 把普通的java类(pojos)交给spring管理 -->
<bean name="userController" class="com.aaa.ioc.controller.UserController" scope="singleton">
<!-- spring ioc 依赖注入的方法1 setter注入,又叫设值注入 -->
<property name="userService" ref="userS"></property>
<!-- 基本类型使用 -->
<property name="userId" value="200"></property>
</bean>
<bean name="userS" class="com.aaa.ioc.service.UserServiceImpl">
<property name="userDao" ref="userD"></property>
</bean>
<bean name="userD" class="com.aaa.ioc.dao.UserDaoImpl"></bean>
<!--
1.IoC是什么?
Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。在Java开发中,
Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。
2.谁控制谁,控制什么:传统Java SE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;
而IoC是有专门一个容器来创建这些对象,即由Ioc容器来控制对 象的创建;谁控制谁?
当然是IoC 容器控制了对象;控制什么?那就是主要控制了外部资源获取(不只是对象包括比如文件等)。
3.为何是反转,哪些方面反转了:
有反转就有正转,传统应用程序是由我们自己在对象中主动控制去直接获取依赖对象,也就是正转;
而反转则是由容器来帮忙创建及注入依赖对象;为何是反转?因为由容器帮我们查找及注入依赖对象,
对象只是被动的接受依赖对象,所以是反转;哪些方面反转了?依赖对象的获取被反转了。
4.DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,
即由容器动态的将某个依赖关系注入到组件之中。依赖注入的目的并非为软件系统带来更多功能,而是为了提升组件重用的频率,并为系统搭建一个灵活、可扩展的平台。
1 谁依赖于谁:当然是应用程序依赖于IoC容器;
2 为什么需要依赖:应用程序需要IoC容器来提供对象需要的外部资源;
3 谁注入谁:很明显是IoC容器注入应用程序某个对象,应用程序依赖的对象;
4 注入了什么:就是注入某个对象所需要的外部资源(包括对象、资源、常量数据)。
-->
</beans>
2.1 Dao接口
package com.aaa.ioc.dao;
import com.aaa.ioc.entity.User;
public interface UserDao {
User getById(int userId);
}
2.2 Dao实现类
package com.aaa.ioc.dao;
import com.aaa.ioc.entity.User;
public class UserDaoImpl implements UserDao {
@Override
public User getById(int userId) {
return new User(userId,"月光下的河","可可",10,"1244787782@qq.com");
}
}
3.1 Service接口
package com.aaa.ioc.service;
import com.aaa.ioc.entity.User;
public interface UserService {
User getById(int userId);
}
3.2 Service实现类
package com.aaa.ioc.service;
import com.aaa.ioc.dao.UserDao;
import com.aaa.ioc.entity.User;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public User getById(int userId) {
return userDao.getById(userId);
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
4 Controller层
package com.aaa.ioc.controller;
import com.aaa.ioc.entity.User;
import com.aaa.ioc.service.UserService;
public class UserController {
private UserService userService;
private int userId;
public void showUser(){
System.out.println(userId);
User u = userService.getById(1);
System.out.println(u.getUserName()+" "+u.getRealName());
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
5 实体类
package com.aaa.ioc.entity;
public class User {
private Integer userId;
private String userName;
private String realName;
private Integer age;
private String email;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User(Integer userId, String userName, String realName, Integer age,
String email) {
super();
this.userId = userId;
this.userName = userName;
this.realName = realName;
this.age = age;
this.email = email;
}
}
6 测试类
package com.aaa.ioc.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aaa.ioc.controller.UserController;
public class UserTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
applicationContext.getBean("userController");
UserController userController = (UserController)applicationContext.getBean("userController");
UserController userController1 = (UserController)applicationContext.getBean("userController");
userController.showUser();
System.out.println(userController==userController1);
}
}