广告
返回顶部
首页 > 资讯 > 后端开发 > Python >基于Spring p标签和c标签注入方式
  • 524
分享到

基于Spring p标签和c标签注入方式

2024-04-02 19:04:59 524人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录spring p和c标签注入方式1.编写实体类2.配置文件(重点)3.测试spring Bean注入和P标签使用1.构造方法参数2.setter方法注入(开发推荐)3.p名称空间

Spring p和c标签注入方式

1.编写实体类


package com.ming04.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
}

2.配置文件(重点)


<?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">
    <!-- p命名空间注入 ,可以直接注入属性的值 :property -->
    <bean id="user" class="com.ming04.pojo.User" p:name="秦小东" p:id="18"/>
    <!-- c命名空间注入 可以构造器注入属性的值 :construct-args -->
    <bean id="user2" class="com.qin.pojo.User" c:id="19" c:name="狂神" />
</beans>

3.测试


  @Test
    public void Text(){
       ApplicationContext context = new ClassPathXmlApplicationContext("Beans04.xml");
        User user = (User) context.getBean("user2");
        System.out.println(user);
    }

spring Bean注入和P标签使用

1.构造方法参数

对应 配置文件 <constructor-arg> 元素

可以index|name|type 三选一 、三选二 ; ref|value 二选一

2.setter方法注入(开发推荐)

为需要注入属性提供setter方法

配置 每个注入属性, 对应<property> 元素

3.p名称空间的使用

spring2.5以后,为了简化setter方法属性注入,引用p名称空间的概念,可以将<property> 子元素,简化为<bean>元素属性配置 !!

a.

在applicationContext.xml 引入p 名称空间


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd">

b. 简化注入的配置

4.集合类型的属性注入(了解)

Spring 对每个常用集合对象,提供单独元素完成注入

  • List 对象 ---- <list> 元素
  • Set 对象 ---- <set> 元素
  • Map对象 ----<map>元素
  • Properties 对象 ---- <props> 元素

集合属性的注入,主要用于框架的配置 !

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 基于Spring p标签和c标签注入方式

本文链接: https://www.lsjlt.com/news/136711.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作