iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring注解@Configuration与@Bean注册组件的使用详解
  • 707
分享到

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

2024-04-02 19:04:59 707人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录原始spring开发Person.javapom.xmlbean.xmlPersonTest.java注解Spring开发原始Spring开发 Person.java 准备Per

原始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基本依赖:

<project xmlns="Http://Maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jektong</groupId>
	<artifactId>maven01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>maven01</name>
	<description>maven01</description>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.12.RELEASE</version>
		</dependency> 
	</dependencies>
</project>

bean.xml

在没有使用Spring注解开发之前,我们通常会通过一个xml配置文件(bean.xml)去将我们需要使用的对象通过Bean的方式去注入到Spring容器中。

下面就是将Person作为对象注入Spring容器中:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="person" class="com.jektong.spring.Person">
		<property name="name" value="zs"></property>
		<property name="age" value="18"></property>
	</bean>
</beans>

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) 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>标签中的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) {	
		ApplicationContext 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。

到此这篇关于Spring注解@Configuration与@Bean注册组件的使用详解的文章就介绍到这了,更多相关Spring @Configuration内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Spring注解@Configuration与@Bean注册组件的使用详解

本文链接: https://www.lsjlt.com/news/151415.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作