Spring是在进行web开发中必不可少的一个框架,而基于传统的xml文件配置bean的方式太过繁琐,降低了开发的效率。从Spring2.5以后注解开发的出现大大简化日常开发中繁琐的配置。接下来就通过实例分析Spring中各种注解的用法。
如果不采用注解开发,通常进行组件注册是首先新建一个实体类:
package com.xinyi.bean; public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } public Person(String name, Integer age) { super(); this.name = name; this.age = age; } public Person() { } } 复制代码
然后在resource文件夹下新建一个配置文件beans.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:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <bean id="Person" class="com.xinyi.bean.Person"> <property name="name" value="新一"></property> <property name="age" value="18"></property> </bean> </beans> 复制代码
然后新建一个测试类:
package com.xinyi; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xinyi.bean.Person; public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Person personbean = (Person) applicationContext.getBean("Person"); System.out.println(personbean); } } 复制代码
输出结果如下:
Person [name=新一, age=18] 复制代码
接下来通过采用注解的方式进行开发,首先介绍两个注解@Configuration和@Bean。
@Configuration
从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
@Configuration注解的配置类有如下要求:
@Configuration不可以是匿名类;
@Configuration不可以是final类型;
- 嵌套的configuration必须是静态类。
@Bean
@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。@Bean给容器中注入一个bean,类型为返回值的类型,默认方法名作为id。
通过注解开发的方式在spring容器中注册组件过程,首先声明一个实体类,同上。然后声明一个配置类:
package com.xinyi.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.xinyi.bean.Person; //配置类等同于以前的配置文件 //@Configuration等同于告诉spring这是一个配置类 @Configuration public class MyConfig { //@Bean给容器中注入一个bean,类型为返回值得类型,id默认方法名作为id //注入person的值 //person222为自定义bean的名字 @Bean("person222") public Person person111() { return new Person("xinyi、",19); } } 复制代码
测试类测试:
package com.xinyi; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.xinyi.bean.Person; import com.xinyi.config.MyConfig; public class MainTest { public static void main(String[] args) { //以前是返回配置文件的位置,这里是返回配置类的位置 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class); Person person = applicationContext.getBean(Person.class); System.out.println(person); //返回bean类型 String[] persontype = applicationContext.getBeanNamesForType(Person.class); for(String nameString :persontype) { System.out.println(nameString); } } } 复制代码
输出结果如下:
Person [name=xinyi、, age=19] person222 复制代码
以上就是如何通过采用@Configuration和@Bean注解的方式注册组件,与传统的基于配置文件开发来比较减少了繁琐的配置文件,灵活性更高。