當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
Spring系列(二):Bean注解用法介绍
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Spring系列(二):Bean注解用法介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                ? ? ? ?? ? ? ? ?
今天給大家介紹一下Spring中Bean注解的用法,后續的文章給大家介紹Sping其他注解用法,希望對大家日常工作能有所幫助!
1、首先創建一個maven項目引入spring依賴
<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>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;}@Overridepublic 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技術分享社區
個人博客網站:https://programmerblog.xyz
文章推薦程序員效率:畫流程圖常用的工具程序員效率:整理常用的在線筆記軟件遠程辦公:常用的遠程協助軟件,你都知道嗎?51單片機程序下載、ISP及串口基礎知識硬件:斷路器、接觸器、繼電器基礎知識
總結
以上是生活随笔為你收集整理的Spring系列(二):Bean注解用法介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: CoreAVC
 - 下一篇: vb.net中滚动条一直显示没有数据时也