关于Spring注解@Configuration
以下内容仅供参考
用途
- 代替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
- 两个测试实体类
package bean;
public class Bean01 {
public Bean01(){
System.out.println("Bean01构造器被调用");
}
}
package bean;
public class Bean02 {
public Bean02(){
System.out.println("Bean02构造器被调用");
}
}
- 配置类(使用@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();
}
}
- 测试
package test;
import configuration.Configuration01;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test01 {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(Configuration01.class);
}
}
- 测试结果,这表明这两个类的对象已经被实例化并由Spring进行管理了。
Bean01构造器被调用
Bean02构造器被调用
2. 结合@Component在Spring容器中注册Bean
- 创建实体类Bean03,并使用@Component进行注解
package bean;
import org.springframework.stereotype.Component;
@Component
public class Bean03 {
public Bean03(){
System.out.println("Bean03构造器被调用");
}
}
- 在配置类上增加一个包扫描注解@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();
}
}
- 测试
package test;
import configuration.Configuration01;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test01 {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(Configuration01.class);
}
}
- 结果
Bean03构造器被调用
Bean01构造器被调用
Bean02构造器被调用
为什么是先03在01,02呢?我想大概是Spring扫描时扫描到注解@ComponentScan然后扫描到Bean03上的@Component注解就对bean03进行了注册
3.结合@Import在Spring容器中注册Bean
对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配置的方式
随便说说看个乐
- @Configuration = @Service = @Controller = @Component
写成这样不过是为了方便阅读知道这是一个配置类,这是一个服务类,全部可用@Component进行代替,@Component目前理解是为了扫描,凡是被@Component注解了的类在被扫描后都会注册到Spring中。 - @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接管,被注册