關(guān)于spring web-mvc衍生注解
衍生注解
在web開發(fā)中,會根據(jù)webmvc的三層加購分層,這時候每層都有相同一個不同的注解名字,但功能都相同。
- Service層:@Service用于標(biāo)注業(yè)務(wù)層組件
- controller層:@Controller用于標(biāo)注控制層組件
- dao層:@Repository用于標(biāo)注數(shù)據(jù)訪問組件
- 組件歸類層,名字可以隨便定義,例如User層,@Component可以進(jìn)行標(biāo)注
注解:使用了注解之后,在容器中不需要注入bean,但要指定查找的的項目。
容器配置
<?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.yc.ch"/>
</beans>創(chuàng)建一個dao層和一個組件歸類層,文件樣式如下

User類
import com.yc.dao.Userdao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//該注釋表示注冊到容器中,直接由spring容器托管,
@Component
public class User {
//表示設(shè)置name的參數(shù)值為為Tom
//等價于 <property name="name" value="Tom"/>
@Value("Tom")
private String name;
private Userdao userdao;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Userdao getUserdao() {
return userdao;
}
public void setUserdao(Userdao userdao) {
this.userdao = userdao;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", userdao=" + userdao +
'}';
}
}
Userdao類
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
//該注釋表示注冊到容器中,直接由spring容器托管,
@Repository
public class Userdao {
//表示設(shè)置name的參數(shù)值為為游泳
//等價于 <property name="name" value="游泳"/>
@Value("游泳")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Userdao{" +
"name='" + name + '\'' +
'}';
}
}
spring容器配置
<?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">
<!--表示查找到com.yc下的所有層-->
<context:component-scan base-package="com.yc"/>
</beans>
創(chuàng)建測試類
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
User u=context.getBean("user",User.class);
System.out.println(u);
輸出
User{name=‘Tom’, userdao=null}
最終輸出的只有User類,但是Userdao層次卻直接顯示空,問題是沒有把Userdao的bean引入到User的bean中,所以在測試User類時顯示空。解決:使用使用注解 @Autowired方式匹配到Userdao中
將User類修改
import com.yc.dao.Userdao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
@Value("Tom")
private String name;
@Autowired
private Userdao userdao;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Userdao getUserdao() {
return userdao;
}
public void setUserdao(Userdao userdao) {
this.userdao = userdao;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", userdao=" + userdao +
'}';
}
}
修改spring容器配置
<?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.yc"/>
<context:annotation-config/>
</beans>
創(chuàng)建測試類,輸出
User{name=‘Tom’, userdao=Userdao{name=‘游泳’}}
@Component使用
@Component還可以加參數(shù),里面值相當(dāng)于注入bean中的id值,上面的案例默認(rèn)為User類的小寫user,如果設(shè)置為@Component(value = “user2”)或者@Component(“user2”),則將測試類改寫為
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
User u=context.getBean("user2",User.class);
System.out.println(u);
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot容器級后置處理器BeanDefinitionRegistryPostProcessor
這篇文章主要介紹了Springboot容器級后置處理器BeanDefinitionRegistryPostProcessor,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄
這篇文章主要介紹了關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot使用阿里OSS實現(xiàn)文件云存儲的方法
這篇文章主要介紹了SpringBoot使用阿里OSS實現(xiàn)文件云存儲,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
Hibernate想要實現(xiàn)雙向的關(guān)聯(lián)就必須在映射文件的兩端同時配置<one-to-one>,另外還要在主映射的一端采用foreign外鍵關(guān)聯(lián)屬性,下面我們就一起來看一下Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)方法:2016-06-06

