关于Spring注解@Configuration


以下内容仅供参考

用途

  1. 代替xml的配置方式,使用Java的方式进行配置,被@Configuration注解的类被称为配置类
  • 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
       
    <bean id="bean01" class="bean.Bean01"></bean>
    <bean id="bean02" class="bean.Bean02"></bean>
    
</beans>
  • java方式
@Configuration
public class Configuration01 {

    @Bean
    public Bean01 bean01(){
        return new Bean01();
    }

    @Bean
    public Bean02 bean02(){
        return new Bean02();
    }

}

也就是说@Configuration类比于xml中的< beans >

使用方式

1. 结合@Bean注解在Spring容器中注册Bean

  1. 两个测试实体类
package bean;

public class Bean01 {
    public Bean01(){
        System.out.println("Bean01构造器被调用");
    }
}
package bean;

public class Bean02 {
    public Bean02(){
        System.out.println("Bean02构造器被调用");
    }
}
  1. 配置类(使用@Configuration进行注解),需要注册的实体类用@Bean进行注解,方法名列如bean01,bean02为Spring容器中的名称。
package configuration;

import bean.Bean01;
import bean.Bean02;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Configuration01 {

    @Bean
    public Bean01 bean01(){
        return new Bean01();
    }

    @Bean
    public Bean02 bean02(){
        return new Bean02();
    }

}
  1. 测试
package test;

import configuration.Configuration01;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test01 {
    public static void main(String[] args) {
        new AnnotationConfigApplicationContext(Configuration01.class);
    }
}
  1. 测试结果,这表明这两个类的对象已经被实例化并由Spring进行管理了。
Bean01构造器被调用
Bean02构造器被调用

2. 结合@Component在Spring容器中注册Bean

  1. 创建实体类Bean03,并使用@Component进行注解
package bean;

import org.springframework.stereotype.Component;

@Component
public class Bean03 {
    public Bean03(){
        System.out.println("Bean03构造器被调用");
    }
}
  1. 在配置类上增加一个包扫描注解@ComponentScan,值为包名 这里是bean
package configuration;


import bean.Bean01;
import bean.Bean02;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("bean")
public class Configuration01 {

    @Bean
    public Bean01 bean01(){
        return new Bean01();
    }

    @Bean
    public Bean02 bean02(){
        return new Bean02();
    }

}
  1. 测试
package test;

import configuration.Configuration01;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test01 {
    public static void main(String[] args) {
        new AnnotationConfigApplicationContext(Configuration01.class);
    }
}
  1. 结果
Bean03构造器被调用
Bean01构造器被调用
Bean02构造器被调用

为什么是先03在01,02呢?我想大概是Spring扫描时扫描到注解@ComponentScan然后扫描到Bean03上的@Component注解就对bean03进行了注册

3.结合@Import在Spring容器中注册Bean

关于Spring注解@Import

对Web进行配置

web.xml的配置如下

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <context-param>
    <param-name>contextClass</param-name>
    <param-value>
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
  </context-param>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      configuration.Configuration01
    </param-value>
  </context-param>

  <listener>
    <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>


  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
      <param-name>contextClass</param-name>
      <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

这样Spring就会读取configuration.Configuration01这个配置类实现了无xml,纯java配置的方式

随便说说看个乐

  1. @Configuration = @Service = @Controller = @Component
    写成这样不过是为了方便阅读知道这是一个配置类,这是一个服务类,全部可用@Component进行代替,@Component目前理解是为了扫描,凡是被@Component注解了的类在被扫描后都会注册到Spring中。
  2. @Import不是一个组件,单独使用没有效果的
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {

	/**
	 * {@link Configuration @Configuration}, {@link ImportSelector},
	 * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
	 */
	Class<?>[] value();

}

但是被Import的类缺会被Spring接管,被注册

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值