`

2.spring_beginning

阅读更多

1.Spring依赖库
 * SPRING_HOME/dist/spring.jar
 * SPRING_HOME/lib/jakarta-commons/commons-logging.jar
 * SPRING_HOME/lib/log4j/log4j-1.2.14.jar
 
2.拷贝Spring的配置文件applicationContext.xml到src下
 * SPRING_HOME/samples/jpetstore/war/WEB-INF/applicationContext.xml
 
3.在UserManager类中提供setter方法,让Spring将实例化发的实现注入给我们

 

4.让Spring管理我们的对象创建,让Spring管理我们的对象间的依赖关系,必需定义Spring配置文件

 

5.编写客户端
 //通过配置文件初始化Bean工厂
 BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
 
 //通过Bean工厂得到UserManager
 UserManager userManager = (UserManager) factory.getBean("UserManager"); 
 
 userManager.save("张三", "123");
 

Spring IoC的关键:
 * 让Spring管理你的对象,必须将被管理的对象定义在配置文件中
 * 定义setter或构造方法,Spring会将对象注入过来

 

 

=========================================================================

UserManager.java

 

package com.bjsxt.spring.manager;

import com.bjsxt.spring.dao.UserDao;

 

public class UserManager {
 
     private UserDao userDao;
 
     public UserManager(UserDao userDao) {
          this.userDao = userDao;
     }
 
     // public void setUserDao(UserDao userDao) {
     // this.userDao = userDao;
     // }

 

     public void delete(String userid) {

 

     }

 

     public void modify(String userid, String username, String password) {

 

     }

 

     public void save(String username, String password) {
          userDao.save(username, password);
     }


}

 

 

=========================================================================

UserDao.java

 

package com.bjsxt.spring.dao;

 

public interface UserDao {

 

     public void delete(String userid);

 

     public void modify(String userid, String username, String password);

 

     public void save(String username, String password);
}

 

 

=========================================================================

UserDao4MySqlImpl.java

 

package com.bjsxt.spring.dao;

 

public class UserDao4MySqlImpl implements UserDao {

 

      public void delete(String userid) {

 

      }

 

      public void modify(String userid, String username, String password) {

 

      }

 

      public void save(String username, String password) {

           System.out.println("-----------UserDao4MySqlImpl.save-----------");
      }

}

 

 

=========================================================================

UserDao4OracleImpl .java

 

package com.bjsxt.spring.dao;

 

public class UserDao4OracleImpl implements UserDao {

 

       public void delete(String userid) {

 

       }

 

       public void modify(String userid, String username, String password) {

 

       }

 

       public void save(String username, String password) {
              System.out.println("-----------UserDao4OracleImpl.save-----------");
       }

}

 

 

=========================================================================

Client .java

 

package com.bjsxt.spring.client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.spring.manager.UserManager;

 

public class Client {
 
       public static void main(String[] args) {
  
               //通过配置文件初始化Bean工厂
               BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  
               //通过Bean工厂得到UserManager
               UserManager userManager = (UserManager) factory.getBean("UserManager"); 
  
               userManager.save("张三", "123");
        }
 
}

 

 

=========================================================================

applicationContext.xml

 

 

 <?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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

        <bean id="UserDao4MySqlImpl" class="com.bjsxt.spring.dao.UserDao4MySqlImpl"></bean>
 
        <bean id="UserDao4OracleImpl" class="com.bjsxt.spring.dao.UserDao4OracleImpl"></bean>
 
        <bean id="UserManager" class="com.bjsxt.spring.manager.UserManager">
              <!-- 构造方法注入 -->
              <constructor-arg ref="UserDao4MySqlImpl"></constructor-arg>


              <!-- setter注入
              <property name="userDao" ref="UserDao4MySqlImpl"></property>
              -->
       </bean>
</beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    20110930_spring_beginning

    20110930_spring_beginning20110930_spring_beginning20110930_spring_beginning20110930_spring_beginning20110930_spring_beginning20110930_spring_beginning

    Beginning_Spring_Boot_2

    《 Beginning_Spring_Boot_2 》是一本专为初学者设计的...Spring_Boot_2_Tutorial.pdf这个文档很可能是本书的电子版,读者可以通过阅读其中的内容,逐步掌握Spring Boot 2的精髓,成为一名熟练的Spring Boot开发者。

    spring示例代码好又全.rar

    内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...

    Apress.Beginning.Spring.Boot.2.pdf

    根据提供的文件信息,我们可以归纳出以下关于《Beginning Spring Boot 2》这本书的关键知识点: ### 一、书籍基本信息 - **书名**:Beginning Spring Boot 2: Applications and Microservices with the Spring ...

    Wrox.Beginning.Spring.2015

    ### 使用Spring进行依赖注入 (Chapter 2) 依赖注入(DI, Dependency Injection)是Spring框架的核心特性之一。本章详细讲解了如何在Spring框架中实现依赖注入,并探讨了不同的依赖注入方式:构造器注入、setter注入...

    Beginning Spring Boot 2

    Beginning Spring Boot 2 Beginning Spring Boot 2 Beginning Spring Boot 2

    Beginning.Spring-Spring入门经典.part1.rar

    Beginning.Spring-Spring入门经典.part1.rarBeginning.Spring-Spring入门经典.part1.rarBeginning.Spring-Spring入门经典.part1.rarBeginning.Spring-Spring入门经典.part1.rar

    Beginning Spring Boot 2 Applications and Microservices with the Spring Framework

    This book will help you understand what Spring Boot is, how Spring Boot helps you build Spring-based applications quickly and easily, and the inner workings of Spring Boot using easy-to-follow ...

    Beginning-Spring-Boot-2.pdf

    Spring is the most popular Java-based framework for building enterprise applications. The Spring framework provides a rich ecosystem of projects to address modern application needs, like security, ...

    Beginning Spring Boot 2 Applications and Microservices with the Spring epub

    Beginning Spring Boot 2 Applications and Microservices with the Spring Framework 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    Beginning Spring Boot 2 (epub, mobi, pdf, source code)

    Beginning Spring Boot 2 - K. Siva Prasad Reddy. Learn Spring Boot and how to build Java-based enterprise, web, and microservice applications with it. In this book, you'll see how to work with ...

    Apress Beginning Spring Boot 2 PDF

    After reading and learning with Beginning Spring Boot 2, you'll have the skills and techniques to start building your first Spring Boot applications and microservices with confidence to take the next ...

    Beginning Spring(Wrox,2015)

    Beginning Spring is the complete beginner's guide to Java's most popular framework. Written with an eye toward real-world enterprises, the book covers all aspects of application development within the...

    Packt.Learning.Spring5

    Beginning with an introduction to Spring and setting up the environment, the book will teach you in detail about the Bean life cycle and help you discover the power of wiring for dependency injection....

    Spring Framework 数据(均为英文本, 均较新, 2017~2018)

    Beginning Spring Boot 2.pdf Learn Microservices with Spring Boot.pdf Pro Spring 5, 5th Edition.pdf Pro Spring Boot 2, 2nd Edition.pdf Pro Spring Boot.pdf Spring 5 Recipes, 4th Edition.pdf Spring Boot ...

    Beginning Spring(2015)

    通过上述章节的详细介绍,《Beginning Spring》这本书为初学者提供了全面而深入的Spring框架学习指南,不仅覆盖了Spring的基本概念和技术细节,还提供了实用的案例研究和最佳实践建议,对于想要深入了解Spring框架的...

Global site tag (gtag.js) - Google Analytics