sprig-ioc之setter注入(设值注入)

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) {
		//实体类User赋值的两种方法
		/*User u = new User();
		u.setAge(10);
		u.setEmail("22222222@qq.com");
		u.setUserName("月光下的河");
		u.setRealName("可可");
		u.setUserId(userId);
		return u;*/
		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;
	}
	
	/**
	 * 注入的成员变量必须有setter方法
	 * @param 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(){
		//这个userId取得是UserController的bean中属性为userId的值
		System.out.println(userId);
		User u = userService.getById(1);
		System.out.println(u.getUserName()+" "+u.getRealName());
	}


	public UserService getUserService() {
		return userService;
	}


	/**
	 * 注入的成员变量必须有setter方法
	 * @param userId
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public int getUserId() {
		return userId;
	}

	/**
	 * 注入的成员变量必须有setter方法
	 * @param 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) {
		
		/*UserController uc = new UserController();
		uc.showUser();*/
		
		
		//使用spring提供的ClassPathXmlApplicationContext类,加载配置文件
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		//applicationContext 提供一个获取配置后的bean的方法
		applicationContext.getBean("userController");
		UserController userController = (UserController)applicationContext.getBean("userController");
		UserController userController1 = (UserController)applicationContext.getBean("userController");
		//调用showUser方法
		userController.showUser();
		
		//spring管理bean 默认是单例 即比较两个对象地址是否指向同一个地址空间
		//applicationContext.xml配置里里面 bean有个属性 scope="singleton"/"prototype"
		//singleton:单态(默认),Spring容器里只有一个共享的实例存在
		//prototype:原型 ,每次请求ID的bean都将产生新的实例
		System.out.println(userController==userController1);
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值