概括说明:下面通过实例介绍下属性方法注入、构造方法注入

1、源码结构图

Spring IOC(通过实例介绍,属性与构造方法注入)-LMLPHP

2、代码介绍

(1)、Dao接口 :UserDAO

(2)、Dao接口实现:UserDAOImpl

(3)、实体类:User

(4)、服务层接口:UserService

(5)、服务层接口实现:UserServiceImpl

(6)、测试主类:UserServiceTest

3、源代码

(1)、Dao接口 :UserDAO

package com.northeasttycoon.dao;

import com.northeasttycoon.model.User;

/**
* @author tycoon
*/
public interface UserDAO { public void save(User user);
}

(2)、Dao接口实现:UserDAOImpl

package com.northeasttycoon.dao.impl;

import com.northeasttycoon.dao.UserDAO;
import com.northeasttycoon.model.User;
/**
* @author tycoon
*/
public class UserDAOImpl implements UserDAO { // 保存数据方法
public void save(User user) { System.out.println("username:"+user.getUsername() +" ,userpassword:"+user.getPassword()+ " ,saved in Oracle!");
System.out.println(user.getUsername() + " saved in DB2!");
}
}

(3)、实体类:User

package com.northeasttycoon.model;

/**
* 用户实体类
* @author tycoon
*
*/
public class User { private String username;
private String password; 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;
}
}

(4)、服务层接口:UserService

package com.northeasttycoon.service;

import com.northeasttycoon.model.User;

/**
* 服务层接口
* @author tycoon
*
*/
public interface UserService { //处理新增用户业务逻辑
public void add(User user);
}

(5)、服务层接口实现:UserServiceImpl

package com.northeasttycoon.service;

import com.northeasttycoon.dao.UserDAO;
import com.northeasttycoon.model.User; /**
* 服务层接口实现类
* @author tycoon
*/
public class UserServiceImpl { // 属性注册方式实现方法注入
private UserDAO userDAO; public void add(User user) {
userDAO.save(user);
} public UserDAO getUserDAO() {
return userDAO;
} public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
} // 构造方法注入
// public UserServiceImpl(UserDAO userDAO) {
// super();
// this.userDAO = userDAO;
// }
}

(6)、测试主类:UserServiceTest

package com.northeasttycoon.service.test;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.northeasttycoon.model.User;
import com.northeasttycoon.service.UserServiceImpl; // 测试类
public class UserServiceTest { final static Logger logger = Logger.getLogger(UserServiceTest.class); @Test
public void testAdd() throws Exception {
logger.info("insert sucessful start! ");
// 创建容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl service = (UserServiceImpl) ctx.getBean("userService"); // 创建U实例
User u = new User();
u.setUsername("东北大亨");
u.setPassword("123456");
service.add(u);
logger.info("insert sucessful end! ");
}
}

(7)、配置文件

<?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-2.5.xsd">
<bean id="u" class="com.northeasttycoon.dao.impl.UserDAOImpl"/>
<bean id="userService" class="com.northeasttycoon.service.UserServiceImpl">
<!-- 通过property元素 把U实例注册到userDAO中 -->
<property name="userDAO">
<ref bean="u"/>
</property>
<!-- 通过构造方法 把U实例注册到userDAO中 -->
<!--<constructor-arg>
<ref bean="u"/>
</constructor-arg>-->
</bean>
</beans>

(8)、测试结果截图

Spring IOC(通过实例介绍,属性与构造方法注入)-LMLPHP

05-11 22:50