Spring使用IOC與DI實(shí)現(xiàn)完全注解開(kāi)發(fā)
方式一:@Component + @ComponentScan + @Value + @Autowired
首先還是pom文件,maven項(xiàng)目依賴必不可少。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>然后,寫兩個(gè)Java Bean,一個(gè)是Student學(xué)生類、另一個(gè)是School學(xué)校類。
由于不寫xml配置,所以在兩個(gè)類上方都要加上 @Component 注解,通過(guò)注解的方式將其交給Spring IOC容器管理,@Value注解則用于給8種基本數(shù)據(jù)類型以及String類型做依賴注入,@Autowired是針對(duì)引用類型的,這里不再多說(shuō)了。
package com.szh.bean;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*
*/
@Data
@Component
public class Student {
@Value("張起靈")
private String name;
@Value("20")
private Integer age;
@Autowired
private School school;
}package com.szh.bean;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*
*/
@Data
@Component
public class School {
@Value("北京大學(xué)")
private String name;
@Value("北京市海淀區(qū)")
private String address;
}下面要寫一個(gè)配置類,功能就是添加包掃描機(jī)制,確保上面那兩個(gè)@Component 注解修飾的Java Bean可以被Spring掃描并添加至容器中。
package com.szh.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
*
*/
@Configuration
@ComponentScan(basePackages = "com.szh.bean")
public class SpringConfig {
}最后是我們的測(cè)試類了。
package com.szh;
import com.szh.bean.Student;
import com.szh.config.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
*
*/
public class MyTest {
@Test
public void testIocAnnotation() {
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) ioc.getBean("student");
System.out.println(student);
}
}
方式二:@Configuration + @Bean
pom文件和方式一是一樣的。
下面是不一樣的Java Bean。
package com.szh.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Goods {
private String name;
private String info;
}package com.szh.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
/**
*
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
private Integer id;
private BigDecimal totalFee;
private Goods goods;
}然后是該方式對(duì)應(yīng)的配置類,采用@Bean實(shí)現(xiàn)。
package com.szh.config;
import com.szh.entity.Goods;
import com.szh.entity.Order;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.math.BigDecimal;
/**
*
*/
@Configuration
public class SpringConfig2 {
@Bean
public Goods goods() {
return new Goods("聯(lián)想-拯救者", "一款不錯(cuò)的游戲筆記本");
}
@Bean
public Order order(Goods goods) {
return new Order(1, new BigDecimal(9999), goods);
}
}最后是這種方式的測(cè)試類代碼。
@Test
public void testIocAnnotation2() {
ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig2.class);
Order order = (Order) ioc.getBean("order");
System.out.println(order);
System.out.println("IOC容器中存在的bean如下:");
String[] beanDefinitionNames = ioc.getBeanDefinitionNames();
for (String bean : beanDefinitionNames) {
System.out.println(bean);
}
}
到此這篇關(guān)于Spring使用IOC與DI實(shí)現(xiàn)完全注解開(kāi)發(fā)的文章就介紹到這了,更多相關(guān)Spring完全注解 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud?客戶端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法
Ribbon 是 Netflix 提供的一個(gè)基于 Http 和 TCP 的客戶端負(fù)載均衡工具,且已集成在 Eureka 依賴中,這篇文章主要介紹了SpringCloud?客戶端Ribbon負(fù)載均衡的實(shí)現(xiàn)方法,需要的朋友可以參考下2022-06-06
Springboot Tomcat APR模式詳解和實(shí)踐記錄
這篇文章主要介紹了Springboot Tomcat APR模式詳解和實(shí)踐記錄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-04-04
Spring源碼之事件監(jiān)聽(tīng)機(jī)制詳解(@EventListener實(shí)現(xiàn)方式)
這篇文章主要介紹了Spring源碼之事件監(jiān)聽(tīng)機(jī)制(@EventListener實(shí)現(xiàn)方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
SpringBoot實(shí)現(xiàn)向量數(shù)據(jù)庫(kù)優(yōu)化檢索的方案及示例
在Spring?Boot中實(shí)現(xiàn)RAG(Retrieval-Augmented?Generation)的增強(qiáng),可以從檢索優(yōu)化、生成優(yōu)化和系統(tǒng)架構(gòu)三個(gè)維度進(jìn)行改進(jìn),本文給大家介紹了具體實(shí)現(xiàn)方案及示例,需要的朋友可以參考下2025-02-02
Struts2 通過(guò)ognl表達(dá)式實(shí)現(xiàn)投影
這篇文章主要介紹了Struts2 通過(guò)ognl表達(dá)式實(shí)現(xiàn)投影,具有一定參考價(jià)值,需要的朋友可以了解下。2017-09-09

