Spring框架對于Bean的管理詳解
什么是Bean管理
bean管理指的是如下的兩個操作。
1.創(chuàng)建對象
2.注入屬性
Bean管理操作的兩種方式
1.基于xml配置文件的方式實現(xiàn)
2.基于注解方式實現(xiàn)
基于注解的方式實現(xiàn)Bean管理和注入屬性(常用)
1.什么是注解
①:注解是代碼特殊標記,格式:@注解名稱(屬性名稱=屬性值,屬性名稱=屬性值…)
②:使用注解,注解作用在類上面,方法上面,屬性上邊
③:使用注解的目的:簡化XML配置
2.Spring針對Bean管理中創(chuàng)建對象提供的注解
@Component 普通的類
@Controller 表現(xiàn)層
@Service 業(yè)務層
@Repository 持久層
上邊四個功能一樣,都可以用來創(chuàng)建bean實例
3.用注解的方式創(chuàng)建對象
①:編寫接口和實現(xiàn)類
package com.qcby.testanno;
public interface UserService {
public void hello();
}②:在需要管理的類上添加@Component注解
package com.qcby.testanno;
import org.springframework.stereotype.Component;
/* <bean id="us" class="UserServiceImpl"/> */
/**
* 組件,作用:把當前類使用IOC容器進行管理,如果沒有指定名稱,默認使用類名,首字母是小寫。
* userServiceImpl。或者自己指定名稱
**/
@Controller(value="us")
public class UserServiceImpl implements UserService {
public void hello() {
System.out.println("使用注解,方便吧!");
}
}③:編寫配置文件,重點是開啟注解掃描。
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟注解掃描 com.qcby所有的包中的所有的類-->
<context:component-scan base-package="com.qcby"/>
</beans>④:測試
package com.qcby.test;
import com.qcby.testanno.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
@Test
public void run1(){
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContextanno.xml");
UserService us = (UserService) ac.getBean("us");
us.hello();
}
}4.用注解的方實現(xiàn)屬性注入
@Value 用于注入普通類型(String,int,double等類型)
@Autowired 默認按類型進行自動裝配(引用類型)
@Qualifier 不能單獨使用必須和@Autowired一起使用,強制使用名稱注入
@Resource Java提供的注解,也被支持。使用name屬性,按名稱注入
具體的代碼如下:用注解的方式創(chuàng)建對象和賦值
// 默認當前類名就是ID名稱,首字母小寫
@Component(value = "car")
// @Controller
// @Service(value = "c")
// @Repository(valu = "c")
public class Car {
// 注解注入值,屬性set方法是可以省略不寫的。
// 只有一個屬性,屬性的名稱是value,value是可以省略不寫的
@Value("大奔2")
private String cname;
@Value(value = "400000")
private Double money;
// 也不用提供set方法
// 按類型自動裝配的注解,和id名稱沒有關系
@Qualifier("person")
@Autowired //注意:(所引用的Car類里面必須要寫:類聲明@Component(value="person") )
// 注入引用數(shù)據(jù)類型:不需要寫值,根據(jù)類型自動匹配
//@Resource(name="person") //是jdk提供,按照名稱進行注入,只能在引用數(shù)據(jù)類型當中發(fā)使用
private Person person;
@Override
public String toString() {
return "Car{" +
"cname='" + cname + '\'' +
", money=" + money +
", person=" + person +
'}';
}
}注意聲明:@Component(value = “person”)
@Component(value = "person")
public class Person {
@Value("張三")
private String pname;
@Override
public String toString() {
return "Person{" +
"pname='" + pname + '\'' +
'}';
}
}測試:用注解的方式創(chuàng)建對象和賦值
@Test
public void run1(){
// 工廠
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
// 獲取對象
Car car = (Car) ac.getBean("car");
System.out.println(car);
}到此這篇關于Spring框架對于Bean的管理詳解的文章就介紹到這了,更多相關Spring Bean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Prometheus 入門教程之SpringBoot 實現(xiàn)自定義指標監(jiān)控
這篇文章主要介紹了Prometheus 入門教程之SpringBoot 實現(xiàn)自定義指標監(jiān)控,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
SpringBoot中集成企業(yè)微信機器人實現(xiàn)運維報警的示例
本文主要介紹了SpringBoot中集成企業(yè)微信機器人實現(xiàn)運維報警,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
Spring學習筆記1之IOC詳解盡量使用注解以及java代碼
這篇文章主要介紹了Spring學習筆記1之IOC詳解盡量使用注解以及java代碼 的相關資料,需要的朋友可以參考下2016-07-07
springboot+thymeleaf國際化之LocaleResolver接口的示例
本篇文章主要介紹了springboot+thymeleaf國際化之LocaleResolver的示例 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

