国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Spring注解的步驟

2019-11-15 01:10:03
字體:
來源:轉載
供稿:網友
SPRing注解的步驟

Spring框架提供DI(屬性注解)和IOC(類/Bean的注解)注解。

注解:標注、注入和解析、解釋;標注和解釋一部分代碼的作用在框架中:就是配置文件的另外一種實現方式@Type、@Taget;減少配置文件的代碼量;提高開發效率

spring注解的步驟:

1、引入命名空間:

xmlns:context="http://www.springframework.org/schema/context"            http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-2.5.xsd

2、設置注解解析器或者設置類掃描器(兩個設置一個即可)

2、設置注解解析器或者設置類掃描器(兩個設置一個即可)        1、 <!--             添加注解解析器:用于依賴注入使用;不具備類掃描功能         -->        <context:annotation-config/>                2、<!--         引入注解類掃描器        指定哪些類要加載進入spring容器中        哪些類需要交給spring進行管理        功能:注解解析器功能            類掃描的功能         -->         <context:component-scan base-package="com.msit.spring.IOC.Annotation.IOC"></context:component-scan>             3、在要引入的類上標注注解        @Component            public class HelloSpring {            }            ===            <bean id="helloSpring" Class="com.msit.spring.IOC.Annotation.IOC.HelloSpring"></bean>        4、在要注入的依賴對象(類的屬性)上加上注解標注        @Resource        public Student stu;

DI(屬性注解)注解原理:1、加載spring配置文件;遇到Bean創建Bean2、查找Bean的屬性;如果有注解標注的屬性;則利用注解解析器進行Spring容器的掃描如果@Resource(name="stu")name不為空則將name的值和spring容器中的Bean的ID進行匹配;匹配成功則注入如果匹配不成功則報錯如果@Resource name屬性為空“”則將會把依賴對象的名稱作為默認值和spring容器中的Bean的ID進行匹配;匹配成功則注入@Resourcepublic Student stu;就相當于@Resource(name="stu")如果名稱匹配不成功;則會按照依賴對象的類型和Bean的類型(Class)進行匹配;匹配成功則注入如果匹配不成功則報錯

第一步:配置applicationContext-annotation-di.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd">         <!--         添加注解解析器     -->    <context:annotation-config/>        <bean id="stus" class="com.msit.spring.IOC.Annotation.DI.Student">        <property name="name" value="張三"></property>    </bean>    <bean id="hellospring" class="com.msit.spring.IOC.Annotation.DI.HelloSpring"></bean></beans>      

第二步:Student 實體類和測試類

package com.mist.IOC.entiy;/* * 實體類 */public class Studentds {    private String name;//姓名    private String age;//年紀    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    }
package com.mist.IOC.Test;import javax.annotation.Resource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.mist.IOC.entiy.Studentds;public class HellowSpring {    //引入依賴對象@Resourcepublic Studentds stu;public Studentds getStu() {    return stu;}public void setStu(Studentds stu) {    this.stu = stu;}public static void main(String[] args) {    ApplicationContext context=new ClassPathXmlApplicationContext("com/mist/applicationContext2.xml");    HellowSpring te=(HellowSpring) context.getBean("hellowSpring");    System.out.println(te.getStu().getName());}}

效果圖:

IOC(類/Bean的注解)注解原理:1、加載配置文件;當讀取到<context:component-scan base-package="com.msit.spring.IOC.Annotation.IOC"></context:component-scan>回去掃描com.msit.spring.IOC.Annotation.IOC這個包下的所有標注的類(@component)裝配到spring容器中@Componentpublic class HelloSpring {}===<bean id="helloSpring" Class="com.msit.spring.IOC.Annotation.IOC.HelloSpring"></bean>@Component value屬性默認值為“”;spring容器會將類的名稱首字母小寫其他不變作為Bean的ID如果不為空@Component("hellospring")會以設置的值為Bean的ID

第一步:加載applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd           http://www.springframework.org/schema/context           http://www.springframework.org/schema/context/spring-context-2.5.xsd">    <!--         引入注解類掃描器        指定哪些類要加載進入spring容器中        哪些類需要交給spring進行管理        功能:注解解析器功能            類掃描的功能     -->     <context:component-scan base-package="com.msit.spring.IOC.Annotation.IOC"></context:component-scan>    </beans>   

第二步:Student 實體類和測試類

package com.mist.spring.DI;import org.springframework.stereotype.Component;@Component("stu")public class Strudents {    private String name="王五";    private String passWord;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    }
package com.mist.spring.DI;import javax.annotation.Resource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;@Component("testSpring")public class TestSpring {    //引入依賴對象    @Resource    //@Autowired//按照類型查找    //@Qualifier("stus")//按照名稱查找    public Strudents student;    public Strudents getStudent() {        return student;    }    public void setStudent(Strudents student) {        this.student = student;    }        public static void main(String[] args) {                ApplicationContext context=new ClassPathXmlApplicationContext("com/mist/spring/DI/applicationContext.xml");        TestSpring Hello=(TestSpring) context.getBean("testSpring");        System.out.print(Hello.getStudent().getName());    }}

效果圖:

注:

XML和注解對比1、XML代碼量多;開發效率低;可讀性強;執行效率高2、注解配置簡單;開發效率高;可讀性低;執行效率相對于XML偏低;但是對于整個系統而言;影響不是太大

  

您可以通過點擊 右下角 的按鈕 來對文章內容作出評價, 也可以通過左下方的 關注按鈕 來關注我的博客的最新動態。 如果文章內容對您有幫助, 不要忘記點擊右下角的 推薦按鈕 來支持一下哦   如果您對文章內容有任何疑問, 可以通過評論或發郵件的方式聯系我: 2276292708@QQ.com歡迎加入java ios 安卓技術交流群: 306431857,期待你的加入。如果需要轉載,請注明出處,謝謝!!

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乾安县| 锡林浩特市| 五华县| 綦江县| 唐山市| 延津县| 东乌| 香河县| 勃利县| 肥东县| 乌兰浩特市| 西安市| 哈密市| 屏山县| 兴安县| 聊城市| 屏东市| 长葛市| 武威市| 泗水县| 石泉县| 博湖县| 永康市| 乌鲁木齐县| 宁强县| 股票| 长丰县| 泰顺县| 龙川县| 巫山县| 大兴区| 湘乡市| 旺苍县| 泸定县| 崇信县| 大冶市| 乌拉特前旗| 南通市| 明星| 临漳县| 山东省|