詳解Spring如何更簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象
前置工作
在spring-config.xml添加如下配置:配置bean的掃描根路徑:只有當(dāng)前目錄下的類才會(huì)掃描是否添加了注解,如果添加了注解,就將這些加了注解的類,存放到IoC容器中。
<?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:content="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">
<content:component-scan base-package="com.java.demo">
</content:component-scan>
</beans>
更加簡(jiǎn)單的存儲(chǔ)Bean對(duì)象
兩種方法:
1.通過類注解實(shí)現(xiàn)Bean對(duì)象的存儲(chǔ);
- @Controller:控制器,驗(yàn)證用戶請(qǐng)求的數(shù)據(jù)正確性(安保系統(tǒng))
- @Service:服務(wù)層,編排和調(diào)度具體執(zhí)行方法的(客服中心)
- @Repository:持久層,和數(shù)據(jù)庫(kù)進(jìn)行交互,等同于DAO(Data Access Object) 數(shù)據(jù)訪問層
- @Component:組件(工具類)
- @Configuration:配置項(xiàng)(配置項(xiàng)目中的一些配置)
2.通過方法注解實(shí)現(xiàn)Bean對(duì)象的存儲(chǔ)
@Bean路徑
使用@Controller(控制器存儲(chǔ))
package com.java.demo;
import org.springframework.stereotype.Controller;
@Controller(value = "userinfo")
public class User {
public void sayHi() {
System.out.println("Hi,User~");
}
}import com.java.demo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@SuppressWarnings({"all"})
public class App {
public static void main(String[] args) {
// 1. 得到容器對(duì)象
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
// 2. 得到 Bean 對(duì)象
User user = context.getBean("userinfo", User.class);
// 3. 使用 Bean 對(duì)象
user.sayHi();
}
}
import com.java.demo.Student;
import com.java.demo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@SuppressWarnings({"all"})
public class App {
public static void main(String[] args) {
// 1. 得到容器對(duì)象
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
// 2. 得到 Bean 對(duì)象
// User user = context.getBean("userinfo", User.class);
Student student = context.getBean("student", Student.class);
// 3. 使用 Bean 對(duì)象
// user.sayHi();
student.sayHi();
}
}
原因就是:student沒有加注解。

使用注解和XML是可以一起使用的。
類注解存儲(chǔ)Bean命名問題
默認(rèn)類名首字母小寫就能獲取到Bean對(duì)象。

獲取不到Bean對(duì)象


使用原類名,可以獲取到Bean對(duì)象


我們?cè)讷@取Bean對(duì)象時(shí),傳入名稱時(shí),一般分為兩種情況:
默認(rèn)情況下:使用原類名首字母小寫就能讀取到Bean對(duì)象
特殊情況:如果不滿足首字母大寫和第二個(gè)字母小寫的情況,那么使用原類名獲取


Java項(xiàng)目標(biāo)準(zhǔn)分層

五大類注解之間的關(guān)系




方法注解@Bean
a.使用注意事項(xiàng):@Bean 注解 必須要配合五大類注解一起使用
package com.java.demo.model;
import java.time.LocalDateTime;
@SuppressWarnings({"all"})
/**
* 普通的文章實(shí)體類
*/
public class ArticleInfo {
private int aid;
private String title;
private String content;
private LocalDateTime createtime;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public LocalDateTime getCreatetime() {
return createtime;
}
public void setCreatetime(LocalDateTime createtime) {
this.createtime = createtime;
}
@Override
public String toString() {
return "ArticleInfo{" +
"aid=" + aid +
", title='" + title + '\'' +
", content='" + content + '\'' +
", createtime=" + createtime +
'}';
}
}package com.java.demo;
import com.java.demo.model.ArticleInfo;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import java.time.LocalDateTime;
@SuppressWarnings({"all"})
@Controller
public class Articles {
@Bean // 將當(dāng)前方法返回的對(duì)象存儲(chǔ)到 IoC 容器
public ArticleInfo articleInfo() {
// 偽代碼
ArticleInfo articleInfo = new ArticleInfo();
articleInfo.setAid(1);
articleInfo.setTitle("今天周幾?");
articleInfo.setContent("今天周一!");
articleInfo.setCreatetime(LocalDateTime.now());
return articleInfo;
}
}import com.java.demo.UConfig;
import com.java.demo.example.Teacher;
import com.java.demo.model.ArticleInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@SuppressWarnings({"all"})
public class App {
public static void main(String[] args) {
// 1. 得到容器對(duì)象
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
// 2. 得到 Bean 對(duì)象
// User user = context.getBean("userinfo", User.class);
// Student student = context.getBean("student", Student.class);
// UserService userService = context.getBean("userService",UserService.class);
// Teacher teacher = context.getBean("teacher",Teacher.class);
// UConfig uConfig = context.getBean("UConfig",UConfig.class);
ArticleInfo articleInfo = context.getBean("articleInfo", ArticleInfo.class);
// 3. 使用 Bean 對(duì)象
// user.sayHi();
// student.sayHi();
// userService.sayHi();
// teacher.sayHi();
// uConfig.sayHi();
System.out.println(articleInfo);
}
}
b.@Bean 獲取時(shí)注意事項(xiàng):@Bean的默認(rèn)命名=方法名。
重命名@Bean的幾種方式
方式1:@Bean("aaa")
方法2:@Bean(name = "aaa")
方式3:@Bean(value = "aaa")
重命名擴(kuò)展:@Bean 支持指定多個(gè)名稱@Bean(value = {“aaa”})默認(rèn)命名注意事項(xiàng):當(dāng)@Bean重命名之后,那么默認(rèn)的使用方法名獲取Bean對(duì)象的方式就不能使用了。


以上就是詳解Spring如何更簡(jiǎn)單的讀取和存儲(chǔ)對(duì)象的詳細(xì)內(nèi)容,更多關(guān)于Spring讀取存儲(chǔ)對(duì)象的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis Properties及別名定義實(shí)例詳解
這篇文章主要介紹了MyBatis Properties及別名定義實(shí)例詳解,需要的朋友可以參考下2017-08-08
java實(shí)現(xiàn)學(xué)生成績(jī)檔案管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生成績(jī)檔案管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
如何禁用IntelliJ IDEA的LightEdit模式(推薦)
這篇文章主要介紹了如何禁用IntelliJ IDEA的LightEdit模式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Java Swing JToggleButton開關(guān)按鈕的實(shí)現(xiàn)
這篇文章主要介紹了Java Swing JToggleButton開關(guān)按鈕的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
springboot實(shí)現(xiàn)定時(shí)任務(wù)的四種方式小結(jié)
本文主要介紹了springboot實(shí)現(xiàn)定時(shí)任務(wù)的四種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

