sprig-ioc之构造注入

  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">
<!-- 构造注入  -->
<!-- 把pojos交给spring管理 -->
<bean name="userC" class="com.aaa.ioc.controller.UserController">
	<constructor-arg index="0" ref="userS"></constructor-arg>
	<constructor-arg index="1" value="2"></constructor-arg>
</bean>

<bean name="userS" class="com.aaa.ioc.service.UserServiceImpl">
	<constructor-arg index="0" ref="userD"></constructor-arg>
</bean>

<bean name="userD" class="com.aaa.ioc.dao.UserDaoImpl"></bean>
</beans>
  1. Dao接口
package com.aaa.ioc.dao;

import com.aaa.ioc.entity.User;

public interface UserDao {

	User getById(int userId);
	
}
  1. Dao实现类
package com.aaa.ioc.dao;

import com.aaa.ioc.entity.User;

public class UserDaoImpl implements UserDao {

	@Override
	public User getById(int userId) {
		/*User u = new User();
		u.setAge(10);
		u.setEmail("1244787782@qq.com");
		u.setUserName("月光下的河");
		u.setRealName("可可");
		u.setUserId(userId);
		return u;*/
		return new User(userId,"月光下的河","可可",10,"1244787782@qq.com");
	}
	
}
  1. Service接口
package com.aaa.ioc.service;

import com.aaa.ioc.entity.User;

public interface UserService {

	User getById(int userId);
	}
	
  1. Service实现类
package com.aaa.ioc.service;

import com.aaa.ioc.dao.UserDao;
import com.aaa.ioc.dao.UserDaoImpl;
import com.aaa.ioc.entity.User;

public class UserServiceImpl implements UserService {

	private UserDao userDao;
	
	
	@Override
	public User getById(int userId) {
		return userDao.getById(userId);
	}

	/**
	 * 必须有构造方法
	 * @param userDao
	 */
	public UserServiceImpl(UserDao userDao) {
		super();
		this.userDao = userDao;
	}
	

}
  1. Controller层
package com.aaa.ioc.controller;

import com.aaa.ioc.entity.User;
import com.aaa.ioc.service.UserService;
import com.aaa.ioc.service.UserServiceImpl;

public class UserController {

	private UserService userService;
	private int userId;
	
	/**
	 * 必须有构造方法
	 * @param userDao
	 */
	public UserController(UserService userService, int userId) {
		super();
		this.userService = userService;
		this.userId = userId;
	}


	public void showUser(){
		System.out.println(userId);
		User u = userService.getById(1);
		System.out.println(u.getUserName()+" "+u.getRealName());
	}
	
}
  1. 实体层
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;
	}
		
}
  1. 测试类
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) {
		
		//使用spring提供的ClassPathXmlApplicationContext类,加载配置文件
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		
		//applicationContext 提供一个获取配置后的bean的方法
		UserController userController = (UserController)applicationContext.getBean("userC");
		UserController userController1 = (UserController)applicationContext.getBean("userC");
		//调用showUser方法
		userController.showUser();
		
		//spring管理bean 默认是单例 即比较两个对象地址是否指向同一个地址空间
		System.out.println(userController==userController1);
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值