今天给大家介绍一下Spring中Bean注解的用法,后续的文章给大家介绍Sping其他注解用法,希望对大家日常工作能有所帮助!
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency></dependencies>
package com.spring.bean; public class Person { private String name; private Integer age; private String address; public Person(String name, Integer age, String address) { this.name = name; this.age = age; this.address = address; } public Person() { } 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; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", address='" + address + '\'' + '}'; }}
package com.spring.config; import com.spring.bean.Person;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; @Configurationpublic class TestBeanConfig { /*@Bean作用是注册一个Bean,类型为返回值的类型,默认是使用方法名作为id,可以自己定义 * value 可以自定义id,默认和方法名一致 * */ @Bean(value = "person1") public Person person() { return new Person("小王", 35, "北京"); }}
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="java"></context:component-scan> <bean id="person" class="com.spring.bean.Person"> <property name="name" value="小明"></property> <property name="age" value="30"></property> <property name="address" value="苏州"></property> </bean></beans>
package com.spring.test; import com.spring.bean.Person;import com.spring.config.TestBeanConfig;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestBean { public static void main(String[] args) { //配置文件方式 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("person.xml"); Person bean = (Person) applicationContext.getBean("person"); System.out.println("配置方式:"); System.out.println(bean); // 注解方式 AnnotationConfigApplicationContext 注解的方式获取spring容器 AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestBeanConfig.class); Person annotationPerson = (Person) annotationContext.getBean("person1"); System.out.println("注解方式:"); System.out.println(annotationPerson); // 用来获取Spring容器中指定类型的所有JavaBean的名称 String[] beanNamesForType = annotationContext.getBeanNamesForType(Person.class); for (String item : beanNamesForType) { System.out.println(item); } } }
6、运行效果:
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
添加我为好友,拉您入交流群!
请使用微信扫一扫!