Java之Spring注解開發(fā)案例詳解
- 在Spring4之后,要使用注解開發(fā),必須要保證aop的包導入了

- 使用注解需要導入context約束,增加注解的支持!
<?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
https://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="com.example.springannotation" />
<context:annotation-config></context:annotation-config>
<!-- <bean id="cat" class="com.example.springannotation.dao.Cat"/>
<bean id="people" class="com.example.springannotation.dao.People"/>-->
</beans>
注解的支持:
//@Component 等價于<bean id="pepople" class="com.example.springannotation.dao.People" />
@Component
public class People {
@Autowired(required = false)
@Value("1235") //相當<property name="id " value="1235"/>
private int id;
@Autowired(required = false)
private String name ="ming";
@Value("qing") //相當<property name="name " value="qing"/>
public void setName(@Nullable String name) {
this.name = name;
}
}
衍生的注解
@Component有幾個衍生注解,我們在web開發(fā)中,會按照mvc三層架構(gòu)分層!
- dao 【@Repository】
- service【@Service】
- controller【@Controler】
這四個注解功能都是一樣的,都是代表將某個類注冊到Spring中,裝配Bean。
@Scope("singleton") //singleton:標識單例模式,prototype:標識原型模式 、request:標識請求模式、session:標識會話模式
xml 與注解:
- xml更加萬能,適用于任何場合!維護簡單方便。注解不是自己類使用不了,維護相對復雜!
xml與注解最佳實踐:
- xml 用來管理bean;
- 注解只負責完成屬性的注入;
- 我們在使用的過程中,只需要注意一個問題:必須讓注解生效,就需要開啟注解的支持
<!--指定要掃描的包,這個包下的注解就會生效-->
<context:component-scan base-package="com.example.springannotation" />
<context:annotation-config></context:annotation-config>
JAVA的方式配置Spring
- @Configuration 這個也會spring容器托管,注冊到容器中,因為他本來就是一個Component,
- @Configuration代表這是一個配置類,就和我們之前看的beans.xml
// 配置類 代替 beans.xml
import com.example.springannotation.dao.Cat;
import com.example.springannotation.dao.People;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("com.example.springannotation")
@Import(WwConfig.class) //引入第二個配置
public class AppConfig {
//注朋一個bean 相當于當于我們之前寫的一個bean標簽
//這個方法的名字,就相當于bean標簽中的id屬性
//這個方法的返回價,就和當了bean標簽中的class屬性
@Bean
public People getPeople(){
return new People();
}
@Bean
public Cat getCat(){
return new Cat();
}
}
import org.springframework.context.annotation.Configuration;
@Configuration
public class WwConfig {
}
//測試類
import com.example.springannotation.config.AppConfig;
import com.example.springannotation.dao.People;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootTest
class SpringannotationApplicationTests {
@Test
void contextLoads() {
如果完全使用了配置類方式做,
// 我們就只能通過 AnnotationConfig 上下文來獲取容器,通過配置類的class對象加載!
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
People people = (People) context.getBean("getPeople");
System.out.println(people.toString());
}
}
到此這篇關(guān)于Java之Spring注解開發(fā)案例詳解的文章就介紹到這了,更多相關(guān)Java之Spring注解開發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot緩存抽象@Cacheable與緩存管理器配置方法
SpringBoot的緩存抽象通過@Cacheable注解和CacheManager接口,提供了靈活高效的緩存管理,本文詳細介紹了@Cacheable的使用技巧、緩存管理器配置方法、自定義鍵生成策略以及緩存同步與失效機制,幫助開發(fā)者構(gòu)建高效的緩存策略,優(yōu)化應用性能,感興趣的朋友一起看看吧2025-03-03
使用mybatis-plus的insert方法遇到的問題及解決方法(添加時id值不存在異常)
這篇文章主要介紹了使用mybatis-plus的insert方法遇到的問題及解決方法(添加時id值不存在異常),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 創(chuàng)建多模塊項目的超詳細教程
這篇文章主要介紹了IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 創(chuàng)建多模塊項目的教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
spring-boot整合Micrometer+Prometheus的詳細過程
這篇文章主要介紹了springboot整合Micrometer+Prometheus的詳細過程,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-05-05
Java8新特性之lambda的作用_動力節(jié)點Java學院整理
我們期待了很久lambda為java帶來閉包的概念,但是如果我們不在集合中使用它的話,就損失了很大價值?,F(xiàn)有接口遷移成為lambda風格的問題已經(jīng)通過default methods解決了,在這篇文章將深入解析Java集合里面的批量數(shù)據(jù)操作解開lambda最強作用的神秘面紗。2017-06-06

