spring-IOC理解1

spring IOC容器的关键点: 
* 必须将被管理的对象定义到spring配置文件中 
* 必须定义构造函数或setter方法,让spring将对象注入过来 
我们可以通过下面这里例子理解spring ioc的实现。本例使用spring 4.3.10 

实例:

一.导包:目前只需导个核心包

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

二.配置spring-context.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5     <bean id="userDao4MySqlImpl" class="com.yztc.spring1.dao.UserDao4MySqlImpl"/>
 6     <bean id="userDao40racleImpl" class="com.yztc.spring1.dao.UserDao40racleImpl"/>
 7     <bean id="userManagerImpl" class="com.yztc.spring1.contrallor.UserManagerImpl">
 8         <property name="userDao" ref="userDao4MySqlImpl"/>
 9     </bean>
10 
11 </beans>

三.创建需要注入的类

 1 public interface UserDao { 2 public void save(String username,String password); 3 } 

 1 package com.yztc.spring1.dao;
 2 
 3 /**
 4  * _ooOoo_
 5  * o8888888o
 6  * 88" . "88
 7  * (| -_- |)
 8  * O\ = /O
 9  * ___/`---'\____
10  * .   ' \\| |// `.
11  * / \\||| : |||// \
12  * / _||||| -:- |||||- \
13  * | | \\\ - /// | |
14  * | \_| ''\---/'' | |
15  * \ .-\__ `-` ___/-. /
16  * ___`. .' /--.--\ `. . __
17  * ."" '< `.___\_<|>_/___.' >'"".
18  * | | : `- \`.;`\ _ /`;.`/ - ` : | |
19  * \ \ `-. \_ __\ /__ _/ .-` / /
20  * ======`-.____`-.___\_____/___.-`____.-'======
21  * `=---='
22  * .............................................
23  *
24  * @author bindu
25  * @date 2017-10-25 19:25
26  */
27 
28 
29 public class UserDao4MySqlImpl implements UserDao {
30     @Override
31     public void save(String username, String password) {
32         System.out.println("----------UserDao4MySqlImpl------------");
33     }
34 }
1 public class UserDao40racleImpl implements UserDao {
2     @Override
3     public void save(String username, String password) {
4         System.out.println("----------------UserDao40racleImpl-------------");
5     }
6 }

四.被注入的类

1 package com.yztc.spring1.contrallor;
2 
3 public interface UserManager {
4     public void save(String username,String password);
5 }
 1 package com.yztc.spring1.contrallor;
 2 
 3 import com.yztc.spring1.dao.UserDao;
 4 
 5 /**
 6  * _ooOoo_
 7  * o8888888o
 8  * 88" . "88
 9  * (| -_- |)
10  * O\ = /O
11  * ___/`---'\____
12  * .   ' \\| |// `.
13  * / \\||| : |||// \
14  * / _||||| -:- |||||- \
15  * | | \\\ - /// | |
16  * | \_| ''\---/'' | |
17  * \ .-\__ `-` ___/-. /
18  * ___`. .' /--.--\ `. . __
19  * ."" '< `.___\_<|>_/___.' >'"".
20  * | | : `- \`.;`\ _ /`;.`/ - ` : | |
21  * \ \ `-. \_ __\ /__ _/ .-` / /
22  * ======`-.____`-.___\_____/___.-`____.-'======
23  * `=---='
24  * .............................................
25  *
26  * @author bindu
27  * @date 2017-10-25 19:29
28  */
29 
30 
31 public class UserManagerImpl implements UserManager {
    //先建立对象属性,在写构造方法或者setter方法
32 private UserDao userDao; 33 @Override 34 public void save(String username, String password) { 35 this.userDao.save(username,password); 36 } 37 38 public void setUserDao(UserDao userDao) { 39 this.userDao = userDao; 40 } 41 }

五.测试类

 1 package com.yztc.spring;
 2 
 3 import com.yztc.spring1.contrallor.UserManager;
 4 import com.yztc.spring1.contrallor.UserManagerImpl;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 /**
 8  * _ooOoo_
 9  * o8888888o
10  * 88" . "88
11  * (| -_- |)
12  * O\ = /O
13  * ___/`---'\____
14  * .   ' \\| |// `.
15  * / \\||| : |||// \
16  * / _||||| -:- |||||- \
17  * | | \\\ - /// | |
18  * | \_| ''\---/'' | |
19  * \ .-\__ `-` ___/-. /
20  * ___`. .' /--.--\ `. . __
21  * ."" '< `.___\_<|>_/___.' >'"".
22  * | | : `- \`.;`\ _ /`;.`/ - ` : | |
23  * \ \ `-. \_ __\ /__ _/ .-` / /
24  * ======`-.____`-.___\_____/___.-`____.-'======
25  * `=---='
26  * .............................................
27  *
28  * @author bindu
29  * @date 2017-10-25 19:33
30  */
31 
32 
33 public class SpringTest {
34     public static void main(String[] args) {
35         ClassPathXmlApplicationContext factoty = new ClassPathXmlApplicationContext("spring-context.xml");
36         UserManager userManager = (UserManager) factoty.getBean("userManagerImpl", UserManagerImpl.class);
37         userManager.save("zhangsan","121718");
38     }
39 }

 

posted @ 2017-10-25 20:19  友情天  阅读(105)  评论(0编辑  收藏  举报