成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring 系列之 Bean 注解用法介紹

開發 架構
今天給大家介紹一下Spring中Bean注解的用法,后續的文章給大家介紹Sping其他注解用法,希望對大家日常工作能有所幫助!

今天給大家介紹一下Spring中Bean注解的用法,后續的文章給大家介紹Sping其他注解用法,希望對大家日常工作能有所幫助!

1、首先創建一個maven項目引入spring依賴

org.springframework spring-context 5.3.9

2、新建一個person.java 實體類

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 + '\'' +
'}';
}
}

3、新建配置類 TestBeanConfig.java

package com.spring.config;

import com.spring.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TestBeanConfig {
/*@Bean作用是注冊一個Bean,類型為返回值的類型,默認是使用方法名作為id,可以自己定義
* value 可以自定義id,默認和方法名一致
* */
@Bean(value = "person1")
public Person person() {
return new Person("小王", 35, "北京");
}
}

4、resources 創建配置文件

<?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>

5、新建測試類TestBean.java 具體展示注解方式和配置方式的示例

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、運行效果:

責任編輯:武曉燕 來源: IT技術分享社區
相關推薦

2022-02-20 07:28:13

Spring注解用法

2024-02-23 10:33:34

SpringBean容器

2022-03-03 07:34:31

注解容器作用域

2022-05-27 08:25:55

容器Spring

2021-09-16 10:05:09

鴻蒙HarmonyOS應用

2023-04-28 12:37:59

Spring@Bean使用方式

2023-04-23 09:50:50

@BeanSpring

2016-09-23 10:34:32

JUnitGradleAgile

2016-09-21 14:17:11

JUnit架構API

2023-08-04 08:25:03

客戶配置Spring

2021-07-09 07:52:34

SpringContextEventListen

2019-07-02 11:01:35

SpringBean配置

2024-01-03 07:57:11

高級參數PowerShellVerbose 參數

2023-09-21 07:06:17

PSDriveProvider

2022-12-22 08:14:54

2022-05-30 11:17:44

Spring容器配置

2022-06-09 07:27:14

JavaSpring容器

2020-10-14 06:23:54

SpringBean實例化

2025-02-28 08:16:14

Spring框架注解

2016-09-23 10:20:22

JUnit擴展模型Extension
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 综合第一页 | 国产羞羞视频在线观看 | 91麻豆精品国产91久久久久久久久 | 亚洲精品1区 | 国产免费一区二区 | 欧美福利在线 | 亚洲精品一区二区在线观看 | 九九色九九 | 99久久久国产精品免费消防器 | 亚洲精视频 | 在线亚洲精品 | 亚洲欧美久久 | 国产乱码久久久 | 亚洲综合色 | 精品久久久久久国产 | 精品久久香蕉国产线看观看亚洲 | 久久蜜桃av一区二区天堂 | 日韩成人在线观看 | 国产精品久久9 | 国产成人影院 | 欧美日韩视频 | 国产区免费视频 | 成人三级视频在线观看 | 99久久久无码国产精品 | www亚洲成人 | 天天天操操操 | 亚洲一区二区在线免费观看 | 国产精彩视频一区 | 亚洲逼院 | 欧美激情久久久 | 亚洲国产精品一区二区久久 | 在线观看av网站永久 | 国产成人精品一区二区 | 国产一区二区三区四区三区四 | 美国a级毛片免费视频 | 国产成人短视频在线观看 | 97国产在线视频 | 草草草久久久 | 久久久久久国产 | 在线观看黄免费 | 超碰人人人人 |