发布于 2018-03-14 08:57:14 | 140 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


下面小编就为大家带来一篇详谈Spring对IOC的理解(推荐篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、IOC控制反转和DI依赖注入

1.控制反转,字面可以理解为:主动权的转移,原来一个应用程序内的对象是类通过new去主动创建并实例化的,对对像创建的主动权在程序代码中。程序不仅要管理业务逻辑也要管理对的象创建和依赖关系。这是很累的,也跟软件工程 "低耦合高内聚" 的概念不十分符合。

有了spring的ioc容器之后,对象的实例化和依赖关系管理都由IOC容器进行统一管理,主体类只要依赖ioc容器就够了,需要啥,容器会给他注入进去,也就是只要声明对象不用再主动去new,ioc容器帮忙把相应的对象注入到声明对象中,使其变成实例化对象。(类似主体类提供一个躯体,ioc容器把灵魂注入进去,使其变成一个生命体,激活他),这样创建对象的主动权就转移交接了,

  

二、使用xml配置方式实现IOC

1.在ioc容器中配置了dao实现类和service类的bean,在容器加载的时候就会实例化这些bean到内存中。(bean.xml配置如下)


<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.xsd
  ">

  <!-- BookDao Bean -->
  <bean id="bookDao" class="com.study.DaoImpl.BookDaoImpl"></bean>

  <!-- BookService Bean-->
  <bean id="bookService" class="com.study.Service.BookService">
    <!-- BookService中的声明了BookDao对象,通过ref属性将BookDao的bean注入到对象中 -->
    <property name="bookDao" ref="bookDao"/>
  </bean>
</beans>

2. service类中需要用到dao类的实例(正常情况下需要new一个dao类对象),但是用ioc容器接管后只需要声明dao接口对象即可,然后写一个dao对象的set方法。(要注入的对象必须要有set方法,否则将报错 Bean property 'bookDao' is not writable or has an invalid setter method)因为spring注入是根据反射机制实现的,他在反射注入的时候会调用该方法名的set方法,如果set方法写错,或者根本没写,那么注入就会失败。(BookService类如下)


public class BookService {
 private BookDao bookDao;

 public BookService() {
  System.out.println("BookService实例化");
 }

 public void setBookDao(BookDao bookDao) {
  System.out.println("BookService属性初始化装配成功");
  this.bookDao = bookDao;
 }

 public void storeBook(String bookname){
  System.out.println("图书上架");
  System.out.println(bookDao.addBook(bookname));
 }
}

如上代码:BookSerivce类需要用到BookDao对象,但是却没有new对象,只有一个set方法,这个set方法就是ioc容器注入的入口(必不可少),

3.此处我们用ApplicationContext作为容器,初始化配置文件,然后从容器中根据id名取出容器中已经帮我们实例化好的对象。


public class TestDmeo {
 BookService bookService;

 @Test
 public void testStoreBook(){
  System.out.println("容器初始化");
  ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
  bookService = (BookService) app.getBean("bookService");//将对象注入到声明好的BookService对象中。(bookService就是配置文件中的id)
  bookService.storeBook("Spring MVC");
 }
}

 

4.dao类和实现类如下:

接口类:


public interface BookDao {
  public String addBook(String BookName);
 }

实现类:


public class BookDaoImpl implements BookDao {

 public BookDaoImpl() {
  System.out.println("BookDao实例化");
 }

 public String addBook(String BookName) {
  return BookName+"添加成功";
 }
}

5.运行测试结果:

6.大体思路如下图:

程序中除了初始化容器用了new对象,其余的基本没有new的存在。

二、注解方式配置IOC

注解配置方式目的和xml配置的目的一样,都是为了实现bean的创建。常用的注解如下:

@Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean。

@Repository 对Dao实现类进行注解 (特殊的@Component)

@Service 用于对业务逻辑层进行注解, (特殊的@Component)

@Controller 用于控制层注解 , (特殊的@Component)

装配注解如下:

@Autowired 默认按照类型装配注入,想按照名称来装配的话要结合@Quapfier(“name”)一起使用,使用@Autowired注解可以不用set方法。@Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个

@Quapfier("name") 中的name是bean的名字,也就是id,和@Autowired可以作为限定专配对象的名称

@Resource 默认按照名称装配注入,当找不到对应名成的bean的时候就按照类型匹配,如果还是找不到的话就会报错,@Autowired是spring提供的,@Resource是javaee提供,使用@Resource可以减少对spring的依赖

范例:

1.例子同上,只是配置bean的方式从xml文件中转移到了代码中,用注解体现。

2.除了把配置文件中<bean id="" class=""/>变成对应的注解外,另外一个区别在于,bean.xml文件中的修改,需要做如下,配置才能够使注解生效

context的配置有如下方法:

1.仅扫描特定包下的特定类


<context:component-scan base-package="com.study" resource-pattern="Service/B*.class"/>

这是扫描Service包下B开头的所有类。

2.使用<context:include-filter .../>和<context:exclude-filter .../>配置那些需要和不需要的扫描的包

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class(注解类)
assignable org.example.SomeClass 指定class或interface的全名(指定确切的类或接口)
aspectj org.example..*Service+ AspectJ语法
regex org\.example\.Default.* Regelar Expression  (正则表达式)
custom org.example.MyTypeFilter Spring3新增自定义Type,org.springframework.core.type.TypeFilter


<!-- 容器扫描包下的注解配置组件 -->
  <context:component-scan base-package="com.study" use-default-filters="false">
    <context:include-filter type="aspectj" expression="com.study.Service.*"/> <!-- 模糊过滤 -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/><!-- 过滤指定的注解 -->
    <context:include-filter type="assignable" expression="com.study.Service.BookService"/><!-- 过滤指定的类或接口,路径要完整,如果是接口的话,所有派生类都会被过滤 -->
    <context:include-filter type="regex" expression="com.*"/>
  </context:component-scan>

<context:exclude-filter ../>要配在<context:include-filter .../>的后面。

以上这篇详谈Spring对IOC的理解(推荐篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务