最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring注解與P/C命名空間超詳細(xì)解析

 更新時(shí)間:2022年11月22日 15:17:55   作者:wei_shuo  
Spring注解方式減少了配置文件內(nèi)容,更加便于管理,并且使用注解可以大大提高了開發(fā)效率!注解本身是沒有功能的,和xml一樣,注解和xml都是一種元數(shù)據(jù),元數(shù)據(jù)即解釋數(shù)據(jù)的數(shù)據(jù),也就是所謂的配置

注解實(shí)現(xiàn)自動(dòng)裝配

@Autowire注解

@Autowire注解,自動(dòng)裝配通過類型,名字如果Autowire不能唯一自動(dòng)裝配上屬性,

則需要通過@Qualifier(value=“xxx”)

配置:

導(dǎo)入約束

<?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 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context.xsd
        ">

開啟注解支持

<context:annotation-config/>

實(shí)現(xiàn):

Dog/Cat類和方法的實(shí)現(xiàn)還是不變

People類

  • 實(shí)現(xiàn)@Autowire注解、可以在屬性上使用、也可以在set方式使用
  • 編寫@Autowire注解后,set方法可以省略不寫
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
}

beans.xml

    <bean id="cat" class="com.wei.pojo.Cat"/>
    <bean id="dog" class="com.wei.pojo.Dog"/>
    <bean id="people" class="com.wei.pojo.People"/>

@Nullable 字段標(biāo)記了這個(gè)注解,說明這個(gè)字段可以為null

可以在帶參構(gòu)造中使用@Nullable

    public People(@Nullable String name) {
        this.name = name;
    }

@Qualifier注解

指定一個(gè)唯一的bean對象注入

People類

public class People {
    @Autowired
    @Qualifier(value = "cat111")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog222")
    private Dog dog;
    private String name;
}

beans.xml

    <bean id="cat" class="com.wei.pojo.Cat"/>
    <bean id="cat111" class="com.wei.pojo.Cat"/>
    <bean id="dog" class="com.wei.pojo.Dog"/>
    <bean id="dog222" class="com.wei.pojo.Dog"/>
    <bean id="people" class="com.wei.pojo.People"/>

@Resource注解

默認(rèn)按照名字裝配Bean,即會(huì)按照name屬性的值來找到具有相同id的Bean Definition 并注入

People類

public class People {
    @Resource(name = "cat2")
    private Cat cat;
    @Resource
    private Dog dog;
    private String name;
}

beans.xml

<bean id="cat1" class="com.wei.pojo.Cat"/>
<bean id="cat2" class="com.wei.pojo.Cat"/>
<bean id="dog" class="com.wei.pojo.Dog"/>
<bean id="people" class="com.wei.pojo.People"/>

總結(jié):

  • @Autowire 通過byType方式實(shí)現(xiàn),而且必須要求對象存在,不能為null
  • @Resource 默認(rèn)通過byname的方式實(shí)現(xiàn),如果找不到名字,則通過byType實(shí)現(xiàn),都不行則報(bào)錯(cuò)

@Component

放在類上,說明這個(gè)類被Spring管理了,等同于bean配置

Java類

此處使用注解@Component @Value 則 beans.xml則無需書寫bean

//等價(jià)于<bean id="user" class="com.wei.pojo.User       Component:組件
@Component
public class User {
    @Value("wei_shuo")  //相當(dāng)于 <property name="name" value="wei_shuo"/>
    public String name;
}

@Component的衍生注解,在web開發(fā)中,按照MVC三層架構(gòu)分層

四個(gè)注解功能、作用相同,都是代表將某個(gè)類注冊到Spring中、裝配Bean

  • pojo —> @Component
  • dao —> @Resource
  • service —> @Service
  • controller —> @Controller

@Scope

作用域注解,限定Spring Bean的作用范圍,在Spring配置文件定義Bean時(shí),通過聲明scope配置項(xiàng),可以靈活定義Bean的作用范圍

@ComponentScan

@ComponentScan(“com.wei.pojo”) 掃描包

@Bean

  • 注冊一個(gè)bean,就相當(dāng)于bean標(biāo)簽
  • 方法的名字,相當(dāng)于bean標(biāo)簽的id屬性
  • 方法的返回值,相當(dāng)于bean標(biāo)簽的class屬性
    @Bean
    public User getUser(){
        return new User();      //就是返回要注入到bean的對象
    }

@Configuration

@Configuration注解會(huì)被Spring容器托管,因?yàn)樗旧砭褪且粋€(gè)@Component

@Configuration 代表這是一個(gè)配置類,相當(dāng)于beans.xml

@Configuration
@ComponentScan("com.wei.pojo")      
@Import(WeiConfig2.class)           
public class WeiConfig {
    @Bean
    public User getUser(){
        return new User();      
    }
}

@Value

屬性注入值

Java類

此處使用@Scope注解使用prototype多實(shí)例注解模式

//等價(jià)于<bean id="user" class="com.wei.pojo.User       Component:組件
@Component
@Scope("prototype")		//作用域注解
public class User {
    @Value("wei_shuo")  //相當(dāng)于 <property name="name" value="wei_shuo"/>
    public String name;
}

P命名空間注入

p命名空間注入,對應(yīng)Set注入方式

需要?jiǎng)?chuàng)建set/get方法

p命名空間注入,可以直接注入屬性的值:property

User類

package com.wei.pojo;
public class User {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

userbeans.xml文件

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--上面添加 xmlns:p="http://www.springframework.org/schema/p" -->
    <!--p命名空間注入,可以直接注入屬性的值:property-->
    <bean id="user" class="com.wei.pojo.User" p:name="秦疆" p:age="18"/>
</beans>

Test測試

        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
//        指定類型User.class則不需要強(qiáng)制轉(zhuǎn)換
        User user = context.getBean("user", User.class);
//        強(qiáng)制轉(zhuǎn)換
//        User user = (User) context.getBean("user");
        System.out.println(user);

C命名空間注入

c命名空間注入,對應(yīng)構(gòu)造器注入

需要?jiǎng)?chuàng)建無參/帶參構(gòu)造方法

c命名空間注入,通過構(gòu)造器 注入:construct-args

User類

此處增加無參/帶參構(gòu)造方法

public class User {
    private String name;
    private int age;
    public User() {
    }
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

userbeans.xml文件

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--上面添加  xmlns:c="http://www.springframework.org/schema/c" -->
    <!--c命名空間注入,通過構(gòu)造器 注入:construct-args-->
    <bean id="user2" class="com.wei.pojo.User" c:name="wei_shuo" c:age="18"/>
</beans>

Test測試

        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user2", User.class);
        System.out.println(user);

總結(jié):

p命名空間注入/c命名空間注入 都需要導(dǎo)入xml約束,不能直接使用

xmlns:p=“http://www.springframework.org/schema/p”

xmlns:c=“http://www.springframework.org/schema/c”

Spring開發(fā)包名解釋

dao包

數(shù)據(jù)庫操作,crud 即增刪改查,對于數(shù)據(jù)庫的增刪改查的操作都在這里

pojo包

簡單java對象

service包

service 服務(wù)器層,也叫業(yè)務(wù)邏輯層,調(diào)用dao中的方法

Java方式配置

使用Java的方式配置Spring,也就是不寫bean.xml配置,使用注解代替

實(shí)體列

//@Configurable放在類上,說明這個(gè)類被Spring管理了,等同于bean配置,注冊到容器中
@Configurable
public class User {
    private String name;
    public String getName() {
        return name;
    }
    @Value("CSDN")  //屬性注入值
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

配置文件

  • @Configuration注解會(huì)被Spring容器托管,因?yàn)樗旧砭褪且粋€(gè)@Component
  • @Configuration 代表這是一個(gè)配置類,相當(dāng)于beans.xml
  • @Import注解,功能就是和Spring XML里面的一樣. @Import注解是用來導(dǎo)入配置類或者一些需要前置加載的類.通俗的將就是將類放入到IOC容器中
  • @Bean注解,注冊一個(gè)bean,就相當(dāng)于bean標(biāo)簽,方法的名字,相當(dāng)于bean標(biāo)簽的id屬性,方法的返回值,相當(dāng)于bean標(biāo)簽的class屬性
//@Configuration注解會(huì)被Spring容器托管,因?yàn)樗旧砭褪且粋€(gè)@Component
//@Configuration 代表這是一個(gè)配置類,相當(dāng)于beans.xml
@Configuration
@ComponentScan("com.wei.pojo")      //掃描包
@Import(WeiConfig2.class)           //@Import注解,功能就是和Spring XML里面的一樣. @Import注解是用來導(dǎo)入配置類或者一些需要前置加載的類.通俗的將就是將類放入到IOC容器中
public class WeiConfig {
    //注冊一個(gè)bean,就相當(dāng)于bean標(biāo)簽
    //方法的名字,相當(dāng)于bean標(biāo)簽的id屬性
    //方法的返回值,相當(dāng)于bean標(biāo)簽的class屬性
    @Bean
    public User getUser(){
        return new User();      //就是返回要注入到bean的對象
    }
}

測試類

public class MyTest {
    //如果完全使用了配置類方式去做,只能通過AnnotationConfig 上下文來獲取容器,通過配置類的class對象加載
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(WeiConfig.class);
        User getUser = (User) context.getBean("getUser");   //此處取方法名
        System.out.println(getUser.getName());
    }
}

到此這篇關(guān)于Spring注解與P/C命名空間超詳細(xì)解析的文章就介紹到這了,更多相關(guān)Spring注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

饶平县| 澎湖县| 宁河县| 和顺县| 三台县| 天全县| 泾川县| 永平县| 泊头市| 交城县| 普宁市| 沂水县| 丹东市| 新丰县| 诏安县| 涪陵区| 舞阳县| 郑州市| 随州市| 鹤峰县| 宜丰县| 当阳市| 蕉岭县| 申扎县| 小金县| 萍乡市| 磐石市| 永昌县| 建始县| 浑源县| 灌南县| 武山县| 泸定县| 安庆市| 安图县| 广宗县| 斗六市| 申扎县| 宝清县| 常宁市| 久治县|