- 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>
- Dao接口
package com.aaa.ioc.dao;
import com.aaa.ioc.entity.User;
public interface UserDao {
User getById(int userId);
}
- 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");
}
}
- Service接口
package com.aaa.ioc.service;
import com.aaa.ioc.entity.User;
public interface UserService {
User getById(int userId);
}
- 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);
}
public UserServiceImpl(UserDao userDao) {
super();
this.userDao = userDao;
}
}
- 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;
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());
}
}
- 实体层
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;
}
}
- 测试类
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");
UserController userController = (UserController)applicationContext.getBean("userC");
UserController userController1 = (UserController)applicationContext.getBean("userC");
userController.showUser();
System.out.println(userController==userController1);
}
}