spring的IOC

IOC(Inversion of Control)控制反转,也叫DI(Dependency Injection)依赖注入,就是由容器控制程序之间的关系,而非程序代码直接操控,控制权由应用代码中转到了外部容器。就是说我有两个类,一个是A类一个是B类,如果在B类中想调用A类中的方法,传统的方法就是在B类中new一个A类的对象出来,但是spring的IOC中是在spring配置文件里面进行A类的创建,B类就不需要进行A类的创建,从而省去了这个环节。简单一点说:IOC就是一个生产和管理bean的容器,原来需要在调用类中new的东西,现在都是通过容器生成,同时,要是产生的是单例的bean,他还可以给管理bean的生命周期。

IOC有点像通过中介租房子,在我和房子之间引入第三者:中介。中介公司管理了很多房子的资料,我向中介提出一系列要求,告诉他我想找个什么样的房子,比如家电是否齐全,电视要什么牌子,要几室几厅,厨房是否有油烟机之类的,然后中介就会按照我们的要求,提供一个房子,我们只需要去看房子就行了。简单明了,如果中介给我们的房子不符合要求,我们就会抛出异常。整个过程不再由我自己控制,而是有中介公司这样一个类似容器的机构来控制。

使用单例或多例的原则是:“如果类中没有属性或有属性且不需要变的就用单例,反之用多例”。

使用注解的方式

spring-ioc-annotation-beans.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"
	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.3.xsd">

	<!-- 指定spring去哪些包下扫描组件 -->
	<context:component-scan base-package="com.spring.ioc.annotation" />
	
	<!-- 开启spring对注解的支持 -->
	<context:annotation-config />
</beans>

User实体类

package com.spring.ioc.annotation.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {

	@Value("1004")
	private int id;
	@Value("张三")
	private String username;
	@Value("456456")
	private String password;
	@Value("zhangsan@qq.com")
	private String emain;
	@Value("17845983765")
	private String phone;

	public User() {
		super();
	}

	public User(int id, String username, String password, String emain, String phone) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.emain = emain;
		this.phone = phone;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmain() {
		return emain;
	}

	public void setEmain(String emain) {
		this.emain = emain;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", emain=" + emain + ", phone="
				+ phone + "]";
	}

}

UserDao层

package com.spring.ioc.annotation.dao;

import com.spring.ioc.annotation.entity.User;

public interface UserDao {

	int add(User user);

	int del(int id);

	int update(User user);

	User load(int id);

}

UserDaoImpl

package com.spring.ioc.annotation.dao.impl;

import org.springframework.stereotype.Repository;

import com.spring.ioc.annotation.dao.UserDao;
import com.spring.ioc.annotation.entity.User;

//@Component
// 或
@Repository
public class UserDaoImpl implements UserDao {

	@Override
	public int add(User user) {
		System.out.println("添加了" + user);
		return 1;
	}

	@Override
	public int del(int id) {
		System.out.println("删除了id为" + id + "的数据");
		return 1;
	}

	@Override
	public int update(User user) {
		System.out.println("修改数据为" + user);
		return 1;
	}

	@Override
	public User load(int id) {
		User user = new User(2, "李四", "lll", "ls@qq.com", "17598463574");
		System.out.println(user);
		return user;
	}

}

UserService层

package com.spring.ioc.annotation.service;

import com.spring.ioc.annotation.entity.User;

public interface UserService {

	int add(User user);

	int del(int id);

	int update(User user);

	User load(int id);

}

UserServiceImpl

package com.spring.ioc.annotation.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.ioc.annotation.dao.UserDao;
import com.spring.ioc.annotation.entity.User;
import com.spring.ioc.annotation.service.UserService;

//@Component
// 或
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public int add(User user) {
		return userDao.add(user);
	}

	@Override
	public int del(int id) {
		return userDao.del(id);
	}

	@Override
	public int update(User user) {
		return userDao.update(user);
	}

	@Override
	public User load(int id) {
		return userDao.load(id);
	}

}

UserController层

package com.spring.ioc.annotation.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.spring.ioc.annotation.entity.User;
import com.spring.ioc.annotation.service.UserService;

// @Component相当于<bean id="userController" class="com.spring.xml.controller.UserController" scope="prototype">
//@Component
// 或
@Controller
@Scope("prototype") // 多例模式
public class UserController {

	@Autowired
	private User user;

	@Autowired // 根据类型自动注入
	private UserService userService;

	@Value("1") // 注入id值
	private int id;

	@Value("张三")
	private List<String> names;

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public void outUser() {
		System.out.println(user);
	}

	public void outNames() {
		System.out.println(names);
	}

	/*******************************************/

	public int add() {
		int i = 0;
		i = userService.add(user);
		return i;
	}

	public int del() {
		int i = 0;
		i = userService.del(id);
		return i;
	}

	public int update() {
		int i = 0;
		i = userService.update(user);
		return i;
	}

	public User load() {
		User user = null;
		user = userService.load(id);
		return user;
	}

}

TestUser测试类

package com.spring.ioc.annotation.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.ioc.annotation.controller.UserController;
import com.spring.ioc.annotation.entity.User;

public class TestUserController {

	private BeanFactory bf = new ClassPathXmlApplicationContext("spring-ioc-annotation-beans.xml");

	@Test
	public void addTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		User user = new User(1, "Tom", "101010", "Tom@qq.com", "15632475984");
		uc.setUser(user);
		uc.add();
	}

	@Test
	public void outUserTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outUser();
	}

	@Test
	public void outNamesTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outNames();
	}

	@Test
	public void delTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.del();
	}

	@Test
	public void updateTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		User user = new User(1003, "Tom", "101010", "Tom@qq.com", "15632475984");
		uc.setUser(user);
		uc.update();
	}

	@Test
	public void loadTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.load();
	}

}

使用xml的方式

spring-ioc-xml-beans.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.xsd">

	<bean id="user" class="com.spring.ioc.xml.entity.User">
		<property name="id" value="1001"/>
		<property name="username" value="jack"/>
		<property name="password" value="123123"/>
		<property name="emain" value="jack@qq.com"/>
		<property name="phone" value="18754697531"/>
	</bean>
	
	<bean id="userDao" class="com.spring.ioc.xml.dao.impl.UserDaoImpl" />

	<!--
		autowire:自动注入
		default使用父标签的注入方式;no不使用自动注入
		constructor使用构造器方式注入
		byName根据属性名称注入;byType根据属性类型注入
	-->
	<bean id="userService" class="com.spring.ioc.xml.service.impl.UserServiceImpl" autowire="byName">
		<!--
			setXxx方式实现注入
			name="userDao"的userDao是自动调用UserServiceImpl里面的userDao属性对应的setXxx()方法实现注入
			ref="userDao"的userDao是依赖某个<bean id="xxx">里面的id值
		-->
		<!-- <property name="userDao" ref="userDao" /> -->
		<!--
			构造函数的方式实现注入
			ref="userDao"的userDao是依赖某个<bean id="xxx">里面的id值
		-->
		<!-- <constructor-arg ref="userDao"/> -->
	</bean>

	<!--
		scope:作用域;值有:singleton(默认值:单例模式),prototype(多例模式),request,session
		对于Controller层而言,属性的状态值会根据不同的线程请求而得到不同的值,就应该用多例
	-->
	<bean id="userController" class="com.spring.ioc.xml.controller.UserController" scope="prototype">
		<property name="user" ref="user"/>
		<property name="userService" ref="userService" />
		<property name="id" value="1002"/>
		<property name="names">
			<list>
				<value>Tom</value>
				<value>Jack</value>
				<value>Jerry</value>
			</list>
		</property>
	</bean>
</beans>

User实体类

package com.spring.ioc.xml.entity;

public class User {

	private int id;
	private String username;
	private String password;
	private String emain;
	private String phone;

	public User() {
		super();
	}

	public User(int id, String username, String password, String emain, String phone) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.emain = emain;
		this.phone = phone;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmain() {
		return emain;
	}

	public void setEmain(String emain) {
		this.emain = emain;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", emain=" + emain + ", phone="
				+ phone + "]";
	}

}

UserDao层

package com.spring.ioc.xml.dao;

import com.spring.ioc.xml.entity.User;

public interface UserDao {

	int add(User user);

	int del(int id);

	int update(User user);

	User load(int id);

}

UserDaoImpl

package com.spring.ioc.xml.dao.impl;

import com.spring.ioc.xml.dao.UserDao;
import com.spring.ioc.xml.entity.User;

public class UserDaoImpl implements UserDao {

	@Override
	public int add(User user) {
		System.out.println("添加了" + user);
		return 1;
	}

	@Override
	public int del(int id) {
		System.out.println("删除了id为" + id + "的数据");
		return 1;
	}

	@Override
	public int update(User user) {
		System.out.println("修改了" + user + "的数据");
		return 1;
	}

	@Override
	public User load(int id) {
		User user = new User(2, "李四", "lll", "ls@qq.com", "17598463574");
		return user;
	}

}

UserService层

package com.spring.ioc.xml.service;

import com.spring.ioc.xml.entity.User;

public interface UserService {

	int add(User user);

	int del(int id);

	int update(User user);

	User load(int id);

}

UserServiceImpl

package com.spring.ioc.xml.service.impl;

import com.spring.ioc.xml.dao.UserDao;
import com.spring.ioc.xml.entity.User;
import com.spring.ioc.xml.service.UserService;

public class UserServiceImpl implements UserService {

	private UserDao userDao;

	// setXxx方式实现注入
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	// 构造函数的方式实现注入
//	public UserServiceImpl(UserDao userDao) {
//		this.userDao = userDao;
//	}

	@Override
	public int add(User user) {
		return userDao.add(user);
	}

	@Override
	public int del(int id) {
		return userDao.del(id);
	}

	@Override
	public int update(User user) {
		return userDao.update(user);
	}

	@Override
	public User load(int id) {
		return userDao.load(id);
	}

}

UserController层

package com.spring.ioc.xml.controller;

import java.util.List;

import com.spring.ioc.xml.entity.User;
import com.spring.ioc.xml.service.UserService;

public class UserController {

	private User user;
	private UserService userService;
	private int id;
	private List<String> names;

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setNames(List<String> names) {
		this.names = names;
	}

	public void outUser() {
		System.out.println(user);
	}

	public void outNames() {
		System.out.println(names);
	}

	/*******************************************/

	public int add() {
		int i = 0;
		i = userService.add(user);
		return i;
	}

	public int del() {
		int i = 0;
		i = userService.del(id);
		return i;
	}

	public int update() {
		int i = 0;
		i = userService.update(user);
		return i;
	}

	public User load() {
		User user = null;
		user = userService.load(id);
		return user;
	}

}

TestUser测试类

package com.spring.ioc.xml.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.ioc.xml.controller.UserController;
import com.spring.ioc.xml.entity.User;

public class TestUserController {

	private BeanFactory bf = new ClassPathXmlApplicationContext("spring-ioc-xml-beans.xml");

	@Test
	public void addTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		User user = new User(1, "Tom", "101010", "Tom@qq.com", "15632475984");
		uc.setUser(user);
		uc.add();
	}

	@Test
	public void outUserTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outUser();
	}

	@Test
	public void outNamesTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.outNames();
	}

	@Test
	public void delTest() {
		UserController uc = bf.getBean("userController", UserController.class);
		uc.del();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值