国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Java 如何使用@Autowired注解自動注入bean

瀏覽:103日期:2022-08-10 13:12:22
Java @Autowired注解自動注入bean

annotationWire.xml (一定記得配置context:annotation-config/)

<?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' xmlns:p='http://www.springframework.org/schema/p' 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'> <context:annotation-config/> <bean p:order='202020124546' /> <bean /></beans>

User類

package com.annotationWire.pojo;import lombok.Data;import org.springframework.beans.factory.annotation.Autowired;@Datapublic class User { private String name; @Autowired private Order order;}

Order類

package com.annotationWire.pojo;import lombok.Data;@Datapublic class Order { private String order;}

測試類

package com.annotationWire;import com.annotationWire.pojo.User;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAnnotation { @Test public void test(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext('annotationWire.xml');User student = applicationContext.getBean(User.class);System.out.println(student); }}java配置spring,無法@Autowired自動注入bean的問題

要在配置類上加上@ComponentScan

同時在RootConfigure和ServletConfig兩個類上scan的對象是不同的

ServletConfig是用來注冊DispatcherServlet的,它只是用來掃描controller層的

RootConfigure用來注冊ContextLoaderListener,他掃描的范圍是除了controller以外的bean,例如dao,service,bean實體。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 佛教| 满洲里市| 靖安县| 松潘县| 新干县| 红原县| 额尔古纳市| 财经| 虹口区| 东乌珠穆沁旗| 罗江县| 绥棱县| 井研县| 罗甸县| 威海市| 大同市| 宝鸡市| 克什克腾旗| 金昌市| 大名县| 临朐县| 时尚| 临夏县| 隆尧县| 千阳县| 凤凰县| 河源市| 耒阳市| 革吉县| 浮山县| 芜湖县| 明星| 莱阳市| 曲阳县| 双流县| 大港区| 会同县| 吐鲁番市| 蒙山县| 岑溪市| 红安县|