spring(一)注解使用(@Configuration 、@Bean、@ComponentScan、@TypeFilter)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014252478/article/details/83831224

 

1、spring的框架:

spring的核心是控制反转(IOC)横向切面(AOP)

  • 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转 (IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。
  • Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
  • Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖 EJB 组件,就可以将声明性事务管理集成到应用程序中。
  • Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
  • Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
  • Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以,Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
  • Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText 和 POI。

Spring 框架的功能可以用在任何 J2EE 服务器中,大多数功能也适用于不受管理的环境。Spring 的核心要点是:支持不绑定到特定 J2EE 服务的可重用业务和数据访问对象。毫无疑问,这样的对象可以在不同 J2EE 环境 (Web 或 EJB)、独立应用程序、测试环境之间重用。

2、spring注解开发

spring从3.0开始支持注解开发,通过注解方式代替xml定义bean文件,例:

(1)xml定义bean文件:

<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-3.0.xsd">
 
	<bean id="helloBean" class="com.test.HelloWorldImpl">		
</beans>

注解方式为:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.test.HelloWorld;
import com.test.HelloWorldImpl;

@Configuration
public class AppConfig {
	
    @Bean(name="helloBean")
    public HelloWorld helloWorld() {
        return new HelloWorldImpl();
    }	
}

@configuration为配置类注解

(2)@Configuration 和@Bean ( xml定义、调用(以前),注解开发(现在))

以前:

xml定义:

<bean id="person" class="com.test.Person">
     <property name="age" value="18" > </property>
     <property name="name" value="haozi" > </property>
</bean>

main方法中调用:

public static void main(String[] args) {
     ApplicationContext applicationContext = new ClassPathApplicationContext("beans.xml");
     Person bean = applicationContext.getBean(Person.class); //或者getBean("person"); id获取
     Sysytem.out.println(bean);
}

现在:

@Configuration //告诉spring是一个配置类
public class AppConfig {
	
    @Bean(name="person") //给容器注册一个bean
    public Person person() {
        return new Person("haozi", 20);
    }	
}

(3)@ComponentScan自动扫描组件和扫描规则

首先创建三个组件类:

@Controller

@Controller
public class bookController {

}

@Service

@Service
public class bookService {

}

@Repository

@Respository
public class bookDao {

}

包扫描(配置在了配置类中)

@Configuation
@ComponentScan(value = "com.test")
public class mainConfig {
 @Bean(name="person") //给容器注册一个bean
    public Person person() {
        return new Person("haozi", 20);
    }	
}

测试类test:

public class test {
  public static void main(String[] args) {
     ApplicationContext applicationContext = new ClassPathApplicationContext(mainConfig.class);
     String[] defintionNames = applicationContext.getBeanDefinitionNames(); 
     for(String name: defintionNames ) {
        System.out.println(name);
     }
 }
}

此时会打印:

mainConfig
bookController
bookService
bookDao

可以选择只扫描哪些,只需要在@Component部分:(excludeFilter排除,includeFilter只包括)

@Component(value="com.test",excludeFilters={@Filter(type=FilterType.ANNOTATION,
classes=Contoller.class)})

此时仅排除Controller,由于classes可以是数组,因此可以排除多个即classes={Controller, Service}

同时可以选择是否禁用过滤:

useDefaultFilter=false/true

 下面是不禁用:

@Component(value="com.test",excludeFilters={@Filter(type=FilterType.ANNOTATION,
classes={Contoller.class})}, useDefaultFilter=false)

最后,扫描@ComponenScan可以写在@ComponentScans()中。

(4)TypeFilter指定过滤方式

FilterType.ASSIGNABLE_TYPE 按照指定类型

FlterType.ASPECTJ 按照ASPECTJ表达式

FilterType.REGEX 正则表达式

FilterType.CUSTOM 自定义方式过滤

@Component(value="com.test",includeFilters={
          @Filter(type=FilterType.ANNOTATION,classes={Contoller.class})
          @Filter(type=FilterType.ASSIGNABLE_TYPE, classes={bookService.class})
 }, useDefaultFilter=false)

只要是bookService这种类型都会显示。

重点讲解自定义(Custom)

首先建一个类myTypeFilter类:

public class myTypeFilter implements TypeFilter {

  // MetadataReader 读取当前类信息
  // MetadataReaderFactory 可以获取其他类信息
  @Override
  public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory ) throws IOException {

      ClassMetaName classMetaData = metadataReader.getClassMetaData(); //获取当前类信息
      String className = classMetaData.getClassName();
      System.out.println("——" + className);
      if(className.contains("er")) {  //如果类名包含er,则返回true,可以显示
          return true;
       }
      return false;
  }
}

下面指定自定义获取:

@Component(value="com.test",includeFilters={
          @Filter(type=FilterType.ANNOTATION,classes={Contoller.class})
          @Filter(type=FilterType.CUSTOM, classes={myTypeFilter.class})
 }, useDefaultFilter=false)

那么显示类型就会用:

bookService

猜你喜欢

转载自blog.csdn.net/u014252478/article/details/83831224