Spring注解@Configuration与@Bean注册组件的使用详解

目录原始Spring开发Person.javapom.xmlbean.xmlPersonTest.java注解Spring开发
原始Spring开发
Person.java
准备Person.java类:
package com.jektong.spring;
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
pom.xml
在pom文件导入Spring基本依赖:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<dependency>
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<dependency>
bean.xml
在没有使用Spring注解开发之前,我们通常会通过一个xml配置文件(bean.xml)去将我们需要使用的对象通过Bean的方式去注入到Spring容器中。
下面就是将Person作为对象注入Spring容器中:
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springframework.org/schema/p"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:p="http://springframework.org/schema/p"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd">
PersonTest.java
使用一个PersonTest.java测试类测试:
package com.jektong.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonTest {
public static void main(String[] args) {
// 加载配置文件,此文件放在类路径下。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
// 获取bean.xml文件中注入的Person对象,并输出。
Person bean = (Person)http:// applicationContext.getBean("person");
System.out.println(bean);
}
}
输出结果如下:
注解Spring开发
舍弃上面的bean.xml文件,通过注解的方式将xml文件转换成配置类,建立PersonConfig配置类:
package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.jektong.spring.Person;
@Configuration // 告诉spring这是配置类相当于配置文件bean.xml
public class PersonConfig {
// 这是注入到spring容器的对象ID
// 括号内指定唯一ID,不指定则是默认以方法名为唯一ID,相当于:
@Bean("person01")
public Person person() {
return new Person("李四",21);
}
}
测试使用AnnotationConfigApplicationContext类读取注解:
package com.jektong.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.jektong.config.PersonConfig;
public class PersonTest {
public static void main(String[] args) {
ApplicationContehttp://xt ac = new AnnotationConfigApplicationContext(PersonConfig.class);
Person bean = ac.getBean(Person.class);
System.out.println(bean);
// 查看BEAN的id
String[] beanDefinitionNames = ac.getBeanNamesForType(Person.class);
for (int i = 0; i < beanDefinitionNames.length; i++) {
System.out.println("beanid为:"+ beanDefinitionNames[i]);
}
}
}
输出如下:
@Configuration与@Bean作用总结
@Configuration
相当于spring中的XML配置文件,将此XML文件替代成配置类,声明在类上。
@Bean
相当于XML文件中所配置的各个Bean对象,现声明在方法上,默认以方法名作为注入的Bean的id。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~